Skip to content

Commit d37bdc6

Browse files
committed
Update optional visitors
1 parent 49518db commit d37bdc6

File tree

2 files changed

+104
-104
lines changed

2 files changed

+104
-104
lines changed

mypy/type_visitor.py

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -201,25 +201,25 @@ def set_cached(self, orig: Type, new: Type) -> None:
201201
self.cache = {}
202202
self.cache[orig] = new
203203

204-
def visit_unbound_type(self, t: UnboundType) -> Type:
204+
def visit_unbound_type(self, t: UnboundType, /) -> Type:
205205
return t
206206

207-
def visit_any(self, t: AnyType) -> Type:
207+
def visit_any(self, t: AnyType, /) -> Type:
208208
return t
209209

210-
def visit_none_type(self, t: NoneType) -> Type:
210+
def visit_none_type(self, t: NoneType, /) -> Type:
211211
return t
212212

213-
def visit_uninhabited_type(self, t: UninhabitedType) -> Type:
213+
def visit_uninhabited_type(self, t: UninhabitedType, /) -> Type:
214214
return t
215215

216-
def visit_erased_type(self, t: ErasedType) -> Type:
216+
def visit_erased_type(self, t: ErasedType, /) -> Type:
217217
return t
218218

219-
def visit_deleted_type(self, t: DeletedType) -> Type:
219+
def visit_deleted_type(self, t: DeletedType, /) -> Type:
220220
return t
221221

222-
def visit_instance(self, t: Instance) -> Type:
222+
def visit_instance(self, t: Instance, /) -> Type:
223223
last_known_value: LiteralType | None = None
224224
if t.last_known_value is not None:
225225
raw_last_known_value = t.last_known_value.accept(self)
@@ -234,32 +234,32 @@ def visit_instance(self, t: Instance) -> Type:
234234
extra_attrs=t.extra_attrs,
235235
)
236236

237-
def visit_type_var(self, t: TypeVarType) -> Type:
237+
def visit_type_var(self, t: TypeVarType, /) -> Type:
238238
return t
239239

240-
def visit_param_spec(self, t: ParamSpecType) -> Type:
240+
def visit_param_spec(self, t: ParamSpecType, /) -> Type:
241241
return t
242242

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

246-
def visit_type_var_tuple(self, t: TypeVarTupleType) -> Type:
246+
def visit_type_var_tuple(self, t: TypeVarTupleType, /) -> Type:
247247
return t
248248

249-
def visit_partial_type(self, t: PartialType) -> Type:
249+
def visit_partial_type(self, t: PartialType, /) -> Type:
250250
return t
251251

252-
def visit_unpack_type(self, t: UnpackType) -> Type:
252+
def visit_unpack_type(self, t: UnpackType, /) -> Type:
253253
return UnpackType(t.type.accept(self))
254254

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

262-
def visit_tuple_type(self, t: TupleType) -> Type:
262+
def visit_tuple_type(self, t: TupleType, /) -> Type:
263263
return TupleType(
264264
self.translate_types(t.items),
265265
# TODO: This appears to be unsafe.
@@ -268,7 +268,7 @@ def visit_tuple_type(self, t: TupleType) -> Type:
268268
t.column,
269269
)
270270

271-
def visit_typeddict_type(self, t: TypedDictType) -> Type:
271+
def visit_typeddict_type(self, t: TypedDictType, /) -> Type:
272272
# Use cache to avoid O(n**2) or worse expansion of types during translation
273273
if cached := self.get_cached(t):
274274
return cached
@@ -285,12 +285,12 @@ def visit_typeddict_type(self, t: TypedDictType) -> Type:
285285
self.set_cached(t, result)
286286
return result
287287

288-
def visit_literal_type(self, t: LiteralType) -> Type:
288+
def visit_literal_type(self, t: LiteralType, /) -> Type:
289289
fallback = t.fallback.accept(self)
290290
assert isinstance(fallback, Instance) # type: ignore[misc]
291291
return LiteralType(value=t.value, fallback=fallback, line=t.line, column=t.column)
292292

