https://github.com/python/cpython/commit/1503fc8f88d4903e61f76a78a30bcd581b0ee0cd
commit: 1503fc8f88d4903e61f76a78a30bcd581b0ee0cd
branch: main
author: Apostol Fet <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-12-08T12:05:15+02:00
summary:

gh-127610: Added validation for more than one var-positional and var-keyword 
parameters in inspect.Signature (GH-127657)

files:
A Misc/NEWS.d/next/Library/2024-12-06-17-28-55.gh-issue-127610.ctv_NP.rst
M Lib/inspect.py
M Lib/test/test_inspect/test_inspect.py
M Misc/ACKS

diff --git a/Lib/inspect.py b/Lib/inspect.py
index e3f74e9f047eaf..b7d8271f8a471f 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2943,11 +2943,19 @@ def __init__(self, parameters=None, *, 
return_annotation=_empty,
                 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 a92627a4d60f68..1ecf18bf49fa7e 100644
--- a/Lib/test/test_inspect/test_inspect.py
+++ b/Lib/test/test_inspect/test_inspect.py
@@ -2992,6 +2992,17 @@ def test2(pod=42, /):
         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 913f7c8ecf5f1e..086930666822ad 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 00000000000000..58769029d79977
--- /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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to