mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-03 13:23:10 +00:00
[ty] fallback to resolve_real_module in file_to_module (#20461)
Some checks are pending
CI / cargo build (release) (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
CI / cargo build (release) (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
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
This commit is contained in:
parent
bea92c8229
commit
edeb45804e
2 changed files with 56 additions and 12 deletions
|
|
@ -36,6 +36,38 @@ from .foo import X
|
||||||
reveal_type(X) # revealed: int
|
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
|
## Dotted
|
||||||
|
|
||||||
`package/__init__.py`:
|
`package/__init__.py`:
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ use rustc_hash::{FxBuildHasher, FxHashSet};
|
||||||
use ruff_db::files::{File, FilePath, FileRootKind};
|
use ruff_db::files::{File, FilePath, FileRootKind};
|
||||||
use ruff_db::system::{DirectoryEntry, System, SystemPath, SystemPathBuf};
|
use ruff_db::system::{DirectoryEntry, System, SystemPath, SystemPathBuf};
|
||||||
use ruff_db::vendored::VendoredFileSystem;
|
use ruff_db::vendored::VendoredFileSystem;
|
||||||
use ruff_python_ast::PythonVersion;
|
use ruff_python_ast::{PySourceType, PythonVersion};
|
||||||
|
|
||||||
use crate::db::Db;
|
use crate::db::Db;
|
||||||
use crate::module_name::ModuleName;
|
use crate::module_name::ModuleName;
|
||||||
|
|
@ -155,8 +155,19 @@ pub(crate) fn file_to_module(db: &dyn Db, file: File) -> Option<Module<'_>> {
|
||||||
let module_file = module.file(db)?;
|
let module_file = module.file(db)?;
|
||||||
|
|
||||||
if file.path(db) == module_file.path(db) {
|
if file.path(db) == module_file.path(db) {
|
||||||
Some(module)
|
return Some(module);
|
||||||
} else {
|
} 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:
|
// This path is for a module with the same name but with a different precedence. For example:
|
||||||
// ```
|
// ```
|
||||||
// src/foo.py
|
// src/foo.py
|
||||||
|
|
@ -165,7 +176,6 @@ pub(crate) fn file_to_module(db: &dyn Db, file: File) -> Option<Module<'_>> {
|
||||||
// The module name of `src/foo.py` is `foo`, but the module loaded by Python is `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.
|
// That means we need to ignore `src/foo.py` even though it resolves to the same module name.
|
||||||
None
|
None
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn search_paths(db: &dyn Db, resolve_mode: ModuleResolveMode) -> SearchPathIterator<'_> {
|
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 TestCase { db, src, .. } = TestCaseBuilder::new().with_src_files(SRC).build();
|
||||||
|
|
||||||
let foo = resolve_module(&db, &ModuleName::new_static("foo").unwrap()).unwrap();
|
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");
|
let foo_stub = src.join("foo.pyi");
|
||||||
|
|
||||||
assert_eq!(&src, foo.search_path(&db).unwrap());
|
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!(Some(foo), path_to_module(&db, &FilePath::System(foo_stub)));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
None,
|
Some(foo_real),
|
||||||
path_to_module(&db, &FilePath::System(src.join("foo.py")))
|
path_to_module(&db, &FilePath::System(src.join("foo.py")))
|
||||||
);
|
);
|
||||||
|
assert!(foo_real != foo);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue