I\'m looking for a way to check the number of arguments that a given function takes in Python. The purpose is to achieve a more robust method of patching my classes for tests.
You should use inspect.getargspec.
inspect.getargspec
is deprecated in Python 3. Consider something like:
import inspect
len(inspect.signature(foo_func).parameters)
The inspect module allows you to examine a function's arguments. This has been asked a few times on Stack Overflow; try searching for some of those answers. For example:
Getting method parameter names in python
You can use:
import inspect
len(inspect.getargspec(foo_func)[0])
This won't acknowledge variable-length parameters, like:
def foo(a, b, *args, **kwargs):
pass