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
Merged
Show file tree
Hide file tree
Changes from 8 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
17 changes: 10 additions & 7 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 @@ -555,13 +556,15 @@ def analyze_var(name: str,
dispatched_type = meet.meet_types(mx.original_type, itype)
functype = check_self_arg(functype, dispatched_type, var.is_classmethod,
mx.context, name, mx.msg)
signature = bind_self(functype, mx.self_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 = 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
47 changes: 47 additions & 0 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -2128,3 +2128,50 @@ 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

# TODO - This should extend ABC but test errors that ABC not in abc on import
Copy link
Collaborator

Choose a reason for hiding this comment

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

Comment stale

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed thanks.

class MakeTwoAbstract(ABC, Generic[A]):

@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.__call__) # N: Revealed type is 'def [B] (b: B`33) -> __main__.TwoTypes[A`-1, B`33]'
reveal_type(mts(2)) # N: Revealed type is '__main__.TwoTypes[A`-1, builtins.int*]'
reveal_type(mte.__call__) # N: Revealed type is 'def [B] (b: B`36) -> __main__.TwoTypes[A`-1, B`36]'
reveal_type(mte(2)) # N: Revealed type is '__main__.TwoTypes[A`-1, builtins.int*]'
reveal_type(mtgsa.__call__) # N: Revealed type is 'def [B] (b: B`39) -> __main__.TwoTypes[A`-1, B`39]'
reveal_type(mtgsa(2)) # N: Revealed type is '__main__.TwoTypes[A`-1, builtins.int*]'
reveal_type(mtasa.__call__) # N: Revealed type is 'def [B] (b: B`42) -> __main__.TwoTypes[builtins.str, B`42]'
reveal_type(mtasa(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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy