Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
215a75d8
Commit
215a75d8
authored
Jan 28, 2014
by
Yury Selivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
inspect.signature: Handle bound methods with '(*args)' signature correctly #20401
parent
7ff411ec
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
9 deletions
+53
-9
Lib/inspect.py
Lib/inspect.py
+34
-5
Lib/test/test_inspect.py
Lib/test/test_inspect.py
+19
-4
No files found.
Lib/inspect.py
View file @
215a75d8
...
...
@@ -1440,7 +1440,11 @@ def _get_user_defined_method(cls, method_name):
return
meth
def
_get_partial_signature
(
wrapped_sig
,
partial
,
extra_args
=
()):
def
_signature_get_partial
(
wrapped_sig
,
partial
,
extra_args
=
()):
# Internal helper to calculate how 'wrapped_sig' signature will
# look like after applying a 'functools.partial' object (or alike)
# on it.
new_params
=
OrderedDict
(
wrapped_sig
.
parameters
.
items
())
partial_args
=
partial
.
args
or
()
...
...
@@ -1485,6 +1489,31 @@ def _get_partial_signature(wrapped_sig, partial, extra_args=()):
return
wrapped_sig
.
replace
(
parameters
=
new_params
.
values
())
def
_signature_bound_method
(
sig
):
# Internal helper to transform signatures for unbound
# functions to bound methods
params
=
tuple
(
sig
.
parameters
.
values
())
if
not
params
or
params
[
0
].
kind
in
(
_VAR_KEYWORD
,
_KEYWORD_ONLY
):
raise
ValueError
(
'invalid method signature'
)
kind
=
params
[
0
].
kind
if
kind
in
(
_POSITIONAL_OR_KEYWORD
,
_POSITIONAL_ONLY
):
# Drop first parameter:
# '(p1, p2[, ...])' -> '(p2[, ...])'
params
=
params
[
1
:]
else
:
if
kind
is
not
_VAR_POSITIONAL
:
# Unless we add a new parameter type we never
# get here
raise
ValueError
(
'invalid argument type'
)
# It's a var-positional parameter.
# Do nothing. '(*args[, ...])' -> '(*args[, ...])'
return
sig
.
replace
(
parameters
=
params
)
def
signature
(
obj
):
'''Get a signature object for the passed callable.'''
...
...
@@ -1502,7 +1531,7 @@ def signature(obj):
# In this case we skip the first parameter of the underlying
# function (usually `self` or `cls`).
sig
=
signature
(
obj
.
__func__
)
return
sig
.
replace
(
parameters
=
tuple
(
sig
.
parameters
.
values
())[
1
:]
)
return
_signature_bound_method
(
sig
)
# Was this function wrapped by a decorator?
obj
=
unwrap
(
obj
,
stop
=
(
lambda
f
:
hasattr
(
f
,
"__signature__"
)))
...
...
@@ -1528,7 +1557,7 @@ def signature(obj):
# automatically (as for boundmethods)
wrapped_sig
=
signature
(
partialmethod
.
func
)
sig
=
_
get_partial_signature
(
wrapped_sig
,
partialmethod
,
(
None
,))
sig
=
_
signature_get_partial
(
wrapped_sig
,
partialmethod
,
(
None
,))
first_wrapped_param
=
tuple
(
wrapped_sig
.
parameters
.
values
())[
0
]
new_params
=
(
first_wrapped_param
,)
+
tuple
(
sig
.
parameters
.
values
())
...
...
@@ -1540,7 +1569,7 @@ def signature(obj):
if
isinstance
(
obj
,
functools
.
partial
):
wrapped_sig
=
signature
(
obj
.
func
)
return
_
get_partial_signature
(
wrapped_sig
,
obj
)
return
_
signature_get_partial
(
wrapped_sig
,
obj
)
sig
=
None
if
isinstance
(
obj
,
type
):
...
...
@@ -1584,7 +1613,7 @@ def signature(obj):
if
sig
is
not
None
:
# For classes and objects we skip the first parameter of their
# __call__, __new__, or __init__ methods
return
sig
.
replace
(
parameters
=
tuple
(
sig
.
parameters
.
values
())[
1
:]
)
return
_signature_bound_method
(
sig
)
if
isinstance
(
obj
,
types
.
BuiltinFunctionType
):
# Raise a nicer error message for builtins
...
...
Lib/test/test_inspect.py
View file @
215a75d8
...
...
@@ -1669,16 +1669,31 @@ class TestSignatureObject(unittest.TestCase):
def
test_signature_on_method
(
self
):
class
Test
:
def
foo
(
self
,
arg1
,
arg2
=
1
)
->
int
:
def
__init__
(
*
args
):
pass
def
m1
(
self
,
arg1
,
arg2
=
1
)
->
int
:
pass
def
m2
(
*
args
):
pass
def
__call__
(
*
,
a
):
pass
meth
=
Test
().
foo
self
.
assertEqual
(
self
.
signature
(
meth
),
self
.
assertEqual
(
self
.
signature
(
Test
().
m1
),
(((
'arg1'
,
...,
...,
"positional_or_keyword"
),
(
'arg2'
,
1
,
...,
"positional_or_keyword"
)),
int
))
self
.
assertEqual
(
self
.
signature
(
Test
().
m2
),
(((
'args'
,
...,
...,
"var_positional"
),),
...))
self
.
assertEqual
(
self
.
signature
(
Test
),
(((
'args'
,
...,
...,
"var_positional"
),),
...))
with
self
.
assertRaisesRegex
(
ValueError
,
'invalid method signature'
):
self
.
signature
(
Test
())
def
test_signature_on_classmethod
(
self
):
class
Test
:
@
classmethod
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment