Skip to content

Commit 25ea889

Browse files
committed
Document and test .getter rejection
1 parent e0092bf commit 25ea889

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

mypy/semanal.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,12 @@ def _is_valid_property_decorator(
15751575
if not isinstance(deco.expr, NameExpr) or deco.expr.name != property_name:
15761576
return False
15771577
if deco.name not in {"setter", "deleter"}:
1578+
# This intentionally excludes getter. While `@prop.getter` is valid at
1579+
# runtime, that would mean replacing the already processed getter type.
1580+
# Such usage is almost definitely a mistake (except for overrides in
1581+
# subclasses but we don't support them anyway) and might be a typo
1582+
# (only one letter away from `setter`), it's likely almost never used,
1583+
# so supporting it properly won't pay off.
15781584
return False
15791585
return True
15801586

test-data/unit/check-classes.test

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,6 @@ class A:
16241624
[builtins fixtures/property.pyi]
16251625

16261626
[case testPropertyNameIsChecked]
1627-
import typing
16281627
class A:
16291628
@property
16301629
def f(self) -> str: ...
@@ -1651,14 +1650,21 @@ class C:
16511650
[builtins fixtures/property.pyi]
16521651

16531652
[case testPropertyAttributeIsChecked]
1654-
import typing
16551653
class A:
16561654
@property
16571655
def f(self) -> str: ...
16581656
@f.unknown # E: Only supported top decorators are @f.setter and @f.deleter
16591657
def f(self, val: str) -> None: ...
16601658
[builtins fixtures/property.pyi]
16611659

1660+
[case testPropertyGetterDecoratorIsRejected]
1661+
class A:
1662+
@property
1663+
def f(self) -> str: ...
1664+
@f.getter # E: Only supported top decorators are @f.setter and @f.deleter
1665+
def f(self, val: str) -> None: ...
1666+
[builtins fixtures/property.pyi]
1667+
16621668
[case testDynamicallyTypedProperty]
16631669
import typing
16641670
class A:

0 commit comments

Comments
 (0)