Skip to content

Commit da8dea2

Browse files
anntzertimhoffm
authored andcommitted
Deprecate setting the same property under two different aliases. (#12962)
This will allow getting rid of the notion of alias priority, and is consistent with Python's behavior (as explained in the changelog entry).
1 parent c4de984 commit da8dea2

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Setting the same artist property multiple time via aliases is deprecated
2+
````````````````````````````````````````````````````````````````````````
3+
4+
Previously, code such as ``plt.plot([0, 1], c="red", color="blue")`` would
5+
emit a warning indicating that ``c`` and ``color`` are aliases of one another,
6+
and only keep the ``color`` kwarg. This behavior is deprecated; in a future
7+
version, this will raise a TypeError, similarly to Python's behavior when a
8+
keyword argument is passed twice (``plt.plot([0, 1], c="red", c="blue")``).
9+
10+
This warning is raised by `~.cbook.normalize_kwargs`.
11+
12+
Artist.set now normalizes keywords before sorting them
13+
``````````````````````````````````````````````````````
14+
15+
`Artist.set` currently sorts its keyword arguments in reverse alphabetical
16+
order (with a special-case to put ``color`` at the end) before applying them.
17+
18+
It now normalizes aliases (and, as above, emits a warning on duplicate
19+
properties) before doing the sorting (so ``c`` goes to the end too).

lib/matplotlib/artist.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1064,12 +1064,12 @@ def properties(self):
10641064
return ArtistInspector(self).properties()
10651065

10661066
def set(self, **kwargs):
1067-
"""A property batch setter. Pass *kwargs* to set properties.
1068-
"""
1067+
"""A property batch setter. Pass *kwargs* to set properties."""
1068+
kwargs = cbook.normalize_kwargs(
1069+
kwargs, getattr(type(self), "_alias_map", {}))
10691070
props = OrderedDict(
10701071
sorted(kwargs.items(), reverse=True,
10711072
key=lambda x: (self._prop_order.get(x[0], 0), x[0])))
1072-
10731073
return self.update(props)
10741074

10751075
def findobj(self, match=None, include_self=True):

lib/matplotlib/cbook/__init__.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1799,9 +1799,11 @@ def normalize_kwargs(kw, alias_mapping=None, required=(), forbidden=(),
17991799
if tmp:
18001800
ret[canonical] = tmp[-1]
18011801
if len(tmp) > 1:
1802-
_warn_external("Saw kwargs {seen!r} which are all aliases for "
1803-
"{canon!r}. Kept value from {used!r}".format(
1804-
seen=seen, canon=canonical, used=seen[-1]))
1802+
warn_deprecated(
1803+
"3.1", message=f"Saw kwargs {seen!r} which are all "
1804+
f"aliases for {canonical!r}. Kept value from "
1805+
f"{seen[-1]!r}. Passing multiple aliases for the same "
1806+
f"property will raise a TypeError %(removal)s.")
18051807

18061808
# at this point we know that all keys which are aliased are removed, update
18071809
# the return dictionary from the cleaned local copy of the input

0 commit comments

Comments
 (0)