Skip to content

Commit d671b31

Browse files
authored
Allow using expand_type during semantic analyzis (#15201)
This is another PR to prepare for variadic type aliases.
1 parent 3a1dc4c commit d671b31

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

mypy/expandtype.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
ParamSpecType,
2222
PartialType,
2323
ProperType,
24+
TrivialSyntheticTypeTranslator,
2425
TupleType,
2526
Type,
2627
TypeAliasType,
@@ -30,7 +31,6 @@
3031
TypeVarLikeType,
3132
TypeVarTupleType,
3233
TypeVarType,
33-
TypeVisitor,
3434
UnboundType,
3535
UninhabitedType,
3636
UnionType,
@@ -46,6 +46,10 @@
4646
split_with_prefix_and_suffix,
4747
)
4848

49+
# WARNING: these functions should never (directly or indirectly) depend on
50+
# is_subtype(), meet_types(), join_types() etc.
51+
# TODO: add a static dependency test for this.
52+
4953

5054
@overload
5155
def expand_type(typ: CallableType, env: Mapping[TypeVarId, Type]) -> CallableType:
@@ -182,7 +186,7 @@ def visit_type_alias_type(self, t: TypeAliasType) -> Type:
182186
return t.copy_modified(args=[arg.accept(self) for arg in t.args])
183187

184188

185-
class ExpandTypeVisitor(TypeVisitor[Type]):
189+
class ExpandTypeVisitor(TrivialSyntheticTypeTranslator):
186190
"""Visitor that substitutes type variables with values."""
187191

188192
variables: Mapping[TypeVarId, Type] # TypeVar id -> TypeVar value

mypy/semanal_typeddict.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from mypy import errorcodes as codes, message_registry
88
from mypy.errorcodes import ErrorCode
9+
from mypy.expandtype import expand_type
910
from mypy.exprtotype import TypeTranslationError, expr_to_unanalyzed_type
1011
from mypy.messages import MessageBuilder
1112
from mypy.nodes import (
@@ -45,7 +46,6 @@
4546
TypedDictType,
4647
TypeOfAny,
4748
TypeVarLikeType,
48-
replace_alias_tvars,
4949
)
5050

5151
TPDICT_CLASS_ERROR: Final = (
@@ -243,21 +243,18 @@ def map_items_to_base(
243243
) -> dict[str, Type]:
244244
"""Map item types to how they would look in their base with type arguments applied.
245245
246-
We would normally use expand_type() for such task, but we can't use it during
247-
semantic analysis, because it can (indirectly) call is_subtype() etc., and it
248-
will crash on placeholder types. So we hijack replace_alias_tvars() that was initially
249-
intended to deal with eager expansion of generic type aliases during semantic analysis.
246+
Note it is safe to use expand_type() during semantic analysis, because it should never
247+
(indirectly) call is_subtype().
250248
"""
251249
mapped_items = {}
252250
for key in valid_items:
253251
type_in_base = valid_items[key]
254252
if not tvars:
255253
mapped_items[key] = type_in_base
256254
continue
257-
mapped_type = replace_alias_tvars(
258-
type_in_base, tvars, base_args, type_in_base.line, type_in_base.column
255+
mapped_items[key] = expand_type(
256+
type_in_base, {t.id: a for (t, a) in zip(tvars, base_args)}
259257
)
260-
mapped_items[key] = mapped_type
261258
return mapped_items
262259

263260
def analyze_typeddict_classdef_fields(

0 commit comments

Comments
 (0)