[ty] Fix site-package error when multiple versions of pythons are installed in system path#25769
Open
cheparity wants to merge 2 commits into
Open
[ty] Fix site-package error when multiple versions of pythons are installed in system path#25769cheparity wants to merge 2 commits into
cheparity wants to merge 2 commits into
Conversation
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.
Typing conformance resultsNo changes detected ✅Current numbersThe 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. |
Memory usage reportMemory 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.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
SystemEnvironmentinto the site-packages discovery, so it can target the exact version directory viaprobe_package_dirsinstead of scanning all versions.A noticeable change is that
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_versionwas added by simulating the issue's system path.