Skip to content

Commit baee3ef

Browse files
committed
Handle placeholder types better
1 parent 58003db commit baee3ef

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

mypy/expandtype.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
ANY_STRATEGY,
1010
AnyType,
1111
BoolTypeQuery,
12+
CallableArgument,
1213
CallableType,
1314
DeletedType,
15+
EllipsisType,
1416
ErasedType,
1517
FunctionLike,
1618
Instance,
@@ -20,16 +22,20 @@
2022
Parameters,
2123
ParamSpecType,
2224
PartialType,
25+
PlaceholderType,
2326
ProperType,
27+
RawExpressionType,
28+
StarType,
29+
SyntheticTypeVisitor,
2430
TupleType,
2531
Type,
2632
TypeAliasType,
2733
TypedDictType,
34+
TypeList,
2835
TypeType,
2936
TypeVarId,
3037
TypeVarTupleType,
3138
TypeVarType,
32-
TypeVisitor,
3339
UnboundType,
3440
UninhabitedType,
3541
UnionType,
@@ -205,7 +211,7 @@ def visit_type_alias_type(self, t: TypeAliasType) -> Type:
205211
return t.copy_modified(args=[arg.accept(self) for arg in t.args])
206212

207213

208-
class ExpandTypeVisitor(TypeVisitor[Type]):
214+
class ExpandTypeVisitor(SyntheticTypeVisitor[Type]):
209215
"""Visitor that substitutes type variables with values."""
210216

211217
variables: Mapping[TypeVarId, Type] # TypeVar id -> TypeVar value
@@ -523,6 +529,24 @@ def visit_type_alias_type(self, t: TypeAliasType) -> Type:
523529
def expand_types(self, types: Iterable[Type]) -> list[Type]:
524530
return [t.accept(self) for t in types]
525531

532+
def visit_star_type(self, t: StarType) -> Type:
533+
return t
534+
535+
def visit_type_list(self, t: TypeList) -> Type:
536+
return t
537+
538+
def visit_callable_argument(self, t: CallableArgument) -> Type:
539+
return t
540+
541+
def visit_ellipsis_type(self, t: EllipsisType) -> Type:
542+
return t
543+
544+
def visit_raw_expression_type(self, t: RawExpressionType) -> Type:
545+
return t
546+
547+
def visit_placeholder_type(self, t: PlaceholderType) -> Type:
548+
return t
549+
526550

527551
def expand_unpack_with_variables(
528552
t: UnpackType, variables: Mapping[TypeVarId, Type]

mypy/semanal.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1600,11 +1600,17 @@ def analyze_class(self, defn: ClassDef) -> None:
16001600

16011601
for tvd in tvar_defs:
16021602
if isinstance(tvd, TypeVarType) and any(
1603-
has_placeholder(t) for t in [tvd.upper_bound, tvd.default] + tvd.values
1603+
has_placeholder(t) for t in [tvd.upper_bound] + tvd.values
16041604
):
16051605
# Some type variable bounds or values are not ready, we need
16061606
# to re-analyze this class.
16071607
self.defer()
1608+
if isinstance(tvd, TypeVarLikeType) and has_placeholder(tvd.default):
1609+
# Placeholder values in TypeVarLikeTypes may get substituted in.
1610+
# Defer current target until they are ready.
1611+
self.defer()
1612+
self.mark_incomplete(defn.name, defn)
1613+
return
16081614

16091615
self.analyze_class_keywords(defn)
16101616
bases_result = self.analyze_base_classes(bases)

0 commit comments

Comments
 (0)