Skip to content
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
5 changes: 3 additions & 2 deletions mypy/checkmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ def analyze_class_attribute_access(itype: Instance,
is_classmethod = ((is_decorated and cast(Decorator, node.node).func.is_class)
or (isinstance(node.node, FuncBase) and node.node.is_class))
result = add_class_tvars(get_proper_type(t), itype, isuper, is_classmethod,
mx.builtin_type, mx.original_type)
mx.builtin_type, mx.self_type)
if not mx.is_lvalue:
result = analyze_descriptor_access(mx.original_type, result, mx.builtin_type,
mx.msg, mx.context, chk=mx.chk)
Expand Down Expand Up @@ -761,7 +761,8 @@ class B(A[str]): pass

B.foo()

original_type is the value of the type B in the expression B.foo()
original_type is the value of the type B in the expression B.foo() or the corresponding
component in case if a union (this is used to bind the self-types).
"""
# TODO: verify consistency between Q and T
info = itype.type # type: TypeInfo
Expand Down
48 changes: 48 additions & 0 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -569,3 +569,51 @@ class Base:
@classmethod
def make(cls: Type[T], num: int) -> Tuple[T, ...]: ...
[builtins fixtures/classmethod.pyi]

[case testSelfTypeClassMethodOnUnion]
from typing import Type, Union, TypeVar

T = TypeVar('T')

class A:
@classmethod
def meth(cls: Type[T]) -> T: ...
class B(A): ...
class C(A): ...

t: Type[Union[B, C]]
reveal_type(t.meth) # N: Revealed type is 'Union[def () -> __main__.B*, def () -> __main__.C*]'
x = t.meth()
reveal_type(x) # N: Revealed type is 'Union[__main__.B*, __main__.C*]'
[builtins fixtures/classmethod.pyi]

[case testSelfTypeClassMethodOnUnionGeneric]
from typing import Type, Union, TypeVar, Generic

T = TypeVar('T')
S = TypeVar('S')

class A(Generic[T]):
@classmethod
def meth(cls: Type[S]) -> S: ...

t: Type[Union[A[int], A[str]]]
x = t.meth()
reveal_type(x) # N: Revealed type is 'Union[__main__.A*[builtins.int], __main__.A*[builtins.str]]'
[builtins fixtures/classmethod.pyi]

[case testSelfTypeClassMethodOnUnionList]
from typing import Type, Union, TypeVar, List

T = TypeVar('T')

class A:
@classmethod
def meth(cls: Type[T]) -> List[T]: ...
class B(A): ...
class C(A): ...

t: Type[Union[B, C]]
x = t.meth()[0]
reveal_type(x) # N: Revealed type is 'Union[__main__.B*, __main__.C*]'
[builtins fixtures/isinstancelist.pyi]