293-
def visit_union_type(self, t: UnionType) -> Type:
293+
def visit_union_type(self, t: UnionType, /) -> Type:
294294
# Use cache to avoid O(n**2) or worse expansion of types during translation
295295
# (only for large unions, since caching adds overhead)
296296
use_cache = len(t.items) > 3
@@ -315,19 +315,19 @@ def translate_variables(
315315
) -> Sequence[TypeVarLikeType]:
316316
return variables
317317

318-
def visit_overloaded(self, t: Overloaded) -> Type:
318+
def visit_overloaded(self, t: Overloaded, /) -> Type:
319319
items: list[CallableType] = []
320320
for item in t.items:
321321
new = item.accept(self)
322322
assert isinstance(new, CallableType) # type: ignore[misc]
323323
items.append(new)
324324
return Overloaded(items=items)
325325

326-
def visit_type_type(self, t: TypeType) -> Type:
326+
def visit_type_type(self, t: TypeType, /) -> Type:
327327
return TypeType.make_normalized(t.item.accept(self), line=t.line, column=t.column)
328328

329329
@abstractmethod
330-
def visit_type_alias_type(self, t: TypeAliasType) -> Type:
330+
def visit_type_alias_type(self, t: TypeAliasType, /) -> Type:
331331
# This method doesn't have a default implementation for type translators,
332332
# because type aliases are special: some information is contained in the
333333
# TypeAlias node, and we normally don't generate new nodes. Every subclass
@@ -359,83 +359,83 @@ def __init__(self, strategy: Callable[[list[T]], T]) -> None:
359359
# to skip targets in some cases (e.g. when collecting type variables).
360360
self.skip_alias_target = False
361361

362-
def visit_unbound_type(self, t: UnboundType) -> T:
362+
def visit_unbound_type(self, t: UnboundType, /) -> T:
363363
return self.query_types(t.args)
364364

365-
def visit_type_list(self, t: TypeList) -> T:
365+
def visit_type_list(self, t: TypeList, /) -> T:
366366
return self.query_types(t.items)
367367

368-
def visit_callable_argument(self, t: CallableArgument) -> T:
368+
def visit_callable_argument(self, t: CallableArgument, /) -> T:
369369
return t.typ.accept(self)
370370

371-
def visit_any(self, t: AnyType) -> T:
371+
def visit_any(self, t: AnyType, /) -> T:
372372
return self.strategy([])
373373

374-
def visit_uninhabited_type(self, t: UninhabitedType) -> T:
374+
def visit_uninhabited_type(self, t: UninhabitedType, /) -> T:
375375
return self.strategy([])
376376

377-
def visit_none_type(self, t: NoneType) -> T:
377+
def visit_none_type(self, t: NoneType, /) -> T:
378378
return self.strategy([])
379379

380-
def visit_erased_type(self, t: ErasedType) -> T:
380+
def visit_erased_type(self, t: ErasedType, /) -> T:
381381
return self.strategy([])
382382

383-
def visit_deleted_type(self, t: DeletedType) -> T:
383+
def visit_deleted_type(self, t: DeletedType, /) -> T:
384384
return self.strategy([])
385385

386-
def visit_type_var(self, t: TypeVarType) -> T:
386+
def visit_type_var(self, t: TypeVarType, /) -> T:
387387
return self.query_types([t.upper_bound, t.default] + t.values)
388388

389-
def visit_param_spec(self, t: ParamSpecType) -> T:
389+
def visit_param_spec(self, t: ParamSpecType, /) -> T:
390390
return self.query_types([t.upper_bound, t.default, t.prefix])
391391

392-
def visit_type_var_tuple(self, t: TypeVarTupleType) -> T:
392+
def visit_type_var_tuple(self, t: TypeVarTupleType, /) -> T:
393393
return self.query_types([t.upper_bound, t.default])
394394

395-
def visit_unpack_type(self, t: UnpackType) -> T:
395+
def visit_unpack_type(self, t: UnpackType, /) -> T:
396396
return self.query_types([t.type])
397397

398-
def visit_parameters(self, t: Parameters) -> T:
398+
def visit_parameters(self, t: Parameters, /) -> T:
399399
return self.query_types(t.arg_types)
400400

401-
def visit_partial_type(self, t: PartialType) -> T:
401+
def visit_partial_type(self, t: PartialType, /) -> T:
402402
return self.strategy([])
403403

404-
def visit_instance(self, t: Instance) -> T:
404+
def visit_instance(self, t: Instance, /) -> T:
405405
return self.query_types(t.args)
406406

407-
def visit_callable_type(self, t: CallableType) -> T:
407+
def visit_callable_type(self, t: CallableType, /) -> T:
408408
# FIX generics
409409
return self.query_types(t.arg_types + [t.ret_type])
410410

411-
def visit_tuple_type(self, t: TupleType) -> T:
411+
def visit_tuple_type(self, t: TupleType, /) -> T:
412412
return self.query_types(t.items)
413413

414-
def visit_typeddict_type(self, t: TypedDictType) -> T:
414+
def visit_typeddict_type(self, t: TypedDictType, /) -> T:
415415
return self.query_types(t.items.values())
416416

417-
def visit_raw_expression_type(self, t: RawExpressionType) -> T:
417+
def visit_raw_expression_type(self, t: RawExpressionType, /) -> T:
418418
return self.strategy([])
419419

420-
def visit_literal_type(self, t: LiteralType) -> T:
420+
def visit_literal_type(self, t: LiteralType, /) -> T:
421421
return self.strategy([])
422422

423-
def visit_union_type(self, t: UnionType) -> T:
423+
def visit_union_type(self, t: UnionType, /) -> T:
424424
return self.query_types(t.items)
425425

426-
def visit_overloaded(self, t: Overloaded) -> T:
426+
def visit_overloaded(self, t: Overloaded, /) -> T:
427427
return self.query_types(t.items)
428428

429-
def visit_type_type(self, t: TypeType) -> T:
429+
def visit_type_type(self, t: TypeType, /) -> T:
430430
return t.item.accept(self)
431431

432-
def visit_ellipsis_type(self, t: EllipsisType) -> T:
432+
def visit_ellipsis_type(self, t: EllipsisType, /) -> T:
433433
return self.strategy([])
434434

435-
def visit_placeholder_type(self, t: PlaceholderType) -> T:
435+
def visit_placeholder_type(self, t: PlaceholderType, /) -> T:
436436
return self.query_types(t.args)
437437

438-
def visit_type_alias_type(self, t: TypeAliasType) -> T:
438+
def visit_type_alias_type(self, t: TypeAliasType, /) -> T:
439439
# Skip type aliases already visited types to avoid infinite recursion.
440440
# TODO: Ideally we should fire subvisitors here (or use caching) if we care
441441
# about duplicates.
@@ -493,52 +493,52 @@ def reset(self) -> None:
493493
"""
494494
self.seen_aliases = None
495495

496-
def visit_unbound_type(self, t: UnboundType) -> bool:
496+
def visit_unbound_type(self, t: UnboundType, /) -> bool:
497497
return self.query_types(t.args)
498498

499-
def visit_type_list(self, t: TypeList) -> bool:
499+
def visit_type_list(self, t: TypeList, /) -> bool:
500500
return self.query_types(t.items)
501501

502-
def visit_callable_argument(self, t: CallableArgument) -> bool:
502+
def visit_callable_argument(self, t: CallableArgument, /) -> bool:
503503
return t.typ.accept(self)
504504

505-
def visit_any(self, t: AnyType) -> bool:
505+
def visit_any(self, t: AnyType, /) -> bool:
506506
return self.default
507507

508-
def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
508+
def visit_uninhabited_type(self, t: UninhabitedType, /) -> bool:
509509
return self.default
510510

511-
def visit_none_type(self, t: NoneType) -> bool:
511+
def visit_none_type(self, t: NoneType, /) -> bool:
512512
return self.default
513513

514-
def visit_erased_type(self, t: ErasedType) -> bool:
514+
def visit_erased_type(self, t: ErasedType, /) -> bool:
515515
return self.default
516516

517-
def visit_deleted_type(self, t: DeletedType) -> bool:
517+
def visit_deleted_type(self, t: DeletedType, /) -> bool:
518518
return self.default
519519

520-
def visit_type_var(self, t: TypeVarType) -> bool:
520+
def visit_type_var(self, t: TypeVarType, /) -> bool:
521521
return self.query_types([t.upper_bound, t.default] + t.values)
522522

523-
def visit_param_spec(self, t: ParamSpecType) -> bool:
523+
def visit_param_spec(self, t: ParamSpecType, /) -> bool:
524524
return self.query_types([t.upper_bound, t.default])
525525

526-
def visit_type_var_tuple(self, t: TypeVarTupleType) -> bool:
526+
def visit_type_var_tuple(self, t: TypeVarTupleType, /) -> bool:
527527
return self.query_types([t.upper_bound, t.default])
528528

529-
def visit_unpack_type(self, t: UnpackType) -> bool:
529+
def visit_unpack_type(self, t: UnpackType, /) -> bool:
530530
return self.query_types([t.type])
531531

532-
def visit_parameters(self, t: Parameters) -> bool:
532+
def visit_parameters(self, t: Parameters, /) -> bool:
533533
return self.query_types(t.arg_types)
534534

535-
def visit_partial_type(self, t: PartialType) -> bool:
535+
def visit_partial_type(self, t: PartialType, /) -> bool:
536536
return self.default
537537

538-
def visit_instance(self, t: Instance) -> bool:
538+
def visit_instance(self, t: Instance, /) -> bool:
539539
return self.query_types(t.args)
540540

541-
def visit_callable_type(self, t: CallableType) -> bool:
541+
def visit_callable_type(self, t: CallableType, /) -> bool:
542542
# FIX generics
543543
# Avoid allocating any objects here as an optimization.
544544
args = self.query_types(t.arg_types)
@@ -548,34 +548,34 @@ def visit_callable_type(self, t: CallableType) -> bool:
548548
else:
549549
return args and ret
550550

551-
def visit_tuple_type(self, t: TupleType) -> bool:
551+
def visit_tuple_type(self, t: TupleType, /) -> bool:
552552
return self.query_types(t.items)
553553

554-
def visit_typeddict_type(self, t: TypedDictType) -> bool:
554+
def visit_typeddict_type(self, t: TypedDictType, /) -> bool:
555555
return self.query_types(list(t.items.values()))
556556

557-
def visit_raw_expression_type(self, t: RawExpressionType) -> bool:
557+
def visit_raw_expression_type(self, t: RawExpressionType, /) -> bool:
558558
return self.default
559559

560-
def visit_literal_type(self, t: LiteralType) -> bool:
560+
def visit_literal_type(self, t: LiteralType, /) -> bool:
561561
return self.default
562562

563-
def visit_union_type(self, t: UnionType) -> bool:
563+
def visit_union_type(self, t: UnionType, /) -> bool:
564564
return self.query_types(t.items)
565565

566-
def visit_overloaded(self, t: Overloaded) -> bool:
566+
def visit_overloaded(self, t: Overloaded, /) -> bool:
567567
return self.query_types(t.items) # type: ignore[arg-type]
568568

569-
def visit_type_type(self, t: TypeType) -> bool:
569+
def visit_type_type(self, t: TypeType, /) -> bool:
570570
return t.item.accept(self)
571571

572-
def visit_ellipsis_type(self, t: EllipsisType) -> bool:
572+
def visit_ellipsis_type(self, t: EllipsisType, /) -> bool:
573573
return self.default
574574

575-
def visit_placeholder_type(self, t: PlaceholderType) -> bool:
575+
def visit_placeholder_type(self, t: PlaceholderType, /) -> bool:
576576
return self.query_types(t.args)
577577

578-
def visit_type_alias_type(self, t: TypeAliasType) -> bool:
578+
def visit_type_alias_type(self, t: TypeAliasType, /) -> bool:
579579
# Skip type aliases already visited types to avoid infinite recursion.
580580
# TODO: Ideally we should fire subvisitors here (or use caching) if we care
581581
# about duplicates.

0 commit comments

Comments
 (0)