diff options
author | Apostol Fet <90645107+ApostolFet@users.noreply.github.com> | 2024-12-08 10:05:15 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-08 10:05:15 (GMT) |
commit | 1503fc8f88d4903e61f76a78a30bcd581b0ee0cd (patch) | |
tree | c42346ad7d4db76f908062eb2a6bd0948c605772 /Lib | |
parent | 70154855cf698560dd9a5e484a649839cd68dc7c (diff) | |
download | cpython-1503fc8f88d4903e61f76a78a30bcd581b0ee0cd.zip cpython-1503fc8f88d4903e61f76a78a30bcd581b0ee0cd.tar.gz cpython-1503fc8f88d4903e61f76a78a30bcd581b0ee0cd.tar.bz2 |
gh-127610: Added validation for more than one var-positional and var-keyword parameters in inspect.Signature (GH-127657)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/inspect.py | 8 | ||||
-rw-r--r-- | Lib/test/test_inspect/test_inspect.py | 11 |
2 files changed, 19 insertions, 0 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index e3f74e9f..b7d8271 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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 {} ' diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index a92627a..1ecf18b 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -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) |