Skip to content

Commit

Permalink
Fix abstract and non-abstract variant error for prop.deleter (#15395)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja authored Jun 8, 2023
1 parent 27593bc commit 4baf672
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,8 @@ def analyze_property_with_multi_part_definition(self, defn: OverloadedFuncDef) -
first_item.var.is_settable_property = True
# Get abstractness from the original definition.
item.func.abstract_status = first_item.func.abstract_status
if node.name == "deleter":
item.func.abstract_status = first_item.func.abstract_status
else:
self.fail(
f"Only supported top decorator is @{first_item.func.name}.setter", item
Expand Down
41 changes: 39 additions & 2 deletions test-data/unit/check-abstract.test
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,44 @@ class A(metaclass=ABCMeta):
def x(self) -> int: pass
@x.setter
def x(self, x: int) -> None: pass
[out]

[case testReadWriteDeleteAbstractProperty]
from abc import ABC, abstractmethod
class Abstract(ABC):
@property
@abstractmethod
def prop(self) -> str: ...

@prop.setter
@abstractmethod
def prop(self, code: str) -> None: ...

@prop.deleter
@abstractmethod
def prop(self) -> None: ...

class Good(Abstract):
@property
def prop(self) -> str: ...
@prop.setter
def prop(self, code: str) -> None: ...
@prop.deleter
def prop(self) -> None: ...

class Bad1(Abstract):
@property # E: Read-only property cannot override read-write property
def prop(self) -> str: ...

class ThisShouldProbablyError(Abstract):
@property
def prop(self) -> str: ...
@prop.setter
def prop(self, code: str) -> None: ...

a = Good()
reveal_type(a.prop) # N: Revealed type is "builtins.str"
a.prop = 123 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[builtins fixtures/property.pyi]

[case testInstantiateClassWithReadOnlyAbstractProperty]
from abc import abstractproperty, ABCMeta
Expand Down Expand Up @@ -767,7 +804,7 @@ b = B()
b.x() # E: "int" not callable
[builtins fixtures/property.pyi]

[case testImplementReradWriteAbstractPropertyViaProperty]
[case testImplementReadWriteAbstractPropertyViaProperty]
from abc import abstractproperty, ABCMeta
class A(metaclass=ABCMeta):
@abstractproperty
Expand Down

0 comments on commit 4baf672

Please sign in to comment.