Skip to content

Commit

Permalink
Allow using expand_type during semantic analyzis (#15201)
Browse files Browse the repository at this point in the history
This is another PR to prepare for variadic type aliases.
  • Loading branch information
ilevkivskyi authored May 7, 2023
1 parent 3a1dc4c commit d671b31
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
8 changes: 6 additions & 2 deletions mypy/expandtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ParamSpecType,
PartialType,
ProperType,
TrivialSyntheticTypeTranslator,
TupleType,
Type,
TypeAliasType,
Expand All @@ -30,7 +31,6 @@
TypeVarLikeType,
TypeVarTupleType,
TypeVarType,
TypeVisitor,
UnboundType,
UninhabitedType,
UnionType,
Expand All @@ -46,6 +46,10 @@
split_with_prefix_and_suffix,
)

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


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


class ExpandTypeVisitor(TypeVisitor[Type]):
class ExpandTypeVisitor(TrivialSyntheticTypeTranslator):
"""Visitor that substitutes type variables with values."""

variables: Mapping[TypeVarId, Type] # TypeVar id -> TypeVar value
Expand Down
13 changes: 5 additions & 8 deletions mypy/semanal_typeddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from mypy import errorcodes as codes, message_registry
from mypy.errorcodes import ErrorCode
from mypy.expandtype import expand_type
from mypy.exprtotype import TypeTranslationError, expr_to_unanalyzed_type
from mypy.messages import MessageBuilder
from mypy.nodes import (
Expand Down Expand Up @@ -45,7 +46,6 @@
TypedDictType,
TypeOfAny,
TypeVarLikeType,
replace_alias_tvars,
)

TPDICT_CLASS_ERROR: Final = (
Expand Down Expand Up @@ -243,21 +243,18 @@ def map_items_to_base(
) -> dict[str, Type]:
"""Map item types to how they would look in their base with type arguments applied.
We would normally use expand_type() for such task, but we can't use it during
semantic analysis, because it can (indirectly) call is_subtype() etc., and it
will crash on placeholder types. So we hijack replace_alias_tvars() that was initially
intended to deal with eager expansion of generic type aliases during semantic analysis.
Note it is safe to use expand_type() during semantic analysis, because it should never
(indirectly) call is_subtype().
"""
mapped_items = {}
for key in valid_items:
type_in_base = valid_items[key]
if not tvars:
mapped_items[key] = type_in_base
continue
mapped_type = replace_alias_tvars(
type_in_base, tvars, base_args, type_in_base.line, type_in_base.column
mapped_items[key] = expand_type(
type_in_base, {t.id: a for (t, a) in zip(tvars, base_args)}
)
mapped_items[key] = mapped_type
return mapped_items

def analyze_typeddict_classdef_fields(
Expand Down

0 comments on commit d671b31

Please sign in to comment.