@@ -2805,14 +2805,13 @@ def _str_lower_equal(obj, s):
28052805 return isinstance (obj , six .string_types ) and obj .lower () == s
28062806
28072807
2808- def _define_aliases (local_d , alias_d ):
2809- """Define property aliases.
2808+ def _define_aliases (alias_d , cls = None ):
2809+ """Class decorator for defining property aliases.
28102810
2811- Use in a class definition as ::
2811+ Use as ::
28122812
2813- cbook._define_aliases(locals(), {
2814- "property": ["alias", ...], ...
2815- })
2813+ @cbook._define_aliases({"property": ["alias", ...], ...})
2814+ class C: ...
28162815
28172816 For each property, if the corresponding ``get_property`` is defined in the
28182817 class so far, an alias named ``get_alias`` will be defined; the same will
@@ -2822,6 +2821,8 @@ class so far, an alias named ``get_alias`` will be defined; the same will
28222821 The alias map is stored as the ``_alias_map`` attribute on the class and
28232822 can be used by `~.normalize_kwargs`.
28242823 """
2824+ if cls is None :
2825+ return functools .partial (_define_aliases , alias_d )
28252826
28262827 def make_alias (name ): # Enfore a closure over *name*.
28272828 def method (self , * args , ** kwargs ):
@@ -2831,14 +2832,15 @@ def method(self, *args, **kwargs):
28312832 for prop , aliases in alias_d .items ():
28322833 exists = False
28332834 for prefix in ["get_" , "set_" ]:
2834- if prefix + prop in local_d :
2835+ if prefix + prop in vars ( cls ) :
28352836 exists = True
28362837 for alias in aliases :
28372838 method = make_alias (prefix + prop )
28382839 method .__name__ = prefix + alias
28392840 method .__doc__ = "alias for `{}`" .format (prefix + prop )
2840- local_d [ prefix + alias ] = method
2841+ setattr ( cls , prefix + alias , method )
28412842 if not exists :
28422843 raise ValueError ("property {} does not exist" .format (prop ))
28432844
2844- local_d ["_alias_map" ] = alias_d
2845+ cls ._alias_map = alias_d
2846+ return cls
0 commit comments