mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:35:58 +00:00
[ty] Improve tests for site-packages
discovery (#18374)
Some checks are pending
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 (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 / 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 / mkdocs (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
Some checks are pending
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 (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 / 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 / mkdocs (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
## Summary - Convert tests demonstrating our resilience to malformed/absent `version` fields in `pyvenf.cfg` files to mdtests. Also make them more expansive. - Convert the regression test I added in https://github.com/astral-sh/ruff/pull/18157 to an mdtest - Add comments next to unit tests that cannot be converted to mdtests (but where it's not obvious why they can't) so I don't have to do this exercise again 😄 - In `site_packages.rs`, factor out the logic for figuring out where we expect the system-installation `site-packages` to be. Currently we have the same logic twice. ## Test Plan `cargo test -p ty_python_semantic`
This commit is contained in:
parent
363f061f09
commit
ad2f667ee4
2 changed files with 175 additions and 65 deletions
|
@ -1,5 +1,115 @@
|
|||
# Tests for `site-packages` discovery
|
||||
|
||||
## Malformed or absent `version` fields
|
||||
|
||||
The `version`/`version_info` key in a `pyvenv.cfg` file is provided by most virtual-environment
|
||||
creation tools to indicate the Python version the virtual environment is for. They key is useful for
|
||||
our purposes, so we try to parse it when possible. However, the key is not read by the CPython
|
||||
standard library, and is provided under different keys depending on which virtual-environment
|
||||
creation tool created the `pyvenv.cfg` file (the stdlib `venv` module calls the key `version`,
|
||||
whereas uv and virtualenv both call it `version_info`). We therefore do not return an error when
|
||||
discovering a virtual environment's `site-packages` directory if the virtula environment contains a
|
||||
`pyvenv.cfg` file which doesn't have this key, or if the associated value of the key doesn't parse
|
||||
according to our expectations. The file isn't really *invalid* in this situation.
|
||||
|
||||
### No `version` field
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python = "/.venv"
|
||||
```
|
||||
|
||||
`/.venv/pyvenv.cfg`:
|
||||
|
||||
```cfg
|
||||
home = /doo/doo/wop/cpython-3.13.2-macos-aarch64-none/bin
|
||||
```
|
||||
|
||||
`/doo/doo/wop/cpython-3.13.2-macos-aarch64-none/bin/python`:
|
||||
|
||||
```text
|
||||
```
|
||||
|
||||
`/.venv/<path-to-site-packages>/foo.py`:
|
||||
|
||||
```py
|
||||
X: int = 42
|
||||
```
|
||||
|
||||
`/src/main.py`:
|
||||
|
||||
```py
|
||||
from foo import X
|
||||
|
||||
reveal_type(X) # revealed: int
|
||||
```
|
||||
|
||||
### Malformed stdlib-style version field
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python = "/.venv"
|
||||
```
|
||||
|
||||
`/.venv/pyvenv.cfg`:
|
||||
|
||||
```cfg
|
||||
home = /doo/doo/wop/cpython-3.13.2-macos-aarch64-none/bin
|
||||
version = wut
|
||||
```
|
||||
|
||||
`/doo/doo/wop/cpython-3.13.2-macos-aarch64-none/bin/python`:
|
||||
|
||||
```text
|
||||
```
|
||||
|
||||
`/.venv/<path-to-site-packages>/foo.py`:
|
||||
|
||||
```py
|
||||
X: int = 42
|
||||
```
|
||||
|
||||
`/src/main.py`:
|
||||
|
||||
```py
|
||||
from foo import X
|
||||
|
||||
reveal_type(X) # revealed: int
|
||||
```
|
||||
|
||||
### Malformed uv-style version field
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python = "/.venv"
|
||||
```
|
||||
|
||||
`/.venv/pyvenv.cfg`:
|
||||
|
||||
```cfg
|
||||
home = /doo/doo/wop/cpython-3.13.2-macos-aarch64-none/bin
|
||||
version_info = no-really-wut
|
||||
```
|
||||
|
||||
`/doo/doo/wop/cpython-3.13.2-macos-aarch64-none/bin/python`:
|
||||
|
||||
```text
|
||||
```
|
||||
|
||||
`/.venv/<path-to-site-packages>/foo.py`:
|
||||
|
||||
```py
|
||||
X: int = 42
|
||||
```
|
||||
|
||||
`/src/main.py`:
|
||||
|
||||
```py
|
||||
from foo import X
|
||||
|
||||
reveal_type(X) # revealed: int
|
||||
```
|
||||
|
||||
## Ephemeral uv environments
|
||||
|
||||
If you use the `--with` flag when invoking `uv run`, uv will create an "ephemeral" virtual
|
||||
|
@ -57,3 +167,41 @@ from bar import Y
|
|||
reveal_type(X) # revealed: int
|
||||
reveal_type(Y) # revealed: str
|
||||
```
|
||||
|
||||
## `pyvenv.cfg` files with unusual values
|
||||
|
||||
`pyvenv.cfg` files can have unusual values in them, which can contain arbitrary characters. This
|
||||
includes `=` characters. The following is a regression test for
|
||||
<https://github.com/astral-sh/ty/issues/430>.
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python = "/.venv"
|
||||
```
|
||||
|
||||
`/.venv/pyvenv.cfg`:
|
||||
|
||||
```cfg
|
||||
home = /doo/doo/wop/cpython-3.13.2-macos-aarch64-none/bin
|
||||
version_info = 3.13
|
||||
command = /.pyenv/versions/3.13.3/bin/python3.13 -m venv --without-pip --prompt="python-default/3.13.3" /somewhere-else/python/virtualenvs/python-default/3.13.3
|
||||
```
|
||||
|
||||
`/doo/doo/wop/cpython-3.13.2-macos-aarch64-none/bin/python`:
|
||||
|
||||
```text
|
||||
```
|
||||
|
||||
`/.venv/<path-to-site-packages>/foo.py`:
|
||||
|
||||
```py
|
||||
X: int = 42
|
||||
```
|
||||
|
||||
`/src/main.py`:
|
||||
|
||||
```py
|
||||
from foo import X
|
||||
|
||||
reveal_type(X) # revealed: int
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue