test() gets the number of arguments that the function being tested expects by accessing function.__code__.co_argcount and compares this number with the number of arguments given in the test table. However, for methods, the first argument (the instance of the class, conventionally named self) is not supplied by the user, yet is still used and counted by co_argcount. Therefore, when testing a method with a table that has the correct number of arguments, test() will raise an error, since the arg count will always be one greater than number of args supplied by the user.
inspect is part of the standard library so doesn't introduce any dependencies. But some other options could be to write our own is_method() function, or use a parameter with a default value and have user decide if method is being passed.
Thanks for spotting this! Using inspect is fine. Could you please create the branch, apply the fix, test also with normal functions and do a PR? Thanks!
This issue relates to testing methods that return some object and do not have any side effects (i.e modifying the state of the object) e.g.
test()
gets the number of arguments that the function being tested expects by accessingfunction.__code__.co_argcount
and compares this number with the number of arguments given in the test table. However, for methods, the first argument (the instance of the class, conventionally namedself
) is not supplied by the user, yet is still used and counted byco_argcount
. Therefore, when testing a method with a table that has the correct number of arguments,test()
will raise an error, since the arg count will always be one greater than number of args supplied by the user.Suggested fix:
inspect
is part of the standard library so doesn't introduce any dependencies. But some other options could be to write our own is_method() function, or use a parameter with a default value and have user decide if method is being passed.