summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorApostol Fet <90645107+ApostolFet@users.noreply.github.com>2024-12-08 10:05:15 (GMT)
committerGitHub <noreply@github.com>2024-12-08 10:05:15 (GMT)
commit1503fc8f88d4903e61f76a78a30bcd581b0ee0cd (patch)
treec42346ad7d4db76f908062eb2a6bd0948c605772
parent70154855cf698560dd9a5e484a649839cd68dc7c (diff)
downloadcpython-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)
-rw-r--r--Lib/inspect.py8
-rw-r--r--Lib/test/test_inspect/test_inspect.py11
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS.d/next/Library/2024-12-06-17-28-55.gh-issue-127610.ctv_NP.rst3
4 files changed, 23 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)
diff --git a/Misc/ACKS b/Misc/ACKS
index 913f7c8..0869306 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -24,6 +24,7 @@ Eitan Adler
Anton Afanasyev
Ali Afshar
Nitika Agarwal
+Maxim Ageev
Anjani Agrawal
Pablo S. Blum de Aguiar
Jim Ahlstrom
diff --git a/Misc/NEWS.d/next/Library/2024-12-06-17-28-55.gh-issue-127610.ctv_NP.rst b/Misc/NEWS.d/next/Library/2024-12-06-17-28-55.gh-issue-127610.ctv_NP.rst
new file mode 100644
index 0000000..5876902
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-12-06-17-28-55.gh-issue-127610.ctv_NP.rst
@@ -0,0 +1,3 @@
+Added validation for more than one var-positional or
+var-keyword parameters in :class:`inspect.Signature`.
+Patch by Maxim Ageev.