Repository navigation
Refactor star-list handling, autoimport parsing and worder naming #965
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
e66165d
7a7fc6b
f3bd700
2521bdd
5fe9173
beee1f5
71fabd4
9b02e42
d47e173
5d8707f
7298471
b002bc6
3b5765b
003b5fc
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| - #965 Clean up star-list handling, autoimport parsing and worder naming, | ||
| and fix same-package import filtering in autoimport: comparing | ||
| `package_name` with `is` instead of `==` meant same-package imports | ||
| were not recognised as such, so they leaked into autoimport results | ||
| instead of being skipped. (@Ketankhunti) |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,14 +26,18 @@ | |||||||
| logger = logging.getLogger(__name__) | ||||||||
|
|
||||||||
|
|
||||||||
| _NODE_TYPE_MAP = ( | ||||||||
| (ast.ClassDef, NameType.Class), | ||||||||
| (ast.FunctionDef, NameType.Function), | ||||||||
| (ast.Assign, NameType.Variable), | ||||||||
| ) | ||||||||
|
|
||||||||
|
|
||||||||
| def get_type_ast(node: ast.AST) -> NameType: | ||||||||
| """Get the lsp type of a node.""" | ||||||||
| if isinstance(node, ast.ClassDef): | ||||||||
| return NameType.Class | ||||||||
| if isinstance(node, ast.FunctionDef): | ||||||||
| return NameType.Function | ||||||||
| if isinstance(node, ast.Assign): | ||||||||
| return NameType.Variable | ||||||||
| for node_type, name_type in _NODE_TYPE_MAP: | ||||||||
| if isinstance(node, node_type): | ||||||||
| return name_type | ||||||||
| return NameType.Variable # default value | ||||||||
|
|
||||||||
|
|
||||||||
|
|
@@ -69,21 +73,25 @@ def get_names_from_file( | |||||||
| get_type_ast(node), | ||||||||
| ) | ||||||||
| elif process_imports and isinstance(node, ast.ImportFrom): | ||||||||
| # When we process imports, we want to include names in it's own package. | ||||||||
| if node.level == 0: | ||||||||
| continue | ||||||||
| if not node.module or package_name is node.module.split(".")[0]: | ||||||||
| continue | ||||||||
| for name in node.names: | ||||||||
| if isinstance(name, ast.alias): | ||||||||
| if name.asname: | ||||||||
| real_name = name.asname | ||||||||
| else: | ||||||||
| real_name = name.name | ||||||||
| else: | ||||||||
| real_name = name | ||||||||
| if underlined or not real_name.startswith("_"): | ||||||||
| yield PartialName(real_name, get_type_ast(node)) | ||||||||
| yield from _names_from_importfrom(node, package_name, underlined) | ||||||||
|
|
||||||||
|
|
||||||||
| def _names_from_importfrom( | ||||||||
| node: ast.ImportFrom, | ||||||||
| package_name: str, | ||||||||
| underlined: bool, | ||||||||
| ) -> Generator[PartialName, None, None]: | ||||||||
| """Yield re-exported names from a relative from-import.""" | ||||||||
| # When we process imports, we want to include names in it's own package. | ||||||||
| if node.level == 0: | ||||||||
| return | ||||||||
| if not node.module or package_name == node.module.split(".")[0]: | ||||||||
| return | ||||||||
| for name in node.names: | ||||||||
| if isinstance(name, ast.alias): | ||||||||
|
Member
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 can just go:
Suggested change
? But probably might just skip the isinstance check entirely if it always has an ast.alias even when there's no alias? Maybe just turn it into |
||||||||
| real_name = name.asname if name.asname else name.name | ||||||||
| if underlined or not real_name.startswith("_"): | ||||||||
| yield PartialName(real_name, get_type_ast(node)) | ||||||||
|
|
||||||||
|
|
||||||||
| def get_type_object(imported_object) -> NameType: | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import logging | ||
|
|
||
| import pytest | ||
|
|
||
| from rope.contrib.autoimport import parse | ||
| from rope.contrib.autoimport.defs import Name, NameType, PartialName, Source | ||
|
|
||
|
|
@@ -28,3 +30,70 @@ def test_find_sys(): | |
| def test_find_underlined(): | ||
| names = list(parse.get_names_from_compiled("os", Source.BUILTIN, underlined=True)) | ||
| assert Name("_exit", "os", "os", Source.BUILTIN, NameType.Function) in names | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("source_text", "expected"), | ||
| [ | ||
| ("from .sub import helper\n", "helper"), | ||
| ("from .sub import helper as h\n", "h"), | ||
| ], | ||
| ) | ||
| def test_names_from_relative_import(tmp_path, source_text, expected): | ||
|
Member
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 excessive abstraction in test, which is a code smell. Tests naturally have lots of repetitive code, but it's not always correct or good idea to replace the test cases with a loop/parameterized test. Agreed with mcepl that, Test 1 (test_names_from_relative_import) and 3 (test_names_from_relative_submodule_import) are duplicate and only one should be kept. But test 2 (test_names_from_relative_import_with_asname) is a completely different test case and should not have been merged. If you want to reduce duplication, usually extracting a fixture/assert helper functions with descriptive names would be more appropriate instead of merging the tests. Also, generally well written tests would be structured with AAA pattern: Arrange (setup/fixture), Act, Assert. This structure should be clearly visible in the main test's body. Generally, helper functions should not cross those boundaries, e.g. a helper function should not be part of an Act and Assert at the same time. So in this test, for example, this is the AAA structure: # arrange
source = tmp_path / "mod.py"
source.write_text(source_text, encoding="utf-8")
# act
names = list(
parse.get_names_from_file(
source, package_name="pkg", process_imports=True
)
)
# assert
assert PartialName(expected, NameType.Variable) in names |
||
| source = tmp_path / "mod.py" | ||
| source.write_text(source_text, encoding="utf-8") | ||
| names = list( | ||
| parse.get_names_from_file( | ||
| source, package_name="pkg", process_imports=True | ||
| ) | ||
| ) | ||
| assert PartialName(expected, NameType.Variable) in names | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
|
Member
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. Likewise, these cases are actually testing three different things and shouldn't have been merged. |
||
| ("source_text", "package_name"), | ||
| [ | ||
| ("from os import path\n", "pkg"), # absolute import: level == 0 | ||
| ("from . import helper\n", "pkg"), # bare relative: no module | ||
| # Same package: guards the == fix in _names_from_importfrom. | ||
| # A plain "pkg" literal is enough here, since | ||
| # node.module.split(".")[0] always creates a new string object. | ||
| ("from .pkg import thing\n", "pkg"), | ||
| ], | ||
| ) | ||
| def test_import_is_skipped(tmp_path, source_text, package_name): | ||
| source = tmp_path / "mod.py" | ||
| source.write_text(source_text, encoding="utf-8") | ||
| assert ( | ||
| list( | ||
| parse.get_names_from_file( | ||
| source, package_name=package_name, process_imports=True | ||
| ) | ||
| ) | ||
| == [] | ||
| ) | ||
|
|
||
|
|
||
| def test_underlined_import_name_is_skipped_by_default(tmp_path): | ||
| source = tmp_path / "mod.py" | ||
| source.write_text("from .sub import _private\n", encoding="utf-8") | ||
| assert ( | ||
| list( | ||
| parse.get_names_from_file( | ||
| source, package_name="pkg", process_imports=True | ||
| ) | ||
| ) | ||
| == [] | ||
| ) | ||
| names = list( | ||
| parse.get_names_from_file( | ||
| source, package_name="pkg", process_imports=True, underlined=True | ||
| ) | ||
| ) | ||
| assert PartialName("_private", NameType.Variable) in names | ||
|
|
||
|
|
||
| def test_imports_ignored_without_process_imports(tmp_path): | ||
| source = tmp_path / "mod.py" | ||
| source.write_text("from . import helper\n", encoding="utf-8") | ||
| assert list(parse.get_names_from_file(source, package_name="pkg")) == [] | ||
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.
_NODE_TYPE_MAP should be
list[tuple[ast node, name type]]. tuple are not just immutable list.List is used when you want a homogenous collection of items. Tuple is used when you want an anonymous class which is usually heterogenous.