Fix relative import resolution in site-packages packages when the site-packages search path is a subdirectory of the first-party search path#17178
Conversation
|
I also manually checked that we are able to resolve the import I guess we don't run mypy_primer on any projects that use pydantic right now! |
|
I now get "Module This error does not show up with Edit: not on all files though, ill try to reproduce |
| /// Path to a custom typeshed directory | ||
| custom_typeshed: Option<SystemPathBuf>, | ||
| /// Paths to the directory to use for `site-packages` | ||
| site_packages: Vec<SystemPathBuf>, |
There was a problem hiding this comment.
You removed custom_typeshed because we aren't using it anymore? (I guess mdtest now supports this so those tests have been able to move there.)
Huh. How are you running this branch? If I run the following commands: Where |
|
That works perfectly fine for me, just in one of my codebases this import fails, ill keep trying to reproduce |
|
I think I'll land this for now anyway since it definitely fixes a bug (both test cases added here fail on |
|
Ah sorry im being stupid, my ruff server was linked to my fork of ruff and my ide was showing the old errors, sorry about that |
|
No worries at all -- very easily done :-) |
* origin/main: [red-knot] Add `Type::TypeVar` variant (#17102) [red-knot] update to latest Salsa with fixpoint caching fix (#17179) Upgrade to Rust 1.86 and bump MSRV to 1.84 (#17171) [red-knot] Avoid unresolved-reference in unreachable code (#17169) Fix relative import resolution in `site-packages` packages when the `site-packages` search path is a subdirectory of the first-party search path (#17178) [DO NOT LAND] bump Salsa version (#17176) [syntax-errors] Detect duplicate keys in `match` mapping patterns (#17129)
…site-packages` search path is a subdirectory of the first-party search path (astral-sh#17178) ## Summary If a package in `site-packages` had this directory structure: ```py # bar/__init__.py from .a import A # bar/a.py class A: ... ``` then we would fail to resolve the `from .a import A` import _if_ (as is usually the case!) the `site-packages` search path was located inside a `.venv` directory that was a subdirectory of the project's first-party search path. The reason for this is a bug in `file_to_module` in the module resolver. In this loop, we would identify that `/project_root/.venv/lib/python3.13/site-packages/foo/__init__.py` can be turned into a path relative to the first-party search path (`/project_root`): https://github.com/astral-sh/ruff/blob/6e2b8f9696e87d786cfed6304dc3baa0f029d341/crates/red_knot_python_semantic/src/module_resolver/resolver.rs#L101-L110 but we'd then try to turn the relative path (.venv/lib/python3.13/site-packages/foo/__init__.py`) into a module path, realise that it wasn't a valid module path... and therefore immediately `break` out of the loop before trying any other search paths (such as the `site-packages` search path). This bug was originally reported on Discord by @MatthewMckee4. ## Test Plan I added a unit test for `file_to_module` in `resolver.rs`, and an integration test that shows we can now resolve the import correctly in `infer.rs`.
…ges` test as an mdtest (#17222) ## Summary This PR does the following things: - Fixes the `python` configuration setting for mdtest (added in #17221) so that it expects a path pointing to a venv's `sys.prefix` variable rather than the a path pointing to the venv's `site-packages` subdirectory. This brings the `python` setting in mdtest in sync with our CLI `--python` flag. - Tweaks mdtest so that it automatically creates a valid `pyvenv.cfg` file for you if you don't specify one. This makes it much more ergonomic to write an mdtest with a custom `python` setting: red-knot will reject a `python` setting that points to a directory that doesn't have a `pyvenv.cfg` file in it - Tweaks mdtest so that it doesn't check a custom `pyvenv.cfg` as Python source code if you _do_ add a custom `pyvenv.cfg` file for your mock virtual environment in an mdtest. (You get a lot of diagnostics about Python syntax errors in the `pyvenv.cfg` file, otherwise!) - Rewrites the test added in #17178 as an mdtest, and deletes the original test that was added in that PR ## Test Plan I verified that the new mdtest fails if I revert the changes to `resolver.rs` that were added in #17178
Summary
If a package in
site-packageshad this directory structure:then we would fail to resolve the
from .a import Aimport if (as is usually the case!) thesite-packagessearch path was located inside a.venvdirectory that was a subdirectory of the project's first-party search path. The reason for this is a bug infile_to_modulein the module resolver. In this loop, we would identify that/project_root/.venv/lib/python3.13/site-packages/foo/__init__.pycan be turned into a path relative to the first-party search path (/project_root):ruff/crates/red_knot_python_semantic/src/module_resolver/resolver.rs
Lines 101 to 110 in 6e2b8f9
but we'd then try to turn the relative path (
.venv/lib/python3.13/site-packages/foo/__init__.py) into a module path, realise that it wasn't a valid module path... and therefore immediatelybreakout of the loop before trying any other search paths (such as thesite-packagessearch path).This bug was originally reported on Discord by @MatthewMckee4.
Test Plan
I added a unit test for
file_to_moduleinresolver.rs, and an integration test that shows we can now resolve the import correctly ininfer.rs.