Skip to content

Commit 2ab8b3a

Browse files
committed
Bug fix: get_num_args doesn't count self
1 parent 26e5e49 commit 2ab8b3a

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

deepxde/utils/internal.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,36 @@ def get_num_args(func):
166166
167167
- https://stackoverflow.com/questions/847936/how-can-i-find-the-number-of-arguments-of-a-python-function
168168
"""
169-
if sys.version_info[0] == 2:
170-
return len(inspect.getargspec(func).args)
171-
sig = inspect.signature(func)
172-
return len(sig.parameters)
169+
# If the function is a class method decorated with functools.wraps, then "self" will
170+
# be in parameters, as inspect.signature follows wrapper chains by default, see
171+
# https://stackoverflow.com/questions/308999/what-does-functools-wraps-do
172+
#
173+
# Example:
174+
#
175+
# import inspect
176+
# from functools import wraps
177+
#
178+
# def dummy(f):
179+
# print(f)
180+
# print(inspect.signature(f))
181+
#
182+
# @wraps(f)
183+
# def wrapper(*args, **kwargs):
184+
# f(*args, **kwargs)
185+
#
186+
# print(wrapper)
187+
# print(inspect.signature(wrapper))
188+
# return wrapper
189+
#
190+
# class A:
191+
# @dummy # See the difference by commenting out this line
192+
# def f(self, x):
193+
# pass
194+
#
195+
# print(A.f)
196+
# print(inspect.signature(A.f))
197+
#
198+
# a = A()
199+
# g = dummy(a.f)
200+
params = inspect.signature(func).parameters
201+
return len(params) - ("self" in params)

0 commit comments

Comments
 (0)