8000 ENH : raise on invalid linestyle · matplotlib/matplotlib@a09d4ad · GitHub
[go: up one dir, main page]

Skip to content

Commit a09d4ad

Browse files
tacaswelllennart0901
authored andcommitted
ENH : raise on invalid linestyle
Added check in code + test for raise
1 parent e6718ca commit a09d4ad

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/matplotlib/lines.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,12 @@ def set_linestyle(self, linestyle):
977977
break
978978

979979
if linestyle not in self._lineStyles:
980-
linestyle = ls_mapper_r.get(linestyle, linestyle)
980+
try:
981+
linestyle = ls_mapper_r[linestyle]
982+
except KeyError:
983+
raise ValueError("You passed in an invalid linestyle, see "
984+
"docs of Line2D.set_linestyle for "
985+
"valid values")
981986
if linestyle in [' ', '']:
982987
linestyle = 'None'
983988
self._linestyle = linestyle

lib/matplotlib/tests/test_lines.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
import six
88

9-
from nose.tools import assert_true
9+
from nose.tools import assert_true, assert_raises
1010
from timeit import repeat
1111
import numpy as np
1212
import matplotlib as mpl
1313
import matplotlib.pyplot as plt
1414
from matplotlib.testing.decorators import cleanup, image_comparison
15+
import sys
1516

1617

1718
@cleanup
@@ -104,6 +105,17 @@ def test_linestyle_variants():
104105
assert True
105106

106107

108+
@cleanup
109+
def test_valid_linestyles():
110+
if sys.version_info[:2] < (2, 7):
111+
raise nose.SkipTest("assert_raises as context manager "
112+
"not supported with Python < 2.7")
113+
114+
line = mpl.lines.Line2D([], [])
115+
with assert_raises(ValueError):
116+
line.set_linestyle('aardvark')
117+
118+
107119
@image_comparison(baseline_images=['line_collection_dashes'], remove_text=True)
108120
def test_set_line_coll_dash_image():
109121
fig = plt.figure()

0 commit comments

Comments
 (0)
0