[red-knot] Fix relative imports in src.root (#15990)

## Summary

Fixes https://github.com/astral-sh/ruff/issues/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.
This commit is contained in:
Micha Reiser 2025-02-06 14:08:20 +00:00 committed by GitHub
parent 9d2105b863
commit 5588c75d65
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 50 additions and 6 deletions

View file

@ -218,3 +218,33 @@ import package
# error: [unresolved-attribute] "Type `<module 'package'>` has no attribute `foo`"
reveal_type(package.foo.X) # revealed: Unknown
```
## In the src-root
`parser.py`:
```py
X: int = 42
```
`__main__.py`:
```py
from .parser import X
reveal_type(X) # revealed: int
```
## Beyond the src-root
`parser.py`:
```py
X: int = 42
```
`__main__.py`:
```py
from ..parser import X # error: [unresolved-import]
```