diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0d2f6e3c9..e4c58c1d1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,7 +1,7 @@ name: Run tests on: [push, pull_request] - + concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true diff --git a/CHANGELOG.md b/CHANGELOG.md index ba328bd9a..cec9585ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - #119, #928 Fix IndexError in rename and find occurrences when a `from` import is the last thing in a module without a trailing newline (@mcepl) - #921 Patch the else suite of try statements that also have a finally (@marlon-costa-dc) - #937 Preserve non-ASCII argument expressions and default values when changing function signatures (@Jinyong-Li) +- #814 Treat `ignored_resources` as non-project files for autoimport purposes (@smheidrich) # Release 1.15.1 diff --git a/rope/contrib/autoimport/utils.py b/rope/contrib/autoimport/utils.py index 11ee6f20e..c8bc5b9c1 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 511e1a541..f2df37f47 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