Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog.d/969.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- #369, #969 Keep imports referenced by string (quoted) type
annotations when organizing imports. Names in `Literal` values
and `Annotated` metadata are not treated as references.
(@Aschenbath)
- #969 Give `async def` bodies their own scope when organizing
imports, as for `def`: an import shadowed by a local variable
inside an `async def` is now correctly removed. (@Aschenbath)
47 changes: 47 additions & 0 deletions rope/refactor/importutils/module_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
mcepl marked this conversation as resolved.
Resolved
self._visit_child_scope(node)

_AsyncFunctionDef = _FunctionDef
Comment thread
mcepl marked this conversation as resolved.
Resolved

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):
Comment thread
mcepl marked this conversation as resolved.
Resolved
if node is None:
return
if isinstance(node, ast.Constant) and isinstance(node.value, str):
try:
parsed = ast.parse(node.value, mode="eval").body
Comment thread
mcepl marked this conversation as resolved.
Resolved
except SyntaxError:
return
for child in ast.walk(parsed):
if hasattr(child, "lineno"):
Comment thread
mcepl marked this conversation as resolved.
Resolved
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))
Comment thread
mcepl marked this conversation as resolved.
Resolved
if name == "Literal":
Comment thread
mcepl marked this conversation as resolved.
Resolved
# 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)
Expand Down
67 changes: 67 additions & 0 deletions ropetest/refactor/importutilstest.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,73 @@ def test_simple_removing_unused_imports(self):
module_with_imports.remove_unused_imports()
self.assertEqual("", module_with_imports.get_changed_source())

def test_imports_used_in_string_annotations(self):
annotations = [
'value: "mod1.Baz"\n',
'class C:\n value: "mod1.Baz"\n',
'def f(value: "mod1.Baz"):\n pass\n',
'def f() -> "mod1.Baz":\n pass\n',
'async def f(value: "mod1.Baz") -> "mod1.Baz":\n pass\n',
'def f(mod1: "mod1.Baz", /, *, other: "mod1.Baz"):\n pass\n',
'value: list["mod1.Baz"]\n',
'value: "list[mod1.Baz]"\n',
'def f(*values: "mod1.Baz", **named: "mod1.Baz"):\n pass\n',
]
for annotation in annotations:
with self.subTest(annotation=annotation):
source = "from pkg1 import mod1\n\n" + annotation
self.mod.write(source)
pymod = self.project.get_module("mod")
imports = self.import_tools.module_imports(pymod)
imports.remove_unused_imports()
self.assertEqual(source, imports.get_changed_source())

def test_imported_class_used_in_string_annotation(self):
self.mod1.write("class Baz:\n pass\n")
source = 'from pkg1.mod1 import Baz\n\nvalue: "Baz"\n'
self.mod.write(source)
imports = self.import_tools.module_imports(self.project.get_module("mod"))
imports.remove_unused_imports()
self.assertEqual(source, imports.get_changed_source())

def test_imports_not_used_by_ordinary_strings(self):
self.mod.write('from pkg1 import mod1\n\nvalue = "mod1.Baz"\n')
imports = self.import_tools.module_imports(self.project.get_module("mod"))
imports.remove_unused_imports()
self.assertNotIn("from pkg1", imports.get_changed_source())

def test_imports_not_used_by_literal_annotation_values(self):
self.mod.write(
'from typing import Literal, Annotated\n'
'from pkg1 import mod1\n\n'
'value: Literal["mod1"]\n'
'other: Annotated[int, "mod1"]\n'
)
imports = self.import_tools.module_imports(self.project.get_module("mod"))
imports.remove_unused_imports()
self.assertNotIn("from pkg1", imports.get_changed_source())
self.assertIn("from typing import", imports.get_changed_source())

# Known limitation, see #993: only the names `Literal` and
# `Annotated` are special-cased, so an aliased `Literal` makes
# its value look like a forward reference and keeps the import.
@unittest.expectedFailure
def test_imports_not_used_by_aliased_literal_annotation_values(self):
self.mod.write(
"from typing import Literal as L\n"
"from pkg1 import mod1\n\n"
'value: L["mod1.Baz"]\n'
)
imports = self.import_tools.module_imports(self.project.get_module("mod"))
imports.remove_unused_imports()
self.assertNotIn("from pkg1", imports.get_changed_source())

def test_invalid_string_annotation_does_not_crash_import_cleanup(self):
self.mod.write('from pkg1 import mod1\n\nvalue: "invalid ["\n')
imports = self.import_tools.module_imports(self.project.get_module("mod"))
imports.remove_unused_imports()
self.assertNotIn("from pkg1", imports.get_changed_source())

def test_simple_removing_unused_imports_for_froms(self):
self.mod1.write(dedent("""\
def a_func():
Expand Down
Loading