Skip to content

Commit 0316ae1

Browse files
committed
Deprecate setting the same property under two different aliases.
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 d72f069 commit 0316ae1

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed
Lines changed: 19 additions & 0 deletions
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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,12 +1063,12 @@ def properties(self):
10631063
return ArtistInspector(self).properties()
10641064

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

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

lib/matplotlib/cbook/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,9 +1725,11 @@ def normalize_kwargs(kw, alias_mapping=None, required=(), forbidden=(),
17251725
if tmp:
17261726
ret[canonical] = tmp[-1]
17271727
if len(tmp) > 1:
1728-
_warn_external("Saw kwargs {seen!r} which are all aliases for "
1729-
"{canon!r}. Kept value from {used!r}".format(
1730-
seen=seen, canon=canonical, used=seen[-1]))
1728+
warn_deprecated(
1729+
"3.1", message=f"Saw kwargs {seen!r} which are all "
1730+
f"aliases for {canonical!r}. Kept value from "
1731+
f"{seen[-1]!r}. Passing multiple aliases for the same "
1732+
f"property will raise a TypeError %(removal)s.")
17311733

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

0 commit comments

Comments
 (0)