Skip to content

Commit 0f17aff

Browse files
authored
refactor: Use @overload for expand_type (#13641)
Removing a bunch of `TODO` items.
1 parent c8e5278 commit 0f17aff

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

mypy/checker.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6762,7 +6762,6 @@ def overload_can_never_match(signature: CallableType, other: CallableType) -> bo
67626762
exp_signature = expand_type(
67636763
signature, {tvar.id: erase_def_to_union_or_bound(tvar) for tvar in signature.variables}
67646764
)
6765-
assert isinstance(exp_signature, ProperType)
67666765
assert isinstance(exp_signature, CallableType)
67676766
return is_callable_compatible(
67686767
exp_signature, other, is_compat=is_more_precise, ignore_return=True

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,7 @@ def analyze_type_type_callee(self, item: ProperType, context: Context) -> Type:
15061506
res = type_object_type(item.type, self.named_type)
15071507
if isinstance(res, CallableType):
15081508
res = res.copy_modified(from_type_type=True)
1509-
expanded = get_proper_type(expand_type_by_instance(res, item))
1509+
expanded = expand_type_by_instance(res, item)
15101510
if isinstance(expanded, CallableType):
15111511
# Callee of the form Type[...] should never be generic, only
15121512
# proper class objects can be.

mypy/checkmember.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ def analyze_var(
731731
signature, dispatched_type, var.is_classmethod, mx.context, name, mx.msg
732732
)
733733
signature = bind_self(signature, mx.self_type, var.is_classmethod)
734-
expanded_signature = get_proper_type(expand_type_by_instance(signature, itype))
734+
expanded_signature = expand_type_by_instance(signature, itype)
735735
freeze_type_vars(expanded_signature)
736736
if var.is_property:
737737
# A property cannot have an overloaded type => the cast is fine.
@@ -1111,7 +1111,7 @@ class B(A[str]): pass
11111111
]
11121112
)
11131113
if isuper is not None:
1114-
t = cast(ProperType, expand_type_by_instance(t, isuper))
1114+
t = expand_type_by_instance(t, isuper)
11151115
return t
11161116

11171117

mypy/expandtype.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Iterable, Mapping, Sequence, TypeVar, cast
3+
from typing import Iterable, Mapping, Sequence, TypeVar, cast, overload
44

55
from mypy.types import (
66
AnyType,
@@ -37,18 +37,36 @@
3737
from mypy.typevartuples import split_with_instance, split_with_prefix_and_suffix
3838

3939

40+
@overload
41+
def expand_type(typ: ProperType, env: Mapping[TypeVarId, Type]) -> ProperType:
42+
...
43+
44+
45+
@overload
46+
def expand_type(typ: Type, env: Mapping[TypeVarId, Type]) -> Type:
47+
...
48+
49+
4050
def expand_type(typ: Type, env: Mapping[TypeVarId, Type]) -> Type:
4151
"""Substitute any type variable references in a type given by a type
4252
environment.
4353
"""
44-
# TODO: use an overloaded signature? (ProperType stays proper after expansion.)
4554
return typ.accept(ExpandTypeVisitor(env))
4655

4756

57+
@overload
58+
def expand_type_by_instance(typ: ProperType, instance: Instance) -> ProperType:
59+
...
60+
61+
62+
@overload
63+
def expand_type_by_instance(typ: Type, instance: Instance) -> Type:
64+
...
65+
66+
4867
def expand_type_by_instance(typ: Type, instance: Instance) -> Type:
4968
"""Substitute type variables in type using values from an Instance.
5069
Type variables are considered to be bound by the class declaration."""
51-
# TODO: use an overloaded signature? (ProperType stays proper after expansion.)
5270
if not instance.args:
5371
return typ
5472
else:
@@ -87,7 +105,6 @@ def freshen_function_type_vars(callee: F) -> F:
87105
tvs = []
88106
tvmap: dict[TypeVarId, Type] = {}
89107
for v in callee.variables:
90-
# TODO(PEP612): fix for ParamSpecType
91108
if isinstance(v, TypeVarType):
92109
tv: TypeVarLikeType = TypeVarType.new_unification_variable(v)
93110
elif isinstance(v, TypeVarTupleType):

mypy/maptype.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,7 @@
33
import mypy.typeops
44
from mypy.expandtype import expand_type
55
from mypy.nodes import TypeInfo
6-
from mypy.types import (
7-
AnyType,
8-
Instance,
9-
ProperType,
10-
TupleType,
11-
Type,
12-
TypeOfAny,
13-
TypeVarId,
14-
get_proper_type,
15-
has_type_vars,
16-
)
6+
from mypy.types import AnyType, Instance, TupleType, Type, TypeOfAny, TypeVarId, has_type_vars
177

188

199
def map_instance_to_supertype(instance: Instance, superclass: TypeInfo) -> Instance:
@@ -37,7 +27,7 @@ def map_instance_to_supertype(instance: Instance, superclass: TypeInfo) -> Insta
3727
# Unfortunately we can't support this for generic recursive tuples.
3828
# If we skip this special casing we will fall back to tuple[Any, ...].
3929
env = instance_to_type_environment(instance)
40-
tuple_type = get_proper_type(expand_type(instance.type.tuple_type, env))
30+
tuple_type = expand_type(instance.type.tuple_type, env)
4131
if isinstance(tuple_type, TupleType):
4232
return mypy.typeops.tuple_fallback(tuple_type)
4333

@@ -101,7 +91,6 @@ def map_instance_to_direct_supertypes(instance: Instance, supertype: TypeInfo) -
10191
if b.type == supertype:
10292
env = instance_to_type_environment(instance)
10393
t = expand_type(b, env)
104-
assert isinstance(t, ProperType)
10594
assert isinstance(t, Instance)
10695
result.append(t)
10796

0 commit comments

Comments
 (0)