Tadeck
Tadeck

Reputation: 137320

Introspect function to determine which argument unpacking (positional or keyword) was used

I was searching for a way to identify whether some argument is used for unpacking and I have found this:

>>> def func_has_positional_args(func):
    std_args = func.func_code.co_argcount
    wildcard_args = len(func.func_code.co_varnames) - std_args
    if wildcard_args == 2:
        return True  # yes, has both positional and keyword args
    elif wildcard_args == 0:
        return False  # has neither positional, nor keyword args
    else:
        raise NotImplementedError('Unable to tell')


>>> func_has_keyword_args = func_has_positional_args
>>> def test1(a, b, *args, **kwargs): pass

>>> func_has_positional_args(test1), func_has_keyword_args(test1)
(True, True)
>>> def test2(a, b): pass

>>> func_has_positional_args(test2), func_has_keyword_args(test2)
(False, False)
>>> def test3(a, b, *args): pass

>>> func_has_positional_args(test3)

Traceback (most recent call last):
  File "<pyshell#52>", line 1, in <module>
    func_has_positional_args(test3)
  File "<pyshell#41>", line 9, in func_has_positional_args
    raise NotImplementedError('Unable to tell')
NotImplementedError: Unable to tell

So I am able to tell, if there are is no positional, nor keyword arguments unpacking. I am also able to tell if there are both, but I am unable to distinguish which "wildcard" type argument is implemented, if there is only one "wildcard".

Could you help me achieve the following result?

# Already satisfied with above code:
assert func_has_positional_args(test1) == True
assert func_has_keyword_args(test1) == True
assert func_has_positional_args(test2) == False
assert func_has_keyword_args(test2) == False

# Missing functionality (tests are failing):
assert func_has_positional_args(test3) == True
assert func_has_keyword_args(test3) == False

Also, does Python 3 change anything with regard to this functionality or its behavior?

Upvotes: 4

Views: 254

Answers (1)

falsetru
falsetru

Reputation: 368964

As mgilson commented, use inspect.getargspec (more preferably inspect.getfullargspec in Python 3.x).

import inspect

def func_has_positional_args(func):
    spec = inspect.getfullargspec(func)
    return bool(spec.varargs) # varargs: name of the * argument or None
def func_has_keyword_args(func):
    spec = inspect.getfullargspec(func)
    return bool(spec.varkw)   # varkw: name of the ** argument or None

Example:

>>> def test1(a, b, *args, **kwargs): pass
...
>>> def test2(a, b): pass
...
>>> def test3(a, b, *args): pass
...
>>> func_has_positional_args(test1)
True
>>> func_has_keyword_args(test1)
True
>>> func_has_positional_args(test2)
False
>>> func_has_keyword_args(test2)
False
>>> func_has_positional_args(test3)
True
>>> func_has_keyword_args(test3)
False

Upvotes: 5

Related Questions