Skip to content

Commit 1831a9e

Browse files
committed
Micro-optimize TypeTranslator
1 parent dbab0c4 commit 1831a9e

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

mypy/type_visitor.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def visit_instance(self, t: Instance, /) -> Type:
228228
last_known_value = raw_last_known_value
229229
return Instance(
230230
typ=t.type,
231-
args=self.translate_types(t.args),
231+
args=self.translate_type_tuple(t.args),
232232
line=t.line,
233233
column=t.column,
234234
last_known_value=last_known_value,
@@ -242,7 +242,7 @@ def visit_param_spec(self, t: ParamSpecType, /) -> Type:
242242
return t
243243

244244
def visit_parameters(self, t: Parameters, /) -> Type:
245-
return t.copy_modified(arg_types=self.translate_types(t.arg_types))
245+
return t.copy_modified(arg_types=self.translate_type_list(t.arg_types))
246246

247247
def visit_type_var_tuple(self, t: TypeVarTupleType, /) -> Type:
248248
return t
@@ -255,14 +255,14 @@ def visit_unpack_type(self, t: UnpackType, /) -> Type:
255255

256256
def visit_callable_type(self, t: CallableType, /) -> Type:
257257
return t.copy_modified(
258-
arg_types=self.translate_types(t.arg_types),
258+
arg_types=self.translate_type_list(t.arg_types),
259259
ret_type=t.ret_type.accept(self),
260260
variables=self.translate_variables(t.variables),
261261
)
262262

263263
def visit_tuple_type(self, t: TupleType, /) -> Type:
264264
return TupleType(
265-
self.translate_types(t.items),
265+
self.translate_type_list(t.items),
266266
# TODO: This appears to be unsafe.
267267
cast(Any, t.partial_fallback.accept(self)),
268268
t.line,
@@ -299,7 +299,7 @@ def visit_union_type(self, t: UnionType, /) -> Type:
299299
return cached
300300

301301
result = UnionType(
302-
self.translate_types(t.items),
302+
self.translate_type_list(t.items),
303303
t.line,
304304
t.column,
305305
uses_pep604_syntax=t.uses_pep604_syntax,
@@ -308,9 +308,12 @@ def visit_union_type(self, t: UnionType, /) -> Type:
308308
self.set_cached(t, result)
309309
return result
310310

311-
def translate_types(self, types: Iterable[Type]) -> list[Type]:
311+
def translate_type_list(self, types: list[Type]) -> list[Type]:
312312
return [t.accept(self) for t in types]
313313

314+
def translate_type_tuple(self, types: tuple[Type, ...]) -> tuple[Type, ...]:
315+
return tuple(t.accept(self) for t in types)
316+
314317
def translate_variables(
315318
self, variables: Sequence[TypeVarLikeType]
316319
) -> Sequence[TypeVarLikeType]:

0 commit comments

Comments
 (0)