Skip to content

Commit e4fd655

Browse files
elazarggvanrossum
authored andcommitted
require subtyping instead of equivalence in selftype of properties (#2404)
Fix #2402 and add test.
1 parent 9579d15 commit e4fd655

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

mypy/checkmember.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def check_method_type(functype: FunctionLike, itype: Instance, is_classmethod: b
323323
# we passed to typ.fallback in analyze_member_access. See #1432.
324324
if isinstance(selfarg, TupleType):
325325
selfarg = selfarg.fallback
326-
if not subtypes.is_equivalent(selfarg, itype):
326+
if not subtypes.is_subtype(selfarg, itype):
327327
msg.invalid_method_type(item, context)
328328
else:
329329
# Check that cls argument has type 'Any' or valid class type.

test-data/unit/check-selftype.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,21 @@ class E:
333333
def __new__(cls) -> E:
334334
reveal_type(cls) # E: Revealed type is 'def () -> __main__.E'
335335
return cls()
336+
337+
[case testSelfTypeProperty]
338+
from typing import TypeVar
339+
340+
T = TypeVar('T', bound='A')
341+
342+
class A:
343+
@property
344+
def member(self: T) -> T:
345+
pass
346+
347+
class B(A):
348+
pass
349+
350+
reveal_type(A().member) # E: Revealed type is '__main__.A*'
351+
reveal_type(B().member) # E: Revealed type is '__main__.B*'
352+
353+
[builtins fixtures/property.pyi]

0 commit comments

Comments
 (0)