mirror of
https://github.com/python/cpython.git
synced 2025-08-31 22:18:28 +00:00
gh-127610: Added validation for more than one var-positional and var-keyword parameters in inspect.Signature (GH-127657)
This commit is contained in:
parent
70154855cf
commit
1503fc8f88
4 changed files with 23 additions and 0 deletions
|
@ -2943,11 +2943,19 @@ class Signature:
|
|||
params = OrderedDict()
|
||||
top_kind = _POSITIONAL_ONLY
|
||||
seen_default = False
|
||||
seen_var_parameters = set()
|
||||
|
||||
for param in parameters:
|
||||
kind = param.kind
|
||||
name = param.name
|
||||
|
||||
if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
|
||||
if kind in seen_var_parameters:
|
||||
msg = f'more than one {kind.description} parameter'
|
||||
raise ValueError(msg)
|
||||
|
||||
seen_var_parameters.add(kind)
|
||||
|
||||
if kind < top_kind:
|
||||
msg = (
|
||||
'wrong parameter order: {} parameter before {} '
|
||||
|
|
|
@ -2992,6 +2992,17 @@ class TestSignatureObject(unittest.TestCase):
|
|||
with self.assertRaisesRegex(ValueError, 'follows default argument'):
|
||||
S((pkd, pk))
|
||||
|
||||
second_args = args.replace(name="second_args")
|
||||
with self.assertRaisesRegex(ValueError, 'more than one variadic positional parameter'):
|
||||
S((args, second_args))
|
||||
|
||||
with self.assertRaisesRegex(ValueError, 'more than one variadic positional parameter'):
|
||||
S((args, ko, second_args))
|
||||
|
||||
second_kwargs = kwargs.replace(name="second_kwargs")
|
||||
with self.assertRaisesRegex(ValueError, 'more than one variadic keyword parameter'):
|
||||
S((kwargs, second_kwargs))
|
||||
|
||||
def test_signature_object_pickle(self):
|
||||
def foo(a, b, *, c:1={}, **kw) -> {42:'ham'}: pass
|
||||
foo_partial = functools.partial(foo, a=1)
|
||||
|
|
|
@ -24,6 +24,7 @@ Eitan Adler
|
|||
Anton Afanasyev
|
||||
Ali Afshar
|
||||
Nitika Agarwal
|
||||
Maxim Ageev
|
||||
Anjani Agrawal
|
||||
Pablo S. Blum de Aguiar
|
||||
Jim Ahlstrom
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
Added validation for more than one var-positional or
|
||||
var-keyword parameters in :class:`inspect.Signature`.
|
||||
Patch by Maxim Ageev.
|
Loading…
Add table
Add a link
Reference in a new issue