[ty] Allow discovering dependencies in system Python environments#22994
[ty] Allow discovering dependencies in system Python environments#22994
Conversation
Typing conformance resultsNo changes detected ✅ |
|
4c9471a to
dce6885
Compare
dce6885 to
6c953b4
Compare
Memory usage reportMemory usage unchanged ✅ |
|
I don't have all the context on how this works today, but did we consider something like a |
I don't love the merging either, but it pre-exists this PR, and a In practice we haven't seen any issues from the merging -- generally people are fine with ty being able to see more dependencies, we get complaints when we don't see dependencies. So erring on the side of seeing more seems like generally the right approach. |
|
As you say, I think the merging in #21286 was justified by
However, I think it might be justifiable for ty to stop discovering system packages when you happen to be in a project? That seems more likely to be surprising and problematic than the Would you be opposed to shipping this without the merging for that case first to see if we get feedback that it's too strict? |
Not necessarily. But can you specifically describe a scenario in which it would be "surprising and problematic" to include system packages along with project packages (keeping in mind that the system packages in this case are lower priority than the project packages) when running ty in a project, and why in that scenario it would be better not to do so? In every scenario I can think of, including them seems strictly better than not including them. |
|
I think the most basic example is I'm type checking on a system which has Python packages installed for various system functionality and I get a false negative when my project imports one of those packages without declaring it as a dependency. |
|
Yes, that makes sense. Ok, we can try this without merging. |
…led into Fixes an issue where ty couldn't resolve imports from packages installed in a system Python environment when ty itself was installed directly in that system Python (rather than in a virtual environment). Previously, `SysPrefixPathOrigin::SelfEnvironment` was treated as requiring a virtual environment (with `pyvenv.cfg`), which caused discovery to fail for system Python installations. This change allows ty to fall back to treating its own environment as a `SystemEnvironment` when no `pyvenv.cfg` is found. The priority behavior differs based on whether ty is installed in a venv: - When ty is installed in a virtual environment (e.g., `uvx --with ...`), ty's venv takes priority over other discovered environments, and both are included in the search path - When ty is installed in a system Python and there's a project environment (like `.venv`), only the project environment is used; the system Python's site-packages are excluded entirely Fixes astral-sh/ty#2068 https://claude.ai/code/session_01885t5j7zeT78vRZCtu8X9C
6c953b4 to
0fddbab
Compare
AlexWaygood
left a comment
There was a problem hiding this comment.
At some point we need to update the docs at
ruff/crates/ty_project/src/metadata/options.rs
Lines 666 to 681 in 6ded4be
We also need to amend the "ty only supports discovery of virtual environments at this time" line at
ruff/crates/ty_project/src/metadata/options.rs
Lines 666 to 681 in 6ded4be
| .ok(); | ||
| .ok()?; | ||
|
|
||
| tracing::debug!("Using site-packages from ty's environment"); |
There was a problem hiding this comment.
sounds like we won't necessarily actually "use" the site-packages from ty's environment in the latest version of this PR?
| tracing::debug!("Using site-packages from ty's environment"); | |
| tracing::debug!("Resolved site-packages from ty's environment"); |
* main: (209 commits) [ty] Defer base inference for functional `type(...)` classes (#22792) flake8-executable: allow global flags in uv shebangs (EXE003) (#22582) [ty] Add `replace-imports-with-any` option (#23122) Update html comments in mdtests (#23269) Apply ruff formatting to mdtests (#22935) [ty] Exclude test-related symbols from non-first-party packages in auto-import completions [ty] Refactor `CursorTest` helper to support site-packages [ty] Qualify inlay hint edit symbol when possibly referencing another variable (#23265) [ty] Avoid `UnionBuilder` overhead when creating a new union from the filtered elements of an existing union (#22352) [ty] Refactor TypedDict key assignment validation (#23262) [ty] Improve Python environment path documentation (#23256) [ty] loop control flow analysis using loop header definitions Prepare for 0.15.1 (#23253) Remove docker-run-action (#23254) [ty] Allow discovering dependencies in system Python environments (#22994) Ensure pending suppression diagnostics are reported (#23242) [`isort`] support for configurable import section heading comments (#23151) [ty] Fix method calls on subclasses of `Any` (#23248) [ty] Fix bound method access on `None` (#23246) Make range suppression test snapshot actually useful (#23251) ...
Summary
Fixes astral-sh/ty#2068.
This change allows ty to discover and use dependencies installed in system Python environments where ty itself is installed.
The subtlety here is in the case where we discover a local
.venvAND ty is installed in a different Python env. In this case, we prioritize the local.venv, to avoid false-negatives due to accidental reliance on globally installed packages.Test Plan
Added test; existing tests continue to pass.