Repository navigation
Preserve imports referenced by quoted type annotations #969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -437,11 +437,58 @@ def _visit_child_scope(self, node): | |
| visitor.visit(child) | ||
|
|
||
| def _FunctionDef(self, node): | ||
| # Signature annotations resolve outside the function's local scope. | ||
| arguments = node.args | ||
| for argument in ( | ||
| arguments.posonlyargs | ||
| + arguments.args | ||
| + arguments.kwonlyargs | ||
| + [arguments.vararg, arguments.kwarg] | ||
| ): | ||
| if argument is not None: | ||
| self._visit_annotation(argument.annotation) | ||
| self._visit_annotation(node.returns) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: unquoted signature annotations end up being walked twice — once here in the enclosing-scope finder and again through the |
||
| self._visit_child_scope(node) | ||
|
|
||
| _AsyncFunctionDef = _FunctionDef | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is more than string-annotation support: on master, import os
async def f():
os = "local"
return osmaster wrongly keeps |
||
|
|
||
| def _ClassDef(self, node): | ||
| self._visit_child_scope(node) | ||
|
|
||
| def _AnnAssign(self, node): | ||
| self._visit_annotation(node.annotation) | ||
| self.generic_visit(node) | ||
|
|
||
| def _visit_annotation(self, node): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor gap: PEP 695 type aliases still lose the import — |
||
| if node is None: | ||
| return | ||
| if isinstance(node, ast.Constant) and isinstance(node.value, str): | ||
| try: | ||
| parsed = ast.parse(node.value, mode="eval").body | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ultra-nit for a possible follow-up: for f-string annotations the literal text fragments get parsed as expressions too, so |
||
| except SyntaxError: | ||
| return | ||
| for child in ast.walk(parsed): | ||
| if hasattr(child, "lineno"): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: a one-line comment here would help — only |
||
| child.lineno = node.lineno | ||
| self._visit_annotation(parsed) | ||
| elif isinstance(node, (ast.Name, ast.Attribute)): | ||
| self.visit(node) | ||
| elif isinstance(node, ast.Subscript): | ||
| self.visit(node.value) | ||
| name = getattr(node.value, "id", getattr(node.value, "attr", None)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style nit: this nested |
||
| if name == "Literal": | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the other direction, an aliased |
||
| # These strings are values, not forward references. | ||
| self.visit(node.slice) | ||
| elif name == "Annotated" and isinstance(node.slice, ast.Tuple): | ||
| self._visit_annotation(node.slice.elts[0]) | ||
| for metadata in node.slice.elts[1:]: | ||
| self.visit(metadata) | ||
| else: | ||
| self._visit_annotation(node.slice) | ||
| else: | ||
| for child in ast.iter_child_nodes(node): | ||
| self._visit_annotation(child) | ||
|
|
||
| def _Name(self, node): | ||
| if self._get_root()._is_node_interesting(node) and not self.is_bound(node.id): | ||
| self.add_unbound(node.id) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sibling entries carry contributor attribution (e.g.
(@lieryan)); consider(@Aschenbath). Also, this entry currently sits above the#859block — grouping with the recent entries would read better. And if you take up the_AsyncFunctionDefscope-isolation comment above, it deserves its own bullet.