8000 Deprecate setting the same property under two different aliases. · matplotlib/matplotlib@d229bac · GitHub
[go: up one dir, main page]

Skip to content

Commit d229bac

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 d229bac

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,9 @@ def normalize_kwargs(kw, alias_mapping=None, required=(), forbidden=(),
17281728
_warn_external("Saw kwargs {seen!r} which are all aliases for "
17291729
"{canon!r}. Kept value from {used!r}".format(
17301730
seen=seen, canon=canonical, used=seen[-1]))
1731+
warn_deprecated(
1732+
"3.1", "Passing multiple aliases for the same property "
1733+
"will raise a TypeError %(removal)s.")
17311734

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

0 commit comments

Comments
 (0)
0