Skip to content

Fix issue 7863 #7933

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 11 commits into from
Nov 15, 2019
19 changes: 11 additions & 8 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,14 +526,15 @@ def analyze_var(name: str,
if typ:
if isinstance(typ, PartialType):
return mx.chk.handle_partial_var_type(typ, mx.is_lvalue, var, mx.context)
t = get_proper_type(expand_type_by_instance(typ, itype))
if mx.is_lvalue and var.is_property and not var.is_settable_property:
# TODO allow setting attributes in subclass (although it is probably an error)
mx.msg.read_only_property(name, itype.type, mx.context)
if mx.is_lvalue and var.is_classvar:
mx.msg.cant_assign_to_classvar(name, mx.context)
t = get_proper_type(expand_type_by_instance(typ, itype))
result = t # type: Type
if var.is_initialized_in_class and isinstance(t, FunctionLike) and not t.is_type_obj():
typ = get_proper_type(typ)
if var.is_initialized_in_class and isinstance(typ, FunctionLike) and not typ.is_type_obj():
if mx.is_lvalue:
if var.is_property:
if not var.is_settable_property:
Expand All @@ -544,7 +545,7 @@ def analyze_var(name: str,
if not var.is_staticmethod:
# Class-level function objects and classmethods become bound methods:
# the former to the instance, the latter to the class.
functype = t
functype = typ
# Use meet to narrow original_type to the dispatched type.
# For example, assume
# * A.f: Callable[[A1], None] where A1 <: A (maybe A1 == A)
Expand All @@ -553,15 +554,17 @@ def analyze_var(name: str,
# In `x.f`, when checking `x` against A1 we assume x is compatible with A
# and similarly for B1 when checking agains B
dispatched_type = meet.meet_types(mx.original_type, itype)
functype = check_self_arg(functype, dispatched_type, var.is_classmethod,
signature = freshen_function_type_vars(functype)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should freshen the function type vars before the check_self_arg, to match how it is done elsewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks for the pointer.

signature = check_self_arg(signature, dispatched_type, var.is_classmethod,
mx.context, name, mx.msg)
signature = bind_self(functype, mx.self_type, var.is_classmethod)
signature = bind_self(signature, mx.self_type, var.is_classmethod)
expanded_signature = get_proper_type(expand_type_by_instance(signature, itype))
if var.is_property:
# A property cannot have an overloaded type => the cast is fine.
assert isinstance(signature, CallableType)
result = signature.ret_type
assert isinstance(expanded_signature, CallableType)
result = expanded_signature.ret_type
else:
result = signature
result = expanded_signature
else:
if not var.is_ready:
mx.not_ready_callback(var.name(), mx.context)
Expand Down
51 changes: 51 additions & 0 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -2128,3 +2128,54 @@ class B(A):
def from_config(cls) -> B:
return B()
[builtins fixtures/classmethod.pyi]

[case testAbstractGenericMethodInference]
from abc import ABC, abstractmethod
from typing import Callable, Generic, TypeVar

A = TypeVar('A')
B = TypeVar('B')
C = TypeVar('C')

class TwoTypes(Generic[A, B]):

def __call__(self) -> B: pass

class MakeTwoAbstract(ABC, Generic[A]):

def __init__(self) -> None: pass

@abstractmethod
def __call__(self, b: B) -> TwoTypes[A, B]: pass

class MakeTwoConcrete(Generic[A]):

def __call__(self, b: B) -> TwoTypes[A, B]: pass


class MakeTwoGenericSubAbstract(Generic[C], MakeTwoAbstract[C]):

def __call__(self, b: B) -> TwoTypes[C, B]: pass

class MakeTwoAppliedSubAbstract(MakeTwoAbstract[str]):

def __call__(self, b: B) -> TwoTypes[str, B]: pass

class Test():

def make_two(self,
mts: MakeTwoAbstract[A],
mte: MakeTwoConcrete[A],
mtgsa: MakeTwoGenericSubAbstract[A],
mtasa: MakeTwoAppliedSubAbstract) -> None:
reveal_type(mts(2)) # N: Revealed type is '__main__.TwoTypes[A`-1, builtins.int*]'
reveal_type(mte(2)) # N: Revealed type is '__main__.TwoTypes[A`-1, builtins.int*]'
reveal_type(mtgsa(2)) # N: Revealed type is '__main__.TwoTypes[A`-1, builtins.int*]'
reveal_type(mtasa(2)) # N: Revealed type is '__main__.TwoTypes[builtins.str, builtins.int*]'
reveal_type(MakeTwoConcrete[int]()('foo')) # N: Revealed type is '__main__.TwoTypes[builtins.int, builtins.str*]'
reveal_type(MakeTwoConcrete[str]()(2)) # N: Revealed type is '__main__.TwoTypes[builtins.str, builtins.int*]'
reveal_type(MakeTwoAppliedSubAbstract()('foo')) # N: Revealed type is '__main__.TwoTypes[builtins.str, builtins.str*]'
reveal_type(MakeTwoAppliedSubAbstract()(2)) # N: Revealed type is '__main__.TwoTypes[builtins.str, builtins.int*]'
reveal_type(MakeTwoGenericSubAbstract[str]()('foo')) # N: Revealed type is '__main__.TwoTypes[builtins.str, builtins.str*]'
reveal_type(MakeTwoGenericSubAbstract[str]()(2)) # N: Revealed type is '__main__.TwoTypes[builtins.str, builtins.int*]'

8 changes: 4 additions & 4 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,13 @@ class B(A[Q]):
a: A[int]
b: B[str]
reveal_type(a.g) # N: Revealed type is 'builtins.int'
reveal_type(a.gt) # N: Revealed type is 'builtins.int*'
reveal_type(a.gt) # N: Revealed type is 'builtins.int'
reveal_type(a.f()) # N: Revealed type is 'builtins.int'
reveal_type(a.ft()) # N: Revealed type is '__main__.A*[builtins.int]'
reveal_type(a.ft()) # N: Revealed type is '__main__.A[builtins.int]'
reveal_type(b.g) # N: Revealed type is 'builtins.int'
reveal_type(b.gt) # N: Revealed type is 'builtins.str*'
reveal_type(b.gt) # N: Revealed type is 'builtins.str'
reveal_type(b.f()) # N: Revealed type is 'builtins.int'
reveal_type(b.ft()) # N: Revealed type is '__main__.B*[builtins.str]'
reveal_type(b.ft()) # N: Revealed type is '__main__.B[builtins.str]'
[builtins fixtures/property.pyi]

[case testSelfTypeRestrictedMethod]
Expand Down
1 change: 1 addition & 0 deletions test-data/unit/lib-stub/abc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from typing import Type, Any, TypeVar

T = TypeVar('T', bound=Type[Any])

class ABC(type): pass
class ABCMeta(type):
def register(cls, tp: T) -> T: pass
abstractmethod = object()
Expand Down