Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5334bcd

Browse files
committed
inspect.Signauture.from_function: validate duck functions in Signature constructor #17159
1 parent 63da7c7 commit 5334bcd

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

Lib/inspect.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,10 +2097,14 @@ def __init__(self, parameters=None, *, return_annotation=_empty,
20972097
def from_function(cls, func):
20982098
'''Constructs Signature for the given python function'''
20992099

2100-
if not (isfunction(func) or _signature_is_functionlike(func)):
2101-
# If it's not a pure Python function, and not a duck type
2102-
# of pure function:
2103-
raise TypeError('{!r} is not a Python function'.format(func))
2100+
is_duck_function = False
2101+
if not isfunction(func):
2102+
if _signature_is_functionlike(func):
2103+
is_duck_function = True
2104+
else:
2105+
# If it's not a pure Python function, and not a duck type
2106+
# of pure function:
2107+
raise TypeError('{!r} is not a Python function'.format(func))
21042108

21052109
Parameter = cls._parameter_cls
21062110

@@ -2164,9 +2168,11 @@ def from_function(cls, func):
21642168
parameters.append(Parameter(name, annotation=annotation,
21652169
kind=_VAR_KEYWORD))
21662170

2171+
# Is 'func' is a pure Python function - don't validate the
2172+
# parameters list (for correct order and defaults), it should be OK.
21672173
return cls(parameters,
21682174
return_annotation=annotations.get('return', _empty),
2169-
__validate_parameters__=False)
2175+
__validate_parameters__=is_duck_function)
21702176

21712177
@classmethod
21722178
def from_builtin(cls, func):

0 commit comments

Comments
 (0)