https://github.com/python/cpython/commit/c1d7147c820545bb0a97a072fdba82154fd97ab6
commit: c1d7147c820545bb0a97a072fdba82154fd97ab6
branch: main
author: neonene <[email protected]>
committer: erlend-aasland <[email protected]>
date: 2024-04-17T22:43:29+02:00
summary:

gh-117613: Argument Clinic: disallow defining class parameter at module level 
(#117950)

files:
M Lib/test/test_clinic.py
M Tools/clinic/libclinic/dsl_parser.py

diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py
index 43b629f59f0346..d9e4ce280c68a1 100644
--- a/Lib/test/test_clinic.py
+++ b/Lib/test/test_clinic.py
@@ -2518,6 +2518,15 @@ class m.C "PyObject *" ""
         p = function.parameters['cls']
         self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY)
 
+    def test_disallow_defining_class_at_module_level(self):
+        err = "A 'defining_class' parameter cannot be defined at module level."
+        block = """
+            module m
+            m.func
+                cls: defining_class
+        """
+        self.expect_failure(block, err, lineno=2)
+
 
 class ClinicExternalTest(TestCase):
     maxDiff = None
diff --git a/Tools/clinic/libclinic/dsl_parser.py 
b/Tools/clinic/libclinic/dsl_parser.py
index 56c6dca3db3d1d..cb18374cf07e3c 100644
--- a/Tools/clinic/libclinic/dsl_parser.py
+++ b/Tools/clinic/libclinic/dsl_parser.py
@@ -1102,6 +1102,8 @@ def bad_node(self, node: ast.AST) -> None:
                     fail("A 'defining_class' parameter cannot have a default 
value.")
                 if self.group:
                     fail("A 'defining_class' parameter cannot be in an 
optional group.")
+                if self.function.cls is None:
+                    fail("A 'defining_class' parameter cannot be defined at 
module level.")
                 kind = inspect.Parameter.POSITIONAL_ONLY
             else:
                 fail("A 'defining_class' parameter, if specified, must either "

_______________________________________________
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