Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2128c96

Browse files
committed
Make the validation mutation-safe, and improve error messages
1 parent bfe5e3f commit 2128c96

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,13 +781,29 @@ def validate_cycler(s):
781781
else:
782782
raise ValueError("object was not a string or Cycler instance: %s" % s)
783783

784-
unknowns = cycler_inst.keys - (set(_prop_validators.keys()) |
785-
set(_prop_aliases.keys()))
784+
unknowns = cycler_inst.keys - (set(_prop_validators) | set(_prop_aliases))
786785
if unknowns:
787786
raise ValueError("Unknown artist properties: %s" % unknowns)
788787

789788
# Not a full validation, but it'll at least normalize property names
790789
# A fuller validation would require v0.10 of cycler.
790+
checker = set()
791+
for prop in cycler_inst.keys:
792+
norm_prop = _prop_aliases.get(prop, prop)
793+
if norm_prop != prop and norm_prop in cycler_inst.keys:
794+
raise ValueError("Cannot specify both '{0}' and alias '{1}'"
795+
" in the same prop_cycle".format(norm_prop, prop))
796+
if norm_prop in checker:
797+
raise ValueError("Another property was already aliased to '{0}'."
798+
" Collision normalizing '{1}'.".format(norm_prop,
799+
prop))
800+
checker.update([norm_prop])
801+
802+
# This is just an extra-careful check, just in case there is some
803+
# edge-case I haven't thought of.
804+
assert len(checker) == len(cycler_inst.keys)
805+
806+
# Now, it should be safe to mutate this cycler
791807
for prop in cycler_inst.keys:
792808
norm_prop = _prop_aliases.get(prop, prop)
793809
cycler_inst.change_key(prop, norm_prop)

lib/matplotlib/tests/test_cycles.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ def test_invalid_input_forms():
186186
foobar=[1, 2])
187187
assert_raises((TypeError, ValueError), ax.set_prop_cycle,
188188
cycler(foobar=[1, 2]))
189+
assert_raises(ValueError, ax.set_prop_cycle,
190+
cycler(color='rgb', c='cmy'))
189191

190192

191193
if __name__ == '__main__':

0 commit comments

Comments
 (0)