Skip to content

Deprecate setting the same property under two different aliases. #12962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions doc/api/next_api_changes/2018-12-09-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Setting the same artist property multiple time via aliases is deprecated
````````````````````````````````````````````````````````````````````````

Previously, code such as ``plt.plot([0, 1], c="red", color="blue")`` would
emit a warning indicating that ``c`` and ``color`` are aliases of one another,
and only keep the ``color`` kwarg. This behavior is deprecated; in a future
version, this will raise a TypeError, similarly to Python's behavior when a
keyword argument is passed twice (``plt.plot([0, 1], c="red", c="blue")``).

This warning is raised by `~.cbook.normalize_kwargs`.

Artist.set now normalizes keywords before sorting them
``````````````````````````````````````````````````````

`Artist.set` currently sorts its keyword arguments in reverse alphabetical
order (with a special-case to put ``color`` at the end) before applying them.

It now normalizes aliases (and, as above, emits a warning on duplicate
properties) before doing the sorting (so ``c`` goes to the end too).
6 changes: 3 additions & 3 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,12 +1063,12 @@ def properties(self):
return ArtistInspector(self).properties()

def set(self, **kwargs):
"""A property batch setter. Pass *kwargs* to set properties.
"""
"""A property batch setter. Pass *kwargs* to set properties."""
kwargs = cbook.normalize_kwargs(
kwargs, getattr(type(self), "_alias_map", {}))
props = OrderedDict(
sorted(kwargs.items(), reverse=True,
key=lambda x: (self._prop_order.get(x[0], 0), x[0])))

return self.update(props)

def findobj(self, match=None, include_self=True):
Expand Down
8 changes: 5 additions & 3 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1725,9 +1725,11 @@ def normalize_kwargs(kw, alias_mapping=None, required=(), forbidden=(),
if tmp:
ret[canonical] = tmp[-1]
if len(tmp) > 1:
_warn_external("Saw kwargs {seen!r} which are all aliases for "
"{canon!r}. Kept value from {used!r}".format(
seen=seen, canon=canonical, used=seen[-1]))
warn_deprecated(
"3.1", message=f"Saw kwargs {seen!r} which are all "
f"aliases for {canonical!r}. Kept value from "
f"{seen[-1]!r}. Passing multiple aliases for the same "
f"property will raise a TypeError %(removal)s.")

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