Skip to content

Commit 5588c75

Browse files
authored
[red-knot] Fix relative imports in src.root (#15990)
## Summary Fixes #15989 Red Knot failed to resolve relative imports if the importing module is located at a search path root. The issue was that the module resolver returned an `Err(TooManyDots)` as soon as the parent of the current module is `None` (which is the case for a module at the search path root). However, this is incorrect if a `tail` (a module name) exists.
1 parent 9d2105b commit 5588c75

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

crates/red_knot_python_semantic/resources/mdtest/import/relative.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,33 @@ import package
218218
# error: [unresolved-attribute] "Type `<module 'package'>` has no attribute `foo`"
219219
reveal_type(package.foo.X) # revealed: Unknown
220220
```
221+
222+
## In the src-root
223+
224+
`parser.py`:
225+
226+
```py
227+
X: int = 42
228+
```
229+
230+
`__main__.py`:
231+
232+
```py
233+
from .parser import X
234+
235+
reveal_type(X) # revealed: int
236+
```
237+
238+
## Beyond the src-root
239+
240+
`parser.py`:
241+
242+
```py
243+
X: int = 42
244+
```
245+
246+
`__main__.py`:
247+
248+
```py
249+
from ..parser import X # error: [unresolved-import]
250+
```

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2513,18 +2513,32 @@ impl<'db> TypeInferenceBuilder<'db> {
25132513
.ok_or(ModuleNameResolutionError::UnknownCurrentModule)?;
25142514
let mut level = level.get();
25152515
if module.kind().is_package() {
2516-
level -= 1;
2516+
level = level.saturating_sub(1);
25172517
}
2518+
25182519
let mut module_name = module.name().clone();
2519-
for _ in 0..level {
2520-
module_name = module_name
2521-
.parent()
2522-
.ok_or(ModuleNameResolutionError::TooManyDots)?;
2520+
let tail = tail
2521+
.map(|tail| ModuleName::new(tail).ok_or(ModuleNameResolutionError::InvalidSyntax))
2522+
.transpose()?;
2523+
2524+
for remaining_dots in (0..level).rev() {
2525+
if let Some(parent) = module_name.parent() {
2526+
module_name = parent;
2527+
} else if remaining_dots == 0 {
2528+
// If we reached a search path root and this was the last dot return the tail if any.
2529+
// If there's no tail, then we have a relative import that's too deep.
2530+
return tail.ok_or(ModuleNameResolutionError::TooManyDots);
2531+
} else {
2532+
// We're at a search path root. This is a too deep relative import if there's more than
2533+
// one dot remaining.
2534+
return Err(ModuleNameResolutionError::TooManyDots);
2535+
}
25232536
}
2537+
25242538
if let Some(tail) = tail {
2525-
let tail = ModuleName::new(tail).ok_or(ModuleNameResolutionError::InvalidSyntax)?;
25262539
module_name.extend(&tail);
25272540
}
2541+
25282542
Ok(module_name)
25292543
}
25302544

0 commit comments

Comments
 (0)