From edeb45804ef9dbe348d393f3ab77288062a3c77e Mon Sep 17 00:00:00 2001 From: Aria Desires Date: Wed, 24 Sep 2025 21:15:35 -0400 Subject: [PATCH] [ty] fallback to resolve_real_module in file_to_module (#20461) This is a naive(?) implementation of the approach @MichaReiser originally suggested to me in https://github.com/astral-sh/ty/issues/869 Fixes https://github.com/astral-sh/ty/issues/869 Fixes https://github.com/astral-sh/ty/issues/1195 --- .../resources/mdtest/import/relative.md | 32 +++++++++++++++++ .../src/module_resolver/resolver.rs | 36 ++++++++++++------- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/crates/ty_python_semantic/resources/mdtest/import/relative.md b/crates/ty_python_semantic/resources/mdtest/import/relative.md index 9c3e303a9d..6621172404 100644 --- a/crates/ty_python_semantic/resources/mdtest/import/relative.md +++ b/crates/ty_python_semantic/resources/mdtest/import/relative.md @@ -36,6 +36,38 @@ from .foo import X reveal_type(X) # revealed: int ``` +## Simple With Stub and Implementation + +This is a regression test for an issue with relative imports in implementation files when a stub is +also defined. + +`package/__init__.py`: + +```py +``` + +`package/foo.py`: + +```py +X: int = 42 +``` + +`package/bar.py`: + +```py +from .foo import X + +reveal_type(X) # revealed: int +``` + +`package/bar.pyi`: + +```pyi +from .foo import X + +reveal_type(X) # revealed: int +``` + ## Dotted `package/__init__.py`: diff --git a/crates/ty_python_semantic/src/module_resolver/resolver.rs b/crates/ty_python_semantic/src/module_resolver/resolver.rs index 86e7c9fe59..5705aefe11 100644 --- a/crates/ty_python_semantic/src/module_resolver/resolver.rs +++ b/crates/ty_python_semantic/src/module_resolver/resolver.rs @@ -19,7 +19,7 @@ use rustc_hash::{FxBuildHasher, FxHashSet}; use ruff_db::files::{File, FilePath, FileRootKind}; use ruff_db::system::{DirectoryEntry, System, SystemPath, SystemPathBuf}; use ruff_db::vendored::VendoredFileSystem; -use ruff_python_ast::PythonVersion; +use ruff_python_ast::{PySourceType, PythonVersion}; use crate::db::Db; use crate::module_name::ModuleName; @@ -155,17 +155,27 @@ pub(crate) fn file_to_module(db: &dyn Db, file: File) -> Option> { let module_file = module.file(db)?; if file.path(db) == module_file.path(db) { - Some(module) - } else { - // This path is for a module with the same name but with a different precedence. For example: - // ``` - // src/foo.py - // src/foo/__init__.py - // ``` - // The module name of `src/foo.py` is `foo`, but the module loaded by Python is `src/foo/__init__.py`. - // That means we need to ignore `src/foo.py` even though it resolves to the same module name. - None + return Some(module); + } else if file.source_type(db) == PySourceType::Python + && module_file.source_type(db) == PySourceType::Stub + { + // If a .py and .pyi are both defined, the .pyi will be the one returned by `resolve_module().file`, + // which would make us erroneously believe the `.py` is *not* also this module (breaking things + // like relative imports). So here we try `resolve_real_module().file` to cover both cases. + let module = resolve_real_module(db, &module_name)?; + let module_file = module.file(db)?; + if file.path(db) == module_file.path(db) { + return Some(module); + } } + // This path is for a module with the same name but with a different precedence. For example: + // ``` + // src/foo.py + // src/foo/__init__.py + // ``` + // The module name of `src/foo.py` is `foo`, but the module loaded by Python is `src/foo/__init__.py`. + // That means we need to ignore `src/foo.py` even though it resolves to the same module name. + None } pub(crate) fn search_paths(db: &dyn Db, resolve_mode: ModuleResolveMode) -> SearchPathIterator<'_> { @@ -1576,6 +1586,7 @@ mod tests { let TestCase { db, src, .. } = TestCaseBuilder::new().with_src_files(SRC).build(); let foo = resolve_module(&db, &ModuleName::new_static("foo").unwrap()).unwrap(); + let foo_real = resolve_real_module(&db, &ModuleName::new_static("foo").unwrap()).unwrap(); let foo_stub = src.join("foo.pyi"); assert_eq!(&src, foo.search_path(&db).unwrap()); @@ -1583,9 +1594,10 @@ mod tests { assert_eq!(Some(foo), path_to_module(&db, &FilePath::System(foo_stub))); assert_eq!( - None, + Some(foo_real), path_to_module(&db, &FilePath::System(src.join("foo.py"))) ); + assert!(foo_real != foo); } #[test]