Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
62560fb1
Kaydet (Commit)
62560fb1
authored
Ock 28, 2014
tarafından
Yury Selivanov
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
inspect.signature: Handle bound methods with '(*args)' signature correctly #20401
üst
d65bc70d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
9 deletions
+53
-9
inspect.py
Lib/inspect.py
+34
-5
test_inspect.py
Lib/test/test_inspect.py
+19
-4
No files found.
Lib/inspect.py
Dosyayı görüntüle @
62560fb1
...
...
@@ -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
Dosyayı görüntüle @
62560fb1
...
...
@@ -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