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

Class TypeIndirectionVisitor

mypy/indirection.py:9–170  ·  view source on GitHub ↗

Returns all module references within a particular type.

Source from the content-addressed store, hash-verified

7
8
9class TypeIndirectionVisitor(TypeVisitor[None]):
10 """Returns all module references within a particular type."""
11
12 def __init__(self) -> None:
13 # Module references are collected here
14 self.modules: set[str] = set()
15 # User to avoid infinite recursion with recursive types
16 self.seen_types: set[types.TypeAliasType | types.Instance] = set()
17
18 def find_modules(self, typs: Iterable[types.Type]) -> set[str]:
19 self.modules = set()
20 self.seen_types = set()
21 for typ in typs:
22 self._visit(typ)
23 return self.modules
24
25 def _visit(self, typ: types.Type) -> None:
26 # Note: instances are needed for `class str(Sequence[str]): ...`
27 if (
28 isinstance(typ, types.TypeAliasType)
29 or isinstance(typ, types.ProperType)
30 and isinstance(typ, types.Instance)
31 ):
32 # Avoid infinite recursion for recursive types.
33 if typ in self.seen_types:
34 return
35 self.seen_types.add(typ)
36 typ.accept(self)
37
38 def _visit_type_tuple(self, typs: tuple[types.Type, ...]) -> None:
39 # Micro-optimization: Specialized version of _visit for lists
40 for typ in typs:
41 if (
42 isinstance(typ, types.TypeAliasType)
43 or isinstance(typ, types.ProperType)
44 and isinstance(typ, types.Instance)
45 ):
46 # Avoid infinite recursion for recursive types.
47 if typ in self.seen_types:
48 continue
49 self.seen_types.add(typ)
50 typ.accept(self)
51
52 def _visit_type_list(self, typs: list[types.Type]) -> None:
53 # Micro-optimization: Specialized version of _visit for tuples
54 for typ in typs:
55 if (
56 isinstance(typ, types.TypeAliasType)
57 or isinstance(typ, types.ProperType)
58 and isinstance(typ, types.Instance)
59 ):
60 # Avoid infinite recursion for recursive types.
61 if typ in self.seen_types:
62 continue
63 self.seen_types.add(typ)
64 typ.accept(self)
65
66 def visit_unbound_type(self, t: types.UnboundType) -> None:

Callers 2

__init__Method · 0.90

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…