Skip to content

Selftype for properties #2404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def check_method_type(functype: FunctionLike, itype: Instance, is_classmethod: b
# we passed to typ.fallback in analyze_member_access. See #1432.
if isinstance(selfarg, TupleType):
selfarg = selfarg.fallback
if not subtypes.is_equivalent(selfarg, itype):
if not subtypes.is_subtype(selfarg, itype):
msg.invalid_method_type(item, context)
else:
# Check that cls argument has type 'Any' or valid class type.
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,21 @@ class E:
def __new__(cls) -> E:
reveal_type(cls) # E: Revealed type is 'def () -> __main__.E'
return cls()

[case testSelfTypeProperty]
from typing import TypeVar

T = TypeVar('T', bound='A')

class A:
@property
def member(self: T) -> T:
pass

class B(A):
pass

reveal_type(A().member) # E: Revealed type is '__main__.A*'
reveal_type(B().member) # E: Revealed type is '__main__.B*'

[builtins fixtures/property.pyi]