I get the following WARNING while generating docs.
Warning, treated as error:
/home/aashil/github/matplotlib/doc/api/_as_gen/matplotlib.axes.Axes.clabel.rst:83: WARNING: Exception occurred in plotting contour_demo
from /home/aashil/github/matplotlib/doc/mpl_examples/pylab_examples/contour_demo.py:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/matplotlib-2.0.0+3486.g8c57dae-py2.7-linux-x86_64.egg/matplotlib/sphinxext/plot_directive.py", line 525, in run_code
six.exec_(code, ns)
File "/usr/local/lib/python2.7/dist-packages/six.py", line 699, in exec_
exec("""exec _code_ in _globs_, _locs_""")
File "<string>", line 1, in <module>
File "<string>", line 55, in <module>
File "/usr/local/lib/python2.7/dist-packages/matplotlib-2.0.0+3486.g8c57dae-py2.7-linux-x86_64.egg/matplotlib/__init__.py", line 916, in __setitem__
raise ValueError("Key %s: %s" % (key, str(ve)))
ValueError: Key contour.negative_linestyle: linestyle must be a string or an even-length sequence of floats.
Building HTML failed.
The command from contour_demo.py which throws the warning is:
With the vanilla line 55 in contour.py (i.e. matplotlib.rcParams['contour.negative_linestyle'] = 'solid'), the same ValueError exception is raised when executing the script outside of building the documentation. This exception is related to the new validation scheme for line styles, see #8040. Using u'solid' instead of 'solid' makes the exception to disappear.
@tacaswell Is it expected for such an exception to be uncaught during the CI tests?
@tacaswell It looks like you are right. With Python 2.7:
In [1]: frommatplotlib.rcsetupimport_validate_linestyleIn [2]: _validate_linestyle(u'solid')
Out[2]: u'solid'In [3]: _validate_linestyle('solid')
---------------------------------------------------------------------------ValueErrorTraceback (mostrecentcalllast)
<ipython-input-3-9192256a512f>in<module>()
---->1_validate_linestyle('solid')
/home/adrien/matplotlib-git/lib/matplotlib/rcsetup.pycin_validate_linestyle(ls)
928929raiseValueError("linestyle must be a string or "+-->930"an even-length sequence of floats.")
931932ValueError: linestylemustbeastringoraneven-lengthsequenceoffloats.
calling decode like that is not safe as if you are getting in a byte string you don't know what encoding it has. Further, in python 3 that will error because str has no method 'decode' (that is a bytes method).
I do not fully understand what is going on here though.
Python 2.7.13 (default, Dec 21 2016, 07:16:46)
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: d = {'foo': 1, u'foo': 2}
In [2]: d
Out[2]: {'foo': 2}
In [3]: d = {u'foo': 5}
In [4]: 'foo' in d
Out[4]: True
In [5]:
@tacaswell If one trusts SO, it is the expected behavior with Python 2, which “handles translation between str and unicode keys transparently for you, provided the text can be encoded to ASCII.” See for example here and there.
About the reported issue here, I think the problem may be due to the test
ifisinstance(ls, six.text_type):
in the _validate_linestyle function:
Python2.7.11|ContinuumAnalytics, Inc.| (default, Dec62015, 18:08:32)
[...]
In [1]: frommatplotlib.rcsetupimport_validate_named_linestyle, _validate_linestyleIn [2]: importsixIn [3]: _validate_named_linestyle('solid') # No problem with a valid ASCII stringOut[3]: u'solid'In [4]: _validate_linestyle('solid') # Error with a valid ASCII string---------------------------------------------------------------------------ValueErrorTraceback (mostrecentcalllast)
<ipython-input-4-9192256a512f>in<module>()
---->1_validate_linestyle('solid')
/home/adrien/matplotlib-git/matplotlib/lib/matplotlib/rcsetup.pycin_validate_linestyle(ls)
928929raiseValueError("linestyle must be a string or "+-->930"an even-length sequence of floats.")
931932ValueError: linestylemustbeastringoraneven-lengthsequenceoffloats.
In [5]: isinstance('solid', six.text_type) # => issue w/ the aforementioned test...Out[5]: FalseIn [6]: isinstance(u'solid', six.text_type) # => expected behaviorOut[6]: True
I used six.text_type in the current version of _validate_linestyle because it was what was used for what seemed to be similar comparisons to me in rcsetup.py, but maybe the if isinstance(ls, …) should test against six.string_types instead of six.text_type:
from __future__ importprint_functionimportsixfrommatplotlib.rcsetupimport_validate_named_linestyledef_validate_linestyle(ls):
""" A validator for all possible line styles, the named ones *and* the on-off ink sequences. """# Named line style, like u'--' or u'solid'ifisinstance(ls, six.string_types): # <= Here is the change!return_validate_named_linestyle(ls)
# On-off ink (in points) sequence *of even length*.# Offset is set to None.try:
iflen(ls) %2!=0:
# Expecting a sequence of even lengthraiseValueErrorreturn (None, validate_nseq_float()(ls))
except (ValueError, TypeError):
# TypeError can be raised by wrong types passed to float()# (called inside the instance of validate_nseq_float).passraiseValueError("linestyle must be a string or "+"an even-length sequence of floats.")
# Testing different types of strings for the same valid name of line style.validated_strings= []
forlsin ['solid', u'solid', bytes('solid')]:
validated_strings.append(_validate_linestyle(ls))
print(validated_strings) # => [u'solid', u'solid', u'solid']
Activity
afvincent commented on Feb 25, 2017
Thank you @aashil for reporting this issue.
With the vanilla line 55 in contour.py (i.e. matplotlib.rcParams['contour.negative_linestyle'] = 'solid'), the same ValueError exception is raised when executing the script outside of building the documentation. This exception is related to the new validation scheme for line styles, see #8040. Using u'solid' instead of 'solid' makes the exception to disappear.
@tacaswell Is it expected for such an exception to be uncaught during the CI tests?
tacaswell commented on Feb 26, 2017
We build the docs on py3 so it reasonable that we missed this.
I think this is a unicode bug in the recently merged dash-pattern validator.
afvincent commented on Feb 26, 2017
@tacaswell It looks like you are right. With Python 2.7:
afvincent commented on Feb 26, 2017
In _validate_linestyle(ls), should I use something like:
... if isinstance(ls, six.text_type): return _validate_named_linestyle(ls.decode()) ...to make sure that we pass a unicode (string) argument? Or should this kind of conversion be handled differently?
tacaswell commented on Feb 26, 2017
calling decode like that is not safe as if you are getting in a byte string you don't know what encoding it has. Further, in python 3 that will error because str has no method 'decode' (that is a bytes method).
I do not fully understand what is going on here though.
Python 2.7.13 (default, Dec 21 2016, 07:16:46) Type "copyright", "credits" or "license" for more information. IPython 5.1.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: d = {'foo': 1, u'foo': 2} In [2]: d Out[2]: {'foo': 2} In [3]: d = {u'foo': 5} In [4]: 'foo' in d Out[4]: True In [5]:afvincent commented on Feb 27, 2017
@tacaswell If one trusts SO, it is the expected behavior with Python 2, which “handles translation between str and unicode keys transparently for you, provided the text can be encoded to ASCII.” See for example here and there.
About the reported issue here, I think the problem may be due to the test
in the _validate_linestyle function:
I used six.text_type in the current version of _validate_linestyle because it was what was used for what seemed to be similar comparisons to me in rcsetup.py, but maybe the if isinstance(ls, …) should test against six.string_types instead of six.text_type:
tacaswell commented on Feb 27, 2017
Or skip the type checking and just do
afvincent commented on Feb 27, 2017
Indeed ^^. I am going to open a PR to fix the issue as you suggest.
afvincent commented on Feb 27, 2017
With the PR #8165, I was able to locally build the documentation under Python 2.7.