Skip to content

Commit

Permalink
Convert hooks.varnames() to inspect.signature() on Python 3
Browse files Browse the repository at this point in the history
Avoids a possible deprecation warning with inspect.getfullargspec()

Fixes pytest-dev#209
  • Loading branch information
hroncok committed May 13, 2019
1 parent f8af26c commit ec28f02
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions src/pluggy/hooks.py
Expand Up @@ -124,18 +124,6 @@ def normalize_hookimpl_opts(opts):
opts.setdefault("optionalhook", False)


if hasattr(inspect, "getfullargspec"):

def _getargspec(func):
return inspect.getfullargspec(func)


else:

def _getargspec(func):
return inspect.getargspec(func)


_PYPY3 = hasattr(sys, "pypy_version_info") and sys.version_info.major == 3


Expand Down Expand Up @@ -163,22 +151,41 @@ def varnames(func):
except Exception:
return ()

try: # func MUST be a function or method here or we won't parse any args
spec = _getargspec(func)
except TypeError:
return (), ()
# func MUST be a function or method here or we won't parse any args
# this is what we return if it is not
non_function = (), ()

args, defaults = tuple(spec.args), spec.defaults
if defaults:
index = -len(defaults)
args, defaults = args[:index], tuple(args[index:])
if hasattr(inspect, "signature"):
try:
signature = inspect.signature(func)
except ValueError:
return non_function

empty = inspect.Parameter.empty
variable = (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD)
parameters = signature.parameters.values()
parameters = tuple(p for p in parameters if p.kind not in variable)
args = tuple(p.name for p in parameters if p.default is empty)
defaults = tuple(p.name for p in parameters if p.default is not empty)
mangled_self = inspect.ismethod(func)
else:
defaults = ()
try:
spec = inspect.getargspec(func)
except TypeError:
return non_function

args, defaults = tuple(spec.args), spec.defaults
if defaults:
index = -len(defaults)
args, defaults = args[:index], tuple(args[index:])
else:
defaults = ()
mangled_self = False

# strip any implicit instance arg
# pypy3 uses "obj" instead of "self" for default dunder methods
implicit_names = ("self",) if not _PYPY3 else ("self", "obj")
if args:
if args and not mangled_self:
if inspect.ismethod(func) or (
"." in getattr(func, "__qualname__", ()) and args[0] in implicit_names
):
Expand Down

0 comments on commit ec28f02

Please sign in to comment.