diff --git a/CHANGELOG.md b/CHANGELOG.md index c6c6aa2f..c0ddbfb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - #786 Upgrade Actions used in Github Workflows (@lieryan) - #785 Refactoring movetest.py (@lieryan) - #788 Introduce the `preferred_import_style` configuration (@nicoolas25, @lieryan) +- #814 Treat `ignored_resources` as non-project files for autoimport purposes (@smheidrich) # Release 1.13.0 diff --git a/rope/contrib/autoimport/utils.py b/rope/contrib/autoimport/utils.py index a16efba0..008ec4a8 100644 --- a/rope/contrib/autoimport/utils.py +++ b/rope/contrib/autoimport/utils.py @@ -49,7 +49,11 @@ def get_package_source( """Detect the source of a given package. Rudimentary implementation.""" if name in sys.builtin_module_names: return Source.BUILTIN - if project is not None and project.address in str(package): + if ( + project is not None + and project.address in str(package) + and not project.is_ignored(project.get_file(str(package))) + ): return Source.PROJECT if "site-packages" in package.parts: return Source.SITE_PACKAGE diff --git a/ropetest/contrib/autoimport/utilstest.py b/ropetest/contrib/autoimport/utilstest.py index 511e1a54..43723489 100644 --- a/ropetest/contrib/autoimport/utilstest.py +++ b/ropetest/contrib/autoimport/utilstest.py @@ -1,5 +1,6 @@ """Tests for autoimport utility functions, written in pytest""" +import venv from pathlib import Path from rope.contrib.autoimport import utils @@ -22,6 +23,29 @@ def test_get_package_source_pytest(example_external_package_path): assert source == Source.SITE_PACKAGE +def test_get_package_source_venv_in_project_dir(example_external_package_path, project): + # Many Python dev tools create .venv folders inside the project directory. + # Modules in such folders should count as SITE_PACKAGE files despite + # technically being inside the project folder. + + # Set up actual venv in project folder: + project_venv_path = project.root.pathlib/".venv" + venv.create(project_venv_path) + + # Crude approximation of a package installed into this venv: + project_venv_site_packages_path = next(project_venv_path.glob("**/*site-packages")) + project.prefs["python_path"].append(project_venv_site_packages_path) + module_path = project_venv_site_packages_path / "foo.py" + module_path.touch() + + # Such directories are normally part of `ignored_resources` (e.g. `.venv` + # is in there by default): + project.prefs["ignored_resources"] += [".venv"] + + source = utils.get_package_source(module_path, project, "foo") + assert source == Source.SITE_PACKAGE + + def test_get_package_source_typing(typing_path): assert utils.get_package_source(typing_path, None, "typing") == Source.STANDARD