[ty] Migrate the namespace package module resolver tests to mdtests (#18133)
Some checks are pending
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
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 (release) (push) Waiting to run
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 / mkdocs (push) Waiting to run
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 (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Micha Reiser 2025-05-16 19:56:33 +02:00 committed by GitHub
parent e21972a79b
commit 3d55a16c91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 107 additions and 95 deletions

View file

@ -0,0 +1,107 @@
# Namespace package
## Basic namespace package
```toml
[environment]
python = "/.venv"
```
`parent/child/one.py`:
```py
one = 1
```
`/.venv/<path-to-site-packages>/parent/child/two.py`:
```py
two = 2
```
`main.py`:
```py
import parent.child.one
import parent.child.two
```
`from.py`
```py
# TODO: This should not be an error
from parent.child import one, two # error: [unresolved-import]
```
## Regular package in namespace package
```toml
[environment]
python = "/.venv"
```
An adapted test case from the
[PEP420 examples](https://peps.python.org/pep-0420/#nested-namespace-packages). The
`src/parent/child` package is a regular package. Therefore, `site_packages/parent/child/two.py`
should not be resolved.
```ignore
src
parent
child
__init__.py
one.py
.venv/site-packages
parent
child
two.py
```
`parent/child/__init__.py`:
```py
```
`parent/child/one.py`:
```py
one = 1
```
`/.venv/<path-to-site-packages>/parent/child/two.py`:
```py
two = 2
```
`main.py`:
```py
import parent.child.one
import parent.child.two # error: [unresolved-import]
```
## Priority between file and identically named namespace package
If there's a namespace package with the same name as a module, the module takes precedence.
`foo.py`:
```py
x = "module"
```
`foo/bar.py`:
```py
x = "namespace"
```
```py
from foo import x
reveal_type(x) # revealed: Unknown | Literal["module"]
import foo.bar # error: [unresolved-import]
```