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
07a9e452
Kaydet (Commit)
07a9e452
authored
Ock 29, 2014
tarafından
Yury Selivanov
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
inspect.Signature: ensure that non-default params don't follow default ones #20427
üst
76c6c593
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
2 deletions
+32
-2
inspect.py
Lib/inspect.py
+20
-1
test_inspect.py
Lib/test/test_inspect.py
+12
-1
No files found.
Lib/inspect.py
Dosyayı görüntüle @
07a9e452
...
@@ -1924,6 +1924,7 @@ class Signature:
...
@@ -1924,6 +1924,7 @@ class Signature:
if
__validate_parameters__
:
if
__validate_parameters__
:
params
=
OrderedDict
()
params
=
OrderedDict
()
top_kind
=
_POSITIONAL_ONLY
top_kind
=
_POSITIONAL_ONLY
kind_defaults
=
False
for
idx
,
param
in
enumerate
(
parameters
):
for
idx
,
param
in
enumerate
(
parameters
):
kind
=
param
.
kind
kind
=
param
.
kind
...
@@ -1933,9 +1934,27 @@ class Signature:
...
@@ -1933,9 +1934,27 @@ class Signature:
msg
=
'wrong parameter order: {} before {}'
msg
=
'wrong parameter order: {} before {}'
msg
=
msg
.
format
(
top_kind
,
kind
)
msg
=
msg
.
format
(
top_kind
,
kind
)
raise
ValueError
(
msg
)
raise
ValueError
(
msg
)
else
:
elif
kind
>
top_kind
:
kind_defaults
=
False
top_kind
=
kind
top_kind
=
kind
if
(
kind
in
(
_POSITIONAL_ONLY
,
_POSITIONAL_OR_KEYWORD
)
and
not
param
.
_partial_kwarg
):
# If we have a positional-only or positional-or-keyword
# parameter, that does not have its default value set
# by 'functools.partial' or other "partial" signature:
if
param
.
default
is
_empty
:
if
kind_defaults
:
# No default for this parameter, but the
# previous parameter of the same kind had
# a default
msg
=
'non-default argument follows default '
\
'argument'
raise
ValueError
(
msg
)
else
:
# There is a default for this parameter.
kind_defaults
=
True
if
name
in
params
:
if
name
in
params
:
msg
=
'duplicate parameter name: {!r}'
.
format
(
name
)
msg
=
'duplicate parameter name: {!r}'
.
format
(
name
)
raise
ValueError
(
msg
)
raise
ValueError
(
msg
)
...
...
Lib/test/test_inspect.py
Dosyayı görüntüle @
07a9e452
...
@@ -1522,11 +1522,13 @@ class TestSignatureObject(unittest.TestCase):
...
@@ -1522,11 +1522,13 @@ class TestSignatureObject(unittest.TestCase):
self
.
assertEqual
(
str
(
S
()),
'()'
)
self
.
assertEqual
(
str
(
S
()),
'()'
)
def
test
(
po
,
pk
,
*
args
,
ko
,
**
kwargs
):
def
test
(
po
,
pk
,
pod
=
42
,
pkd
=
100
,
*
args
,
ko
,
**
kwargs
):
pass
pass
sig
=
inspect
.
signature
(
test
)
sig
=
inspect
.
signature
(
test
)
po
=
sig
.
parameters
[
'po'
]
.
replace
(
kind
=
P
.
POSITIONAL_ONLY
)
po
=
sig
.
parameters
[
'po'
]
.
replace
(
kind
=
P
.
POSITIONAL_ONLY
)
pod
=
sig
.
parameters
[
'pod'
]
.
replace
(
kind
=
P
.
POSITIONAL_ONLY
)
pk
=
sig
.
parameters
[
'pk'
]
pk
=
sig
.
parameters
[
'pk'
]
pkd
=
sig
.
parameters
[
'pkd'
]
args
=
sig
.
parameters
[
'args'
]
args
=
sig
.
parameters
[
'args'
]
ko
=
sig
.
parameters
[
'ko'
]
ko
=
sig
.
parameters
[
'ko'
]
kwargs
=
sig
.
parameters
[
'kwargs'
]
kwargs
=
sig
.
parameters
[
'kwargs'
]
...
@@ -1549,6 +1551,15 @@ class TestSignatureObject(unittest.TestCase):
...
@@ -1549,6 +1551,15 @@ class TestSignatureObject(unittest.TestCase):
with
self
.
assertRaisesRegex
(
ValueError
,
'duplicate parameter name'
):
with
self
.
assertRaisesRegex
(
ValueError
,
'duplicate parameter name'
):
S
((
po
,
pk
,
args
,
kwargs2
,
ko
))
S
((
po
,
pk
,
args
,
kwargs2
,
ko
))
with
self
.
assertRaisesRegex
(
ValueError
,
'follows default argument'
):
S
((
pod
,
po
))
with
self
.
assertRaisesRegex
(
ValueError
,
'follows default argument'
):
S
((
po
,
pkd
,
pk
))
with
self
.
assertRaisesRegex
(
ValueError
,
'follows default argument'
):
S
((
pkd
,
pk
))
def
test_signature_immutability
(
self
):
def
test_signature_immutability
(
self
):
def
test
(
a
):
def
test
(
a
):
pass
pass
...
...
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