diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index d3dbde88dd82a9..6c6bd4e75a0b02 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -2249,6 +2249,17 @@ class Foo "" "" expected_error = "Cannot apply both @getter and @setter to the same function!" self.expect_failure(block, expected_error, lineno=3) + def test_getset_no_class(self): + for annotation in "@getter", "@setter": + with self.subTest(annotation=annotation): + block = f""" + module m + {annotation} + m.func + """ + expected_error = "@getter and @setter must be methods" + self.expect_failure(block, expected_error, lineno=2) + def test_duplicate_coexist(self): err = "Called @coexist twice" block = """ diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index a9bf110291eadd..87feef1b82ca39 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -5614,6 +5614,10 @@ def state_modulename_name(self, line: str) -> None: function_name = fields.pop() module, cls = self.clinic._module_and_class(fields) + if self.kind in {GETTER, SETTER}: + if not cls: + fail("@getter and @setter must be methods") + self.update_function_kind(full_name) if self.kind is METHOD_INIT and not return_converter: return_converter = init_return_converter()