Returns all module references within a particular type.
| 7 | |
| 8 | |
| 9 | class 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: |
no outgoing calls
searching dependent graphs…