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

Skip to content

Commit e5b62d9

Browse files
committed
Improve alias signatures
1 parent 76fe3ab commit e5b62d9

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,20 +1914,28 @@ class so far, an alias named ``get_alias`` will be defined; the same will
19141914
if cls is None: # Return the actual class decorator.
19151915
return functools.partial(_define_aliases, alias_d)
19161916

1917-
def make_alias(name): # Enforce a closure over *name*.
1918-
def method(self, *args, **kwargs):
1919-
return getattr(self, name)(*args, **kwargs)
1920-
return method
1917+
def make_alias(method):
1918+
import inspect
1919+
args = str(inspect.signature(method))[1:-1] # remove parentheses
1920+
if args[:6] == 'self, ':
1921+
call_args = args[6:]
1922+
elif args == 'self':
1923+
call_args = ''
1924+
else:
1925+
raise RuntimeError('Unexpected signature: %s' % args)
1926+
alias_method = eval('lambda {}: self.{}({})'.format(
1927+
args, method.__name__, call_args))
1928+
alias_method.__doc__ = "Alias for `{}`.".format(method.__name__)
1929+
return alias_method
19211930

19221931
for prop, aliases in alias_d.items():
19231932
exists = False
19241933
for prefix in ["get_", "set_"]:
19251934
if prefix + prop in vars(cls):
19261935
exists = True
19271936
for alias in aliases:
1928-
method = make_alias(prefix + prop)
1937+
method = make_alias(getattr(cls, prefix + prop))
19291938
method.__name__ = prefix + alias
1930-
method.__doc__ = "Alias for `{}`.".format(prefix + prop)
19311939
setattr(cls, prefix + alias, method)
19321940
if not exists:
19331941
raise ValueError(

0 commit comments

Comments
 (0)