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

Skip to content

Commit 078790e

Browse files
authored
Merge pull request #22411 from timhoffm/move-define_aliases
Move cbook._define_aliases() to _api.define_aliases()
2 parents 28f5e91 + 45d1df6 commit 078790e

File tree

8 files changed

+61
-61
lines changed

8 files changed

+61
-61
lines changed

lib/matplotlib/_api/__init__.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,61 @@ def __getattr__(name):
225225
return __getattr__
226226

227227

228+
def define_aliases(alias_d, cls=None):
229+
"""
230+
Class decorator for defining property aliases.
231+
232+
Use as ::
233+
234+
@_api.define_aliases({"property": ["alias", ...], ...})
235+
class C: ...
236+
237+
For each property, if the corresponding ``get_property`` is defined in the
238+
class so far, an alias named ``get_alias`` will be defined; the same will
239+
be done for setters. If neither the getter nor the setter exists, an
240+
exception will be raised.
241+
242+
The alias map is stored as the ``_alias_map`` attribute on the class and
243+
can be used by `.normalize_kwargs` (which assumes that higher priority
244+
aliases come last).
245+
"""
246+
if cls is None: # Return the actual class decorator.
247+
return functools.partial(define_aliases, alias_d)
248+
249+
def make_alias(name): # Enforce a closure over *name*.
250+
@functools.wraps(getattr(cls, name))
251+
def method(self, *args, **kwargs):
252+
return getattr(self, name)(*args, **kwargs)
253+
return method
254+
255+
for prop, aliases in alias_d.items():
256+
exists = False
257+
for prefix in ["get_", "set_"]:
258+
if prefix + prop in vars(cls):
259+
exists = True
260+
for alias in aliases:
261+
method = make_alias(prefix + prop)
262+
method.__name__ = prefix + alias
263+
method.__doc__ = "Alias for `{}`.".format(prefix + prop)
264+
setattr(cls, prefix + alias, method)
265+
if not exists:
266+
raise ValueError(
267+
"Neither getter nor setter exists for {!r}".format(prop))
268+
269+
def get_aliased_and_aliases(d):
270+
return {*d, *(alias for aliases in d.values() for alias in aliases)}
271+
272+
preexisting_aliases = getattr(cls, "_alias_map", {})
273+
conflicting = (get_aliased_and_aliases(preexisting_aliases)
274+
& get_aliased_and_aliases(alias_d))
275+
if conflicting:
276+
# Need to decide on conflict resolution policy.
277+
raise NotImplementedError(
278+
f"Parent class already defines conflicting aliases: {conflicting}")
279+
cls._alias_map = {**preexisting_aliases, **alias_d}
280+
return cls
281+
282+
228283
def select_matching_signature(funcs, *args, **kwargs):
229284
"""
230285
Select and call the function that accepts ``*args, **kwargs``.

lib/matplotlib/axes/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ def _plot_args(self, tup, kwargs, return_kwargs=False):
537537
return [l[0] for l in result]
538538

539539

540-
@cbook._define_aliases({"facecolor": ["fc"]})
540+
@_api.define_aliases({"facecolor": ["fc"]})
541541
class _AxesBase(martist.Artist):
542542
name = "rectilinear"
543543

lib/matplotlib/cbook/__init__.py

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,61 +1834,6 @@ def _str_lower_equal(obj, s):
18341834
return isinstance(obj, str) and obj.lower() == s
18351835

18361836

1837-
def _define_aliases(alias_d, cls=None):
1838-
"""
1839-
Class decorator for defining property aliases.
1840-
1841-
Use as ::
1842-
1843-
@cbook._define_aliases({"property": ["alias", ...], ...})
1844-
class C: ...
1845-
1846-
For each property, if the corresponding ``get_property`` is defined in the
1847-
class so far, an alias named ``get_alias`` will be defined; the same will
1848-
be done for setters. If neither the getter nor the setter exists, an
1849-
exception will be raised.
1850-
1851-
The alias map is stored as the ``_alias_map`` attribute on the class and
1852-
can be used by `.normalize_kwargs` (which assumes that higher priority
1853-
aliases come last).
1854-
"""
1855-
if cls is None: # Return the actual class decorator.
1856-
return functools.partial(_define_aliases, alias_d)
1857-
1858-
def make_alias(name): # Enforce a closure over *name*.
1859-
@functools.wraps(getattr(cls, name))
1860-
def method(self, *args, **kwargs):
1861-
return getattr(self, name)(*args, **kwargs)
1862-
return method
1863-
1864-
for prop, aliases in alias_d.items():
1865-
exists = False
1866-
for prefix in ["get_", "set_"]:
1867-
if prefix + prop in vars(cls):
1868-
exists = True
1869-
for alias in aliases:
1870-
method = make_alias(prefix + prop)
1871-
method.__name__ = prefix + alias
1872-
method.__doc__ = "Alias for `{}`.".format(prefix + prop)
1873-
setattr(cls, prefix + alias, method)
1874-
if not exists:
1875-
raise ValueError(
1876-
"Neither getter nor setter exists for {!r}".format(prop))
1877-
1878-
def get_aliased_and_aliases(d):
1879-
return {*d, *(alias for aliases in d.values() for alias in aliases)}
1880-
1881-
preexisting_aliases = getattr(cls, "_alias_map", {})
1882-
conflicting = (get_aliased_and_aliases(preexisting_aliases)
1883-
& get_aliased_and_aliases(alias_d))
1884-
if conflicting:
1885-
# Need to decide on conflict resolution policy.
1886-
raise NotImplementedError(
1887-
f"Parent class already defines conflicting aliases: {conflicting}")
1888-
cls._alias_map = {**preexisting_aliases, **alias_d}
1889-
return cls
1890-
1891-
18921837
def _array_perimeter(arr):
18931838
"""
18941839
Get the elements on the perimeter of *arr*.

lib/matplotlib/collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
# "color" is excluded; it is a compound setter, and its docstring differs
2626
# in LineCollection.
27-
@cbook._define_aliases({
27+
@_api.define_aliases({
2828
"antialiased": ["antialiaseds", "aa"],
2929
"edgecolor": ["edgecolors", "ec"],
3030
"facecolor": ["facecolors", "fc"],

lib/matplotlib/lines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def _slice_or_none(in_v, slc):
198198

199199

200200
@docstring.interpd
201-
@cbook._define_aliases({
201+
@_api.define_aliases({
202202
"antialiased": ["aa"],
203203
"color": ["c"],
204204
"drawstyle": ["ds"],

lib/matplotlib/patches.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
@docstring.interpd
27-
@cbook._define_aliases({
27+
@_api.define_aliases({
2828
"antialiased": ["aa"],
2929
"edgecolor": ["ec"],
3030
"facecolor": ["fc"],

lib/matplotlib/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def _get_text_metrics_with_cache_impl(
106106

107107

108108
@docstring.interpd
109-
@cbook._define_aliases({
109+
@_api.define_aliases({
110110
"color": ["c"],
111111
"fontfamily": ["family"],
112112
"fontproperties": ["font", "font_properties"],

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242

4343
@docstring.interpd
44-
@cbook._define_aliases({
44+
@_api.define_aliases({
4545
"xlim": ["xlim3d"], "ylim": ["ylim3d"], "zlim": ["zlim3d"]})
4646
class Axes3D(Axes):
4747
"""

0 commit comments

Comments
 (0)