10000 MNT: inspect.getfullargspec will be deprecated in py3.8 by tacaswell · Pull Request #14138 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

MNT: inspect.getfullargspec will be deprecated in py3.8 #14138

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

Closed
wants to merge 2 commits into from

Conversation

tacaswell
Copy link
Member

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.

@tacaswell tacaswell added this to the v3.1.1 milestone May 6, 2019
@@ -1270,7 +1270,7 @@ def _get_setters_and_targets(self):
func = getattr(self.o, name)
if not callable(func):
continue
nargs = len(inspect.getfullargspec(func).args)
nargs = len(inspect.signature(func).parameters)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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">)]))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member
@timhoffm timhoffm May 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough, will make the changes!

Thanks for pushing on this.

else:
sig = inspect.signature(cls.__init__)
argstr = ", ".join(
f"{p.name}={p.default}"
Copy link
Contributor
@anntzer anntzer May 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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__...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"if something seems like it is too hard, it probably is"

@cgohlke
Copy link
Contributor
cgohlke commented Jun 7, 2019
8000

Looks like getfullargspec has been undeprecated python/cpython#13245

@timhoffm
Copy link
Member
timhoffm commented Aug 5, 2019

Superseeded by #14962.

@timhoffm timhoffm closed this Aug 5, 2019
@tacaswell tacaswell deleted the mnt_py38_compat branch August 5, 2019 17:09
@QuLogic QuLogic removed this from the v3.2.0 milestone Aug 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0