MCPcopy Index your code
hub / github.com/python/mypy / TypeStrVisitor

Class TypeStrVisitor

mypy/types.py:3692–4016  ·  view source on GitHub ↗

Visitor for pretty-printing types into strings. This is mostly for debugging/testing. Do not preserve original formatting. Notes: - Represent unbound types as Foo? or Foo?[...]. - Represent the NoneType type as None. - Represent Union[x, y] as x | y

Source from the content-addressed store, hash-verified

3690
3691
3692class TypeStrVisitor(SyntheticTypeVisitor[str]):
3693 """Visitor for pretty-printing types into strings.
3694
3695 This is mostly for debugging/testing.
3696
3697 Do not preserve original formatting.
3698
3699 Notes:
3700 - Represent unbound types as Foo? or Foo?[...].
3701 - Represent the NoneType type as None.
3702 - Represent Union[x, y] as x | y
3703 """
3704
3705 def __init__(self, id_mapper: IdMapper | None = None, *, options: Options) -> None:
3706 self.id_mapper = id_mapper
3707 self.options = options
3708 self.dotted_aliases: set[TypeAliasType] | None = None
3709
3710 def visit_unbound_type(self, t: UnboundType, /) -> str:
3711 s = t.name + "?"
3712 if t.args:
3713 s += f"[{self.list_str(t.args)}]"
3714 return s
3715
3716 def visit_type_list(self, t: TypeList, /) -> str:
3717 return f"<TypeList {self.list_str(t.items)}>"
3718
3719 def visit_callable_argument(self, t: CallableArgument, /) -> str:
3720 typ = t.typ.accept(self)
3721 if t.name is None:
3722 return f"{t.constructor}({typ})"
3723 else:
3724 return f"{t.constructor}({typ}, {t.name})"
3725
3726 def visit_any(self, t: AnyType, /) -> str:
3727 return "Any"
3728
3729 def visit_none_type(self, t: NoneType, /) -> str:
3730 return "None"
3731
3732 def visit_uninhabited_type(self, t: UninhabitedType, /) -> str:
3733 return "Never"
3734
3735 def visit_erased_type(self, t: ErasedType, /) -> str:
3736 return "<Erased>"
3737
3738 def visit_deleted_type(self, t: DeletedType, /) -> str:
3739 if t.source is None:
3740 return "<Deleted>"
3741 else:
3742 return f"<Deleted '{t.source}'>"
3743
3744 def visit_instance(self, t: Instance, /) -> str:
3745 fullname = t.type.fullname
3746 if not self.options.reveal_verbose_types and fullname.startswith("builtins."):
3747 fullname = t.type.name
3748 if t.last_known_value and not t.args:
3749 # Instances with a literal fallback should never be generic. If they are,

Callers 9

dump_taggedFunction · 0.90
reveal_typeMethod · 0.90
reveal_localsMethod · 0.90
format_type_innerFunction · 0.90
setupMethod · 0.90
setupMethod · 0.90
__repr__Method · 0.85
str_with_optionsMethod · 0.85

Calls

no outgoing calls

Tested by 2

setupMethod · 0.72
setupMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…