Skip to content

Commit 6a69b80

Browse files
gh-112205: Require @Getter and @Setter to be methods (#113278)
Co-authored-by: Alex Waygood <[email protected]>
1 parent e51b400 commit 6a69b80

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Lib/test/test_clinic.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,17 @@ class Foo "" ""
22492249
expected_error = "Cannot apply both @getter and @setter to the same function!"
22502250
self.expect_failure(block, expected_error, lineno=3)
22512251

2252+
def test_getset_no_class(self):
2253+
for annotation in "@getter", "@setter":
2254+
with self.subTest(annotation=annotation):
2255+
block = f"""
2256+
module m
2257+
{annotation}
2258+
m.func
2259+
"""
2260+
expected_error = "@getter and @setter must be methods"
2261+
self.expect_failure(block, expected_error, lineno=2)
2262+
22522263
def test_duplicate_coexist(self):
22532264
err = "Called @coexist twice"
22542265
block = """

Tools/clinic/clinic.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5614,6 +5614,10 @@ def state_modulename_name(self, line: str) -> None:
56145614
function_name = fields.pop()
56155615
module, cls = self.clinic._module_and_class(fields)
56165616

5617+
if self.kind in {GETTER, SETTER}:
5618+
if not cls:
5619+
fail("@getter and @setter must be methods")
5620+
56175621
self.update_function_kind(full_name)
56185622
if self.kind is METHOD_INIT and not return_converter:
56195623
return_converter = init_return_converter()

0 commit comments

Comments
 (0)