Skip to content

Commit 3f6736d

Browse files
committed
Fix some typing issues
1 parent 1df73d4 commit 3f6736d

7 files changed

+72
-45
lines changed

mypy/constraints.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def _infer_constraints(template: Type, actual: Type, direction: int) -> list[Con
227227
if isinstance(actual, UnionType):
228228
actual = mypy.typeops.make_simplified_union(actual.items, keep_erased=True)
229229
if isinstance(actual, TypeVarType) and actual.has_default():
230-
actual = actual.default
230+
actual = get_proper_type(actual.default)
231231

232232
# Ignore Any types from the type suggestion engine to avoid them
233233
# causing us to infer Any in situations where a better job could

mypy/semanal.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -4017,13 +4017,15 @@ def process_typevar_parameters(
40174017
if has_values:
40184018
self.fail("TypeVar cannot have both values and an upper bound", context)
40194019
return None
4020-
upper_bound = self.get_typevarlike_argument(param_name, param_value, context)
4021-
if upper_bound is None:
4020+
tv_arg = self.get_typevarlike_argument(param_name, param_value, context)
4021+
if tv_arg is None:
40224022
return None
4023+
upper_bound = tv_arg
40234024
elif param_name == "default":
4024-
default = self.get_typevarlike_argument(param_name, param_value, context)
4025-
if default is None:
4025+
tv_arg = self.get_typevarlike_argument(param_name, param_value, context)
4026+
if tv_arg is None:
40264027
return None
4028+
default = tv_arg
40274029
elif param_name == "values":
40284030
# Probably using obsolete syntax with values=(...). Explain the current syntax.
40294031
self.fail('TypeVar "values" argument not supported', context)
@@ -4119,16 +4121,17 @@ def process_paramspec_declaration(self, s: AssignmentStmt) -> bool:
41194121
return False
41204122

41214123
n_values = call.arg_kinds[1:].count(ARG_POS)
4122-
default = AnyType(TypeOfAny.from_omitted_generics)
4124+
default: Type = AnyType(TypeOfAny.from_omitted_generics)
41234125
for param_value, param_name, param_kind in zip(
41244126
call.args[1 + n_values :],
41254127
call.arg_names[1 + n_values :],
41264128
call.arg_kinds[1 + n_values :],
41274129
):
41284130
if param_name == "default":
4129-
default = self.get_typevarlike_argument(param_name, param_value, s)
4130-
if default is None:
4131+
tv_arg = self.get_typevarlike_argument(param_name, param_value, s)
4132+
if tv_arg is None:
41314133
return False
4134+
default = tv_arg
41324135
else:
41334136
# ParamSpec is different from a regular TypeVar:
41344137
# arguments are not semantically valid. But, allowed in runtime.
@@ -4166,16 +4169,17 @@ def process_typevartuple_declaration(self, s: AssignmentStmt) -> bool:
41664169
return False
41674170

41684171
n_values = call.arg_kinds[1:].count(ARG_POS)
4169-
default = AnyType(TypeOfAny.from_omitted_generics)
4172+
default: Type = AnyType(TypeOfAny.from_omitted_generics)
41704173
for param_value, param_name, param_kind in zip(
41714174
call.args[1 + n_values :],
41724175
call.arg_names[1 + n_values :],
41734176
call.arg_kinds[1 + n_values :],
41744177
):
41754178
if param_name == "default":
4176-
default = self.get_typevarlike_argument(param_name, param_value, s)
4177-
if default is None:
4179+
tv_arg = self.get_typevarlike_argument(param_name, param_value, s)
4180+
if tv_arg is None:
41784181
return False
4182+
default = tv_arg
41794183
if not isinstance(default, UnpackType):
41804184
self.fail(
41814185
"The default argument to TypeVarTuple must be an Unpacked tuple", default

mypy/test/testtypes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def test_generic_function_type(self) -> None:
151151
assert_equal(str(c), "def [X] (X?, Y?) -> Y?")
152152

153153
v = [
154-
TypeVarType("Y", "Y", -1, [], self.fx.o),
154+
TypeVarType("Y", "Y", -1, [], self.fx.o, AnyType(TypeOfAny.from_omitted_generics)),
155155
TypeVarType("X", "X", -2, [], self.fx.o, AnyType(TypeOfAny.from_omitted_generics)),
156156
]
157157
c2 = CallableType([], [], [], NoneType(), self.function, name=None, variables=v)
@@ -180,7 +180,7 @@ def test_type_alias_expand_all(self) -> None:
180180

181181
def test_recursive_nested_in_non_recursive(self) -> None:
182182
A, _ = self.fx.def_alias_1(self.fx.a)
183-
T = TypeVarType("T", "T", -1, [], self.fx.o)
183+
T = TypeVarType("T", "T", -1, [], self.fx.o, AnyType(TypeOfAny.from_omitted_generics))
184184
NA = self.fx.non_rec_alias(Instance(self.fx.gi, [T]), [T], [A])
185185
assert not NA.is_recursive
186186
assert has_recursive_types(NA)

mypy/test/typefixture.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,32 @@ def make_type_info(
312312
v: list[TypeVarLikeType] = []
313313
for id, n in enumerate(typevars, 1):
314314
if typevar_tuple_index is not None and id - 1 == typevar_tuple_index:
315-
v.append(TypeVarTupleType(n, n, id, self.o, self.std_tuple))
315+
v.append(
316+
TypeVarTupleType(
317+
n,
318+
n,
319+
id,
320+
self.o,
321+
self.std_tuple,
322+
AnyType(TypeOfAny.from_omitted_generics),
323+
)
324+
)
316325
else:
317326
if variances:
318327
variance = variances[id - 1]
319328
else:
320329
variance = COVARIANT
321-
v.append(TypeVarType(n, n, id, [], self.o, variance=variance))
330+
v.append(
331+
TypeVarType(
332+
n,
333+
n,
334+
id,
335+
[],
336+
self.o,
337+
AnyType(TypeOfAny.from_omitted_generics),
338+
variance=variance,
339+
)
340+
)
322341
class_def.type_vars = v
323342

324343
info = TypeInfo(SymbolTable(), class_def, module_name)

mypy/tvar_scope.py

+30-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from copy import copy
4-
from typing import Generator
4+
from typing import Iterator
55

66
from mypy.nodes import (
77
ParamSpecExpr,
@@ -10,7 +10,7 @@
1010
TypeVarLikeExpr,
1111
TypeVarTupleExpr,
1212
)
13-
from mypy.type_visitor import SyntheticTypeVisitor, T
13+
from mypy.type_visitor import SyntheticTypeVisitor
1414
from mypy.types import (
1515
AnyType,
1616
CallableArgument,
@@ -45,96 +45,96 @@
4545
)
4646

4747

48-
class TypeVarLikeYielder(SyntheticTypeVisitor[Generator[TypeVarLikeType, None, None]]):
48+
class TypeVarLikeYielder(SyntheticTypeVisitor[Iterator[TypeVarLikeType]]):
4949
"""Yield all TypeVarLikeTypes in a type."""
5050

51-
def visit_type_var(self, t: TypeVarType) -> Generator[TypeVarLikeType, None, None]:
51+
def visit_type_var(self, t: TypeVarType) -> Iterator[TypeVarLikeType]:
5252
yield t
5353

54-
def visit_type_var_tuple(self, t: TypeVarTupleType) -> Generator[TypeVarLikeType, None, None]:
54+
def visit_type_var_tuple(self, t: TypeVarTupleType) -> Iterator[TypeVarLikeType]:
5555
yield t
5656

57-
def visit_param_spec(self, t: ParamSpecType) -> Generator[TypeVarLikeType, None, None]:
57+
def visit_param_spec(self, t: ParamSpecType) -> Iterator[TypeVarLikeType]:
5858
yield t
5959

60-
def visit_callable_type(self, t: CallableType) -> Generator[TypeVarLikeType, None, None]:
60+
def visit_callable_type(self, t: CallableType) -> Iterator[TypeVarLikeType]:
6161
for arg in t.arg_types:
6262
yield from arg.accept(self)
6363
yield from t.ret_type.accept(self)
6464

65-
def visit_instance(self, t: Instance) -> Generator[TypeVarLikeType, None, None]:
65+
def visit_instance(self, t: Instance) -> Iterator[TypeVarLikeType]:
6666
for arg in t.args:
6767
yield from arg.accept(self)
6868

69-
def visit_overloaded(self, t: Overloaded) -> Generator[TypeVarLikeType, None, None]:
69+
def visit_overloaded(self, t: Overloaded) -> Iterator[TypeVarLikeType]:
7070
for item in t.items:
7171
yield from item.accept(self)
7272

73-
def visit_tuple_type(self, t: TupleType) -> Generator[TypeVarLikeType, None, None]:
73+
def visit_tuple_type(self, t: TupleType) -> Iterator[TypeVarLikeType]:
7474
for item in t.items:
7575
yield from item.accept(self)
7676

77-
def visit_type_alias_type(self, t: TypeAliasType) -> Generator[TypeVarLikeType, None, None]:
77+
def visit_type_alias_type(self, t: TypeAliasType) -> Iterator[TypeVarLikeType]:
7878
for arg in t.args:
7979
yield from arg.accept(self)
8080

81-
def visit_typeddict_type(self, t: TypedDictType) -> Generator[TypeVarLikeType, None, None]:
81+
def visit_typeddict_type(self, t: TypedDictType) -> Iterator[TypeVarLikeType]:
8282
for arg in t.items.values():
8383
yield from arg.accept(self)
8484

85-
def visit_union_type(self, t: UnionType) -> Generator[TypeVarLikeType, None, None]:
85+
def visit_union_type(self, t: UnionType) -> Iterator[TypeVarLikeType]:
8686
for arg in t.items:
8787
yield from arg.accept(self)
8888

89-
def visit_type_type(self, t: TypeType) -> T:
89+
def visit_type_type(self, t: TypeType) -> Iterator[TypeVarLikeType]:
9090
yield from t.item.accept(self)
9191

92-
def visit_star_type(self, t: StarType) -> T:
92+
def visit_star_type(self, t: StarType) -> Iterator[TypeVarLikeType]:
9393
yield from ()
9494

95-
def visit_type_list(self, t: TypeList) -> T:
95+
def visit_type_list(self, t: TypeList) -> Iterator[TypeVarLikeType]:
9696
yield from ()
9797

98-
def visit_callable_argument(self, t: CallableArgument) -> T:
98+
def visit_callable_argument(self, t: CallableArgument) -> Iterator[TypeVarLikeType]:
9999
yield from ()
100100

101-
def visit_ellipsis_type(self, t: EllipsisType) -> T:
101+
def visit_ellipsis_type(self, t: EllipsisType) -> Iterator[TypeVarLikeType]:
102102
yield from ()
103103

104-
def visit_raw_expression_type(self, t: RawExpressionType) -> T:
104+
def visit_raw_expression_type(self, t: RawExpressionType) -> Iterator[TypeVarLikeType]:
105105
yield from ()
106106

107-
def visit_unbound_type(self, t: UnboundType) -> T:
107+
def visit_unbound_type(self, t: UnboundType) -> Iterator[TypeVarLikeType]:
108108
yield from ()
109109

110-
def visit_none_type(self, t: NoneType) -> T:
110+
def visit_none_type(self, t: NoneType) -> Iterator[TypeVarLikeType]:
111111
yield from ()
112112

113-
def visit_uninhabited_type(self, t: UninhabitedType) -> T:
113+
def visit_uninhabited_type(self, t: UninhabitedType) -> Iterator[TypeVarLikeType]:
114114
yield from ()
115115

116-
def visit_erased_type(self, t: ErasedType) -> T:
116+
def visit_erased_type(self, t: ErasedType) -> Iterator[TypeVarLikeType]:
117117
yield from ()
118118

119-
def visit_deleted_type(self, t: DeletedType) -> T:
119+
def visit_deleted_type(self, t: DeletedType) -> Iterator[TypeVarLikeType]:
120120
yield from ()
121121

122-
def visit_parameters(self, t: Parameters) -> T:
122+
def visit_parameters(self, t: Parameters) -> Iterator[TypeVarLikeType]:
123123
yield from ()
124124

125-
def visit_literal_type(self, t: LiteralType) -> T:
125+
def visit_literal_type(self, t: LiteralType) -> Iterator[TypeVarLikeType]:
126126
yield from ()
127127

128-
def visit_partial_type(self, t: PartialType) -> T:
128+
def visit_partial_type(self, t: PartialType) -> Iterator[TypeVarLikeType]:
129129
yield from ()
130130

131-
def visit_unpack_type(self, t: UnpackType) -> T:
131+
def visit_unpack_type(self, t: UnpackType) -> Iterator[TypeVarLikeType]:
132132
yield from ()
133133

134-
def visit_any(self, t: AnyType) -> Generator[TypeVarLikeType, None, None]:
134+
def visit_any(self, t: AnyType) -> Iterator[TypeVarLikeType]:
135135
yield from ()
136136

137-
def visit_placeholder_type(self, t: PlaceholderType) -> Generator[TypeVarLikeType, None, None]:
137+
def visit_placeholder_type(self, t: PlaceholderType) -> Iterator[TypeVarLikeType]:
138138
yield from ()
139139

140140

mypy/typeanal.py

+1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ def visit_unbound_type_nonoptional(self, t: UnboundType, defining_literal: bool)
385385
tvar_def.id,
386386
tvar_def.upper_bound,
387387
sym.node.tuple_fallback,
388+
tvar_def.default,
388389
line=t.line,
389390
column=t.column,
390391
)

mypy/types.py

+3
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ def copy_modified(
583583
id: Bogus[TypeVarId | int] = _dummy,
584584
line: int = _dummy_int,
585585
column: int = _dummy_int,
586+
**kwargs: Any,
586587
) -> TypeVarType:
587588
return TypeVarType(
588589
self.name,
@@ -702,6 +703,7 @@ def copy_modified(
702703
flavor: int = _dummy_int,
703704
prefix: Bogus[Parameters] = _dummy,
704705
default: Bogus[Type] = _dummy,
706+
**kwargs: Any,
705707
) -> ParamSpecType:
706708
return ParamSpecType(
707709
self.name,
@@ -823,6 +825,7 @@ def copy_modified(
823825
id: Bogus[TypeVarId | int] = _dummy,
824826
upper_bound: Bogus[Type] = _dummy,
825827
default: Bogus[Type] = _dummy,
828+
**kwargs: Any,
826829
) -> TypeVarTupleType:
827830
return TypeVarTupleType(
828831
self.name,

0 commit comments

Comments
 (0)