10000 Merge pull request #2 from tacaswell/bad_linestyle · matplotlib/matplotlib@608ad21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 608ad21

Browse files
committed
Merge pull request #2 from tacaswell/bad_linestyle
ENH : raise on invalid linestyle
2 parents 8973c40 + 0230395 commit 608ad21

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lib/matplotlib/lines.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -971,10 +971,18 @@ def set_linestyle(self, linestyle):
971971
linestyle = '-'
972972
break
973973

974-
if linestyle not in self._lineStyles:
975-
linestyle = ls_mapper_r.get(linestyle, linestyle)
976-
if linestyle in [' ', '']:
974+
if linestyle in [' ', '', 'none']:
977975
linestyle = 'None'
976+
977+
if linestyle not in self._lineStyles:
978+
try:
979+
linestyle = ls_mapper_r[linestyle]
980+
except KeyError:
981+
raise ValueError(("You passed in an invalid linestyle, "
982+
"`{}`. See "
983+
"docs of Line2D.set_linestyle for "
984+
"valid values.").format(linestyle))
985+
978986
self._linestyle = linestyle
979987

980988
@docstring.dedent_interpd

lib/matplotlib/tests/test_lines.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
import six
88

9-
from nose.tools import assert_true
9+
import nose
10+
from nose.tools import assert_true, assert_raises
1011
from timeit import repeat
1112
import numpy as np
1213
import matplotlib as mpl
1314
import matplotlib.pyplot as plt
1415
from matplotlib.testing.decorators import cleanup, image_comparison
16+
import sys
1517

1618

1719
@cleanup
@@ -104,6 +106,17 @@ def test_linestyle_variants():
104106
assert True
105107

106108

109+
@cleanup
110+
def test_valid_linestyles():
111+
if sys.version_info[:2] < (2, 7):
112+
raise nose.SkipTest("assert_raises as context manager "
113+
"not supported with Python < 2.7")
114+
115+
line = mpl.lines.Line2D([], [])
116+
with assert_raises(ValueError):
117+
line.set_linestyle('aardvark')
118+
119+
107120
@image_comparison(baseline_images=['line_collection_dashes'], remove_text=True)
108121
def test_set_line_coll_dash_image():
109122
fig = plt.figure()
@@ -121,5 +134,4 @@ def test_nan_is_sorted():
121134

122135

123136
if __name__ == '__main__':
124-
import nose
125137
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)
0