MNT: inspect.getfullargspec will be deprecated in py3.8#14138
MNT: inspect.getfullargspec will be deprecated in py3.8#14138tacaswell wants to merge 2 commits intomatplotlib:masterfrom
Conversation
Switch to using inspect.signature
lib/matplotlib/artist.py
Outdated
| if not callable(func): | ||
| continue | ||
| nargs = len(inspect.getfullargspec(func).args) | ||
| nargs = len(inspect.signature(func).parameters) |
There was a problem hiding this comment.
Technically, this is not the same because Signature.parameters contains everything.
I think
len([p for p in inspect.signature(func).parameters.values()
if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD])
is equivalent, but haven't checked for cases with variable positional args or kwonly.
Some example code which may be useful to play around with:
>>> from pprint import pprint
>>> import inspect
>>> def f(a, b, c=0, **kwargs):
... pass
...
>>> inspect.getfullargspec(f)
FullArgSpec(args=['a', 'b', 'c'], varargs=None, varkw='kwargs', defaults=(0,), kwonlyargs=[], kwonlydefaults=None, annotations={})
>>> pprint(inspect.signature(f).parameters)
mappingproxy(OrderedDict([('a', <Parameter "a">),
('b', <Parameter "b">),
('c', <Parameter "c=0">),
('kwargs', <Parameter "**kwargs">)]))
There was a problem hiding this comment.
However, the only thing we use nargs for is in the next line to detect if there if it less than 2 to detect if this is something that is named like a setter, but does not take enough arguments to actually be a setter.
There was a problem hiding this comment.
For the following
len(inspect.getfullargspec(func).args) < 2 is true but len(inspect.signature(func).parameters) < 2 is false:
Line2D.set_data(self, *args)
Axes.set_prop_cycle(self, *args, **kwargs)
Figure.set_constrained_layout_pads(self, **kwargs)
I did not check the full code path, but I assume they will be considered setters after the change - not sure if that's actually desired or not, but it's a change to be aware of.
There was a problem hiding this comment.
fair enough, will make the changes!
Thanks for pushing on this.
| else: | ||
| sig = inspect.signature(cls.__init__) | ||
| argstr = ", ".join( | ||
| f"{p.name}={p.default}" |
There was a problem hiding this comment.
Actually that's just str(p) (which will print out as name=default in presence of a default, isn't the stdlib wonderful? :p)
and the whole thing can be simplified down to
argstr = (str(inspect.signature(cls))[1:-1] # Strip parentheses.
or "None")
because style classes don't take other arguments, and inspect.signature(cls) helpfully drops self from the signature of __init__...
There was a problem hiding this comment.
"if something seems like it is too hard, it probably is"
|
Looks like |
|
Superseeded by #14962. |
Switch to using inspect.signature
Still need to verify that the replacement is equivalent (which is why I marked it as draft), but pushing this out early so I don't forget I did this.