Visitor for converting a node to a human-readable string. For example, an MypyFile node from program '1' is converted into something like this: MypyFile:1( fnam ExpressionStmt:1( IntExpr(1)))
| 18 | |
| 19 | |
| 20 | class StrConv(NodeVisitor[str]): |
| 21 | """Visitor for converting a node to a human-readable string. |
| 22 | |
| 23 | For example, an MypyFile node from program '1' is converted into |
| 24 | something like this: |
| 25 | |
| 26 | MypyFile:1( |
| 27 | fnam |
| 28 | ExpressionStmt:1( |
| 29 | IntExpr(1))) |
| 30 | """ |
| 31 | |
| 32 | __slots__ = ["options", "show_ids", "id_mapper"] |
| 33 | |
| 34 | def __init__(self, *, show_ids: bool = False, options: Options) -> None: |
| 35 | self.options = options |
| 36 | self.show_ids = show_ids |
| 37 | self.id_mapper: IdMapper | None = None |
| 38 | if show_ids: |
| 39 | self.id_mapper = IdMapper() |
| 40 | |
| 41 | def stringify_type(self, t: mypy.types.Type) -> str: |
| 42 | import mypy.types |
| 43 | |
| 44 | return t.accept(mypy.types.TypeStrVisitor(id_mapper=self.id_mapper, options=self.options)) |
| 45 | |
| 46 | def get_id(self, o: object) -> int | None: |
| 47 | if self.id_mapper: |
| 48 | return self.id_mapper.id(o) |
| 49 | return None |
| 50 | |
| 51 | def format_id(self, o: object) -> str: |
| 52 | if self.id_mapper: |
| 53 | return f"<{self.get_id(o)}>" |
| 54 | else: |
| 55 | return "" |
| 56 | |
| 57 | def dump(self, nodes: Sequence[object], obj: mypy.nodes.Context) -> str: |
| 58 | """Convert a list of items to a multiline pretty-printed string. |
| 59 | |
| 60 | The tag is produced from the type name of obj and its line |
| 61 | number. See mypy.util.dump_tagged for a description of the nodes |
| 62 | argument. |
| 63 | """ |
| 64 | tag = short_type(obj) + ":" + str(obj.line) |
| 65 | if self.show_ids: |
| 66 | assert self.id_mapper is not None |
| 67 | tag += f"<{self.get_id(obj)}>" |
| 68 | return dump_tagged(nodes, tag, self) |
| 69 | |
| 70 | def func_helper(self, o: mypy.nodes.FuncItem) -> list[object]: |
| 71 | """Return a list in a format suitable for dump() that represents the |
| 72 | arguments and the body of a function. The caller can then decorate the |
| 73 | array with information specific to methods, global functions or |
| 74 | anonymous functions. |
| 75 | """ |
| 76 | args: list[mypy.nodes.Var | tuple[str, list[mypy.nodes.Node]]] = [] |
| 77 | extra: list[tuple[str, list[mypy.nodes.Var]]] = [] |