Skip to content

[ty] Fix site-package error when multiple versions of pythons are installed in system path#25769

Open
cheparity wants to merge 2 commits into
astral-sh:mainfrom
cheparity:main
Open

[ty] Fix site-package error when multiple versions of pythons are installed in system path#25769
cheparity wants to merge 2 commits into
astral-sh:mainfrom
cheparity:main

Conversation

@cheparity

@cheparity cheparity commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix astral-sh/ty#3424

When --python /opt/bin/python3.14 is passed, the sys.prefix is derived as /opt (grandparent of the executable), but the Python version (3.14) embedded in the filename was discarded. For system installations without a pyvenv.cfg, SystemEnvironment had no way to know the version, so site_packages_directories_from_sys_prefix scanned all python3.* directories under the lib dir, picking up site-packages from unrelated Python versions.

Fix by extracting the Python version from the executable path before it's resolved to sys.prefix, and threading it through SystemEnvironment into the site-packages discovery, so it can target the exact version directory via probe_package_dirs instead of scanning all versions.

A noticeable change is that

pub struct SystemEnvironment {
    root_path: SysPrefixPath,
    python_version: Option<PythonVersion>, //! newly added
}

the struct was changed to convey version info. May it cause potential influence to other modules?

CC @MichaReiser . Thanks for reviewing.

Test Plan

A new unit test uses_correct_site_packages_when_python_executable_path_has_version was added by simulating the issue's system path.

image

When multiple versions of python are installed under `/opt/`, we will
encountered an error that ty will select an error `/opt/lib/`(See astral-sh#3424 ).
The test is for checking whether the lib's selected properly.
@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Jun 9, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 92.23%. The percentage of expected errors that received a diagnostic held steady at 87.42%. The number of fully passing files held steady at 92/134.

@astral-sh-bot

astral-sh-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

When `--python /opt/bin/python3.14` is passed, the sys.prefix is derived
as `/opt` (grandparent of the executable), but the Python version (3.14)
embedded in the filename was discarded. For system installations without
a `pyvenv.cfg`, `SystemEnvironment` had no way to know the version, so
`site_packages_directories_from_sys_prefix` scanned all `python3.*`
directories under the lib dir, picking up site-packages from unrelated
Python versions. (astral-sh#3424)

Fix by extracting the Python version from the executable path before
it's resolved to sys.prefix, and threading it through `SystemEnvironment`
into the site-packages discovery, so it can target the exact version
directory via `probe_package_dirs` instead of scanning all versions.
@astral-sh-bot

astral-sh-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

/// - `.venv/bin/python3.12` -> `Some(PythonVersion(3, 12))`
/// - `/opt/bin/python3.13t` -> `Some(PythonVersion(3, 13))` (free-threaded)
/// - `C:\Python3.14\python3.14.exe` -> `Some(PythonVersion(3, 14))` (Windows)
fn python_version_from_executable_path(path: &SystemPath) -> Option<PythonVersion> {

@cheparity cheparity Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait I discovered a problem. There might be a circumstance in Unix:

/opt/bin/python -> /path/to/real/python3.14 # it is a softlink!
/opt/lib/python3.14/site-packages/

and this function may need to be refined to support softlink. Or, to directly read version info stored in python (I think that we shouldn't do that. It's too slow).

@alicederyn alicederyn left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there's two installed Pythons with the same version, e.g. non-freethreaded + freethreaded 3.14?

I think you need to capture the exact Python and determine the site-packages using the same algorithm as e.g. uv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ty using incorrect site-packages

4 participants