-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[undocumented] Optionally export line-level information about referen…
…ces (#14805) When run with `--export-ref-info`, store line-level reference information in the cache, in a JSON file with a `.refs.json` extension. It includes the line numbers and targets of each RefExpr in the program. This only works properly if incremental mode is disabled. The target can either be a fullname, or `*.name` for an `name` attribute reference where the type of the object is unknown. This is an undocumented, experimental feature that may be useful for certain tools, but it shouldn't be used in production use cases.
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Find line-level reference information from a mypy AST (undocumented feature)""" | ||
|
||
from __future__ import annotations | ||
|
||
from mypy.nodes import LDEF, MemberExpr, MypyFile, NameExpr, RefExpr | ||
from mypy.traverser import TraverserVisitor | ||
|
||
|
||
class RefInfoVisitor(TraverserVisitor): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
self.data: list[dict[str, object]] = [] | ||
|
||
def visit_name_expr(self, expr: NameExpr) -> None: | ||
super().visit_name_expr(expr) | ||
self.record_ref_expr(expr) | ||
|
||
def visit_member_expr(self, expr: MemberExpr) -> None: | ||
super().visit_member_expr(expr) | ||
self.record_ref_expr(expr) | ||
|
||
def record_ref_expr(self, expr: RefExpr) -> None: | ||
fullname = None | ||
if expr.kind != LDEF and "." in expr.fullname: | ||
fullname = expr.fullname | ||
elif isinstance(expr, MemberExpr) and not expr.fullname: | ||
fullname = f"*.{expr.name}" | ||
if fullname is not None: | ||
self.data.append({"line": expr.line, "column": expr.column, "target": fullname}) | ||
|
||
|
||
def get_undocumented_ref_info_json(tree: MypyFile) -> list[dict[str, object]]: | ||
visitor = RefInfoVisitor() | ||
tree.accept(visitor) | ||
return visitor.data |