Skip to content

Commit a924200

Browse files
committed
Handle placeholder types better
1 parent e4400ed commit a924200

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,
@@ -189,7 +195,7 @@ def visit_type_alias_type(self, t: TypeAliasType) -> Type:
189195
return t.copy_modified(args=[arg.accept(self) for arg in t.args])
190196

191197

192-
class ExpandTypeVisitor(TypeVisitor[Type]):
198+
class ExpandTypeVisitor(SyntheticTypeVisitor[Type]):
193199
"""Visitor that substitutes type variables with values."""
194200

195201
variables: Mapping[TypeVarId, Type] # TypeVar id -> TypeVar value
@@ -507,6 +513,24 @@ def visit_type_alias_type(self, t: TypeAliasType) -> Type:
507513
def expand_types(self, types: Iterable[Type]) -> list[Type]:
508514
return [t.accept(self) for t in types]
509515

516+
def visit_star_type(self, t: StarType) -> Type:
517+
return t
518+
519+
def visit_type_list(self, t: TypeList) -> Type:
520+
return t
521+
522+
def visit_callable_argument(self, t: CallableArgument) -> Type:
523+
return t
524+
525+
def visit_ellipsis_type(self, t: EllipsisType) -> Type:
526+
return t
527+
528+
def visit_raw_expression_type(self, t: RawExpressionType) -> Type:
529+
return t
530+
531+
def visit_placeholder_type(self, t: PlaceholderType) -> Type:
532+
return t
533+
510534

511535
def expand_unpack_with_variables(
512536
t: UnpackType, variables: Mapping[TypeVarId, Type]

mypy/semanal.py

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

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

16071613
self.analyze_class_keywords(defn)
16081614
bases_result = self.analyze_base_classes(bases)

0 commit comments

Comments
 (0)