Skip to content

Make constraint inference in bind_self() more principled #7938

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 2 commits into from
Nov 12, 2019
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
19 changes: 2 additions & 17 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from contextlib import contextmanager

from typing import (
Dict, Set, List, cast, Tuple, TypeVar, Union, Optional, NamedTuple, Iterator, Iterable,
Sequence
Dict, Set, List, cast, Tuple, TypeVar, Union, Optional, NamedTuple, Iterator, Sequence
)
from typing_extensions import Final

Expand Down Expand Up @@ -48,7 +47,7 @@
from mypy.typeops import (
map_type_from_supertype, bind_self, erase_to_bound, make_simplified_union,
erase_def_to_union_or_bound, erase_to_union_or_bound,
true_only, false_only, function_type,
true_only, false_only, function_type, TypeVarExtractor
)
from mypy import message_registry
from mypy.subtypes import (
Expand Down Expand Up @@ -4527,20 +4526,6 @@ def detach_callable(typ: CallableType) -> CallableType:
return out


class TypeVarExtractor(TypeQuery[List[TypeVarType]]):
def __init__(self) -> None:
super().__init__(self._merge)

def _merge(self, iter: Iterable[List[TypeVarType]]) -> List[TypeVarType]:
out = []
for item in iter:
out.extend(item)
return out

def visit_type_var(self, t: TypeVarType) -> List[TypeVarType]:
return [t]


def overload_can_never_match(signature: CallableType, other: CallableType) -> bool:
"""Check if the 'other' method can never be matched due to 'signature'.

Expand Down
48 changes: 36 additions & 12 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
since these may assume that MROs are ready.
"""

from typing import cast, Optional, List, Sequence, Set
from typing import cast, Optional, List, Sequence, Set, Iterable
import sys

from mypy.types import (
TupleType, Instance, FunctionLike, Type, CallableType, TypeVarDef, Overloaded,
TypeVarType, UninhabitedType, FormalArgument, UnionType, NoneType,
AnyType, TypeOfAny, TypeType, ProperType, LiteralType, get_proper_type, get_proper_types,
copy_type, TypeAliasType
copy_type, TypeAliasType, TypeQuery
)
from mypy.nodes import (
FuncBase, FuncItem, OverloadedFuncDef, TypeInfo, TypeVar, ARG_STAR, ARG_STAR2, ARG_POS,
Expand Down Expand Up @@ -215,23 +215,29 @@ class B(A): pass
original_type = erase_to_bound(self_param_type)
original_type = get_proper_type(original_type)

ids = [x.id for x in func.variables]
typearg = get_proper_type(infer_type_arguments(ids, self_param_type,
original_type, is_supertype=True)[0])
if (is_classmethod and isinstance(typearg, UninhabitedType)
all_ids = [x.id for x in func.variables]
typeargs = infer_type_arguments(all_ids, self_param_type, original_type,
is_supertype=True)
if (is_classmethod
# TODO: why do we need the extra guards here?
and any(isinstance(get_proper_type(t), UninhabitedType) for t in typeargs)
and isinstance(original_type, (Instance, TypeVarType, TupleType))):
# In case we call a classmethod through an instance x, fallback to type(x)
typearg = get_proper_type(infer_type_arguments(ids, self_param_type,
TypeType(original_type),
is_supertype=True)[0])
typeargs = infer_type_arguments(all_ids, self_param_type, TypeType(original_type),
is_supertype=True)

ids = [tid for tid in all_ids
if any(tid == t.id for t in get_type_vars(self_param_type))]

# Technically, some constrains might be unsolvable, make them <nothing>.
to_apply = [t if t is not None else UninhabitedType() for t in typeargs]

def expand(target: Type) -> Type:
assert typearg is not None
return expand_type(target, {func.variables[0].id: typearg})
return expand_type(target, {id: to_apply[all_ids.index(id)] for id in ids})

arg_types = [expand(x) for x in func.arg_types[1:]]
ret_type = expand(func.ret_type)
variables = func.variables[1:]
variables = [v for v in func.variables if v.id not in ids]
else:
arg_types = func.arg_types[1:]
ret_type = func.ret_type
Expand Down Expand Up @@ -587,3 +593,21 @@ def coerce_to_literal(typ: Type) -> ProperType:
if len(enum_values) == 1:
return LiteralType(value=enum_values[0], fallback=typ)
return typ


def get_type_vars(tp: Type) -> List[TypeVarType]:
return tp.accept(TypeVarExtractor())


class TypeVarExtractor(TypeQuery[List[TypeVarType]]):
def __init__(self) -> None:
super().__init__(self._merge)

def _merge(self, iter: Iterable[List[TypeVarType]]) -> List[TypeVarType]:
out = []
for item in iter:
out.extend(item)
return out

def visit_type_var(self, t: TypeVarType) -> List[TypeVarType]:
return [t]
40 changes: 40 additions & 0 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,46 @@ ab: Union[A, B, C]
reveal_type(ab.x) # N: Revealed type is 'builtins.int'
[builtins fixtures/property.pyi]

[case testSelfTypeNoTypeVars]
from typing import Generic, List, Optional, TypeVar, Any

Q = TypeVar("Q")
T = TypeVar("T", bound=Super[Any])

class Super(Generic[Q]):
@classmethod
def meth(cls, arg: List[T]) -> List[T]:
pass

class Sub(Super[int]): ...

def test(x: List[Sub]) -> None:
reveal_type(Sub.meth(x)) # N: Revealed type is 'builtins.list[__main__.Sub*]'
[builtins fixtures/isinstancelist.pyi]

[case testSelfTypeNoTypeVarsRestrict]
from typing import Generic, TypeVar

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

class C(Generic[T]):
def limited(self: C[str], arg: S) -> S: ...

reveal_type(C[str]().limited(0)) # N: Revealed type is 'builtins.int*'

[case testSelfTypeMultipleTypeVars]
from typing import Generic, TypeVar, Tuple

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

class C(Generic[T]):
def magic(self: C[Tuple[S, U]]) -> Tuple[T, S, U]: ...

reveal_type(C[Tuple[int, str]]().magic()) # N: Revealed type is 'Tuple[Tuple[builtins.int, builtins.str], builtins.int, builtins.str]'

[case testSelfTypeOnUnion]
from typing import TypeVar, Union

Expand Down