Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect resolution of nested modules #14826

Closed
dcreager opened this issue Dec 6, 2024 · 4 comments
Closed

Incorrect resolution of nested modules #14826

dcreager opened this issue Dec 6, 2024 · 4 comments
Assignees
Labels
bug Something isn't working red-knot Multi-file analysis & type inference
Milestone

Comments

@dcreager
Copy link
Member

dcreager commented Dec 6, 2024

In #14825 I tried creating a test case that refers to a class in a nested module:

```py path=a/b.py
class C: ...
```

```py
import a.b
x: type[a.b.C]
```

This produced an unexpected unresolved-attribute error:

Type <module 'System("/src/a/b.py")'> has no attribute b

This suggests that our module resolution code is binding a to a.b, and not to a as the Python spec requires.

@dcreager dcreager added the red-knot Multi-file analysis & type inference label Dec 6, 2024
@dcreager dcreager self-assigned this Dec 6, 2024
@carljm carljm added this to the Red Knot 2024 milestone Dec 7, 2024
@MichaReiser MichaReiser added the bug Something isn't working label Dec 7, 2024
@MichaReiser
Copy link
Member

@carljm this shouldn't be too hard to fix? Maybe something for a contributor to take on?

@carljm
Copy link
Contributor

carljm commented Dec 7, 2024

@MichaReiser I suggested @dcreager might take a look, good intro task for the module resolver.

@AlexWaygood
Copy link
Member

Great spot @dcreager! There is an existing TODO in the codebase regarding submodule imports, but this doesn't seem to be the same issue; I think it is a previously unknown bug.

For the record, the existing TODO is here:

// TODO:
// - Absolute `*` imports (`from collections import *`)
// - Relative `*` imports (`from ...foo import *`)
// - Submodule imports (`from collections import abc`,
// where `abc` is a submodule of the `collections` package)
//
// For the last item, see the currently skipped tests
// `follow_relative_import_bare_to_module()` and
// `follow_nonexistent_import_bare_to_module()`.

The tests referenced by that TODO don't exist anymore, however; that's because they've been ported to mdtests. They're now here:

## Bare to module
```py path=package/__init__.py
```
```py path=package/foo.py
X = 42
```
```py path=package/bar.py
# TODO: support submodule imports
from . import foo # error: [unresolved-import]
y = foo.X
# TODO: should be `Literal[42]`
reveal_type(y) # revealed: Unknown
```
## Non-existent + bare to module
```py path=package/__init__.py
```
```py path=package/bar.py
# TODO: support submodule imports
from . import foo # error: [unresolved-import]
reveal_type(foo) # revealed: Unknown
```

dcreager added a commit that referenced this issue Dec 17, 2024
#14946 fixed our handling of nested imports with the `import` statement,
but didn't touch `from...import` statements.

cf
#14826 (comment)
@dcreager
Copy link
Member Author

Fixed in #14946 and #15026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working red-knot Multi-file analysis & type inference
Projects
None yet
Development

No branches or pull requests

4 participants