From b66c699ea354edfb454bfcac2888eaa9efe79aeb Mon Sep 17 00:00:00 2001 From: Shahriar Heidrich Date: Tue, 13 May 2025 00:42:03 +0200 Subject: [PATCH 1/4] Add test: get_package_source on .venv in project --- ropetest/contrib/autoimport/utilstest.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ropetest/contrib/autoimport/utilstest.py b/ropetest/contrib/autoimport/utilstest.py index 511e1a54..de65473c 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("lib/python*/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 From 9854bc6d5fcaddfa40d513a0ef20f682c68c5313 Mon Sep 17 00:00:00 2001 From: Shahriar Heidrich Date: Tue, 13 May 2025 09:42:22 +0200 Subject: [PATCH 2/4] Treat ignored files as non-project in autoimport Fixes test introduced in previous commit. --- rope/contrib/autoimport/utils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 From 273ef3b524d540cf584676eb7d1e138c5d769c9b Mon Sep 17 00:00:00 2001 From: Shahriar Heidrich Date: Tue, 13 May 2025 21:13:39 +0200 Subject: [PATCH 3/4] Add CHANGELOG entry for package source change --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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 From 426717d695871cb58b58ee8bddd65d12f3410bb5 Mon Sep 17 00:00:00 2001 From: Shahriar Heidrich Date: Wed, 14 May 2025 22:54:33 +0200 Subject: [PATCH 4/4] Fix tests on Windows --- ropetest/contrib/autoimport/utilstest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ropetest/contrib/autoimport/utilstest.py b/ropetest/contrib/autoimport/utilstest.py index de65473c..43723489 100644 --- a/ropetest/contrib/autoimport/utilstest.py +++ b/ropetest/contrib/autoimport/utilstest.py @@ -33,7 +33,7 @@ def test_get_package_source_venv_in_project_dir(example_external_package_path, p venv.create(project_venv_path) # Crude approximation of a package installed into this venv: - project_venv_site_packages_path = next(project_venv_path.glob("lib/python*/site-packages")) + 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()