Skip to content

Conversation

MatthewMckee4
Copy link
Contributor

Summary

Fixes astral-sh/ty#171

Allows the cli to find a virtual environment from the VIRTUAL_ENV environment variable if no --python is set

Test Plan

Unsure how to mock a venv to test that this works

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Mar 19, 2025
Copy link
Contributor

github-actions bot commented Mar 19, 2025

mypy_primer results

No ecosystem changes detected ✅

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Nice

Comment on lines 157 to 166
impl PythonPath {
pub fn find_virtual_env() -> Option<Self> {
let virtual_env = std::env::var("VIRTUAL_ENV").ok();
if let Some(virtual_env) = virtual_env {
tracing::debug!("Found virtual environment at {:?}", virtual_env);
return Some(Self::ActiveEnvironment(SystemPathBuf::from(virtual_env)));
}
None
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Using this approach for when VIRTUAL_ENV is set does make sense to me because we probably want Red Knot to fail if whatever VIRTUAL_ENV is pointing to doesn't turn out to be a virtual env.

I'm not sure if it's the right approach to e.g. support .venv because we then only want to use the folder if it actually is a virtual env.

Copy link
Member

Choose a reason for hiding this comment

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

I should probably look at this for parity with uv..

Copy link
Member

Choose a reason for hiding this comment

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

(there's actually not much to review here — uv's discovery is far more complicated and doesn't really seem relevant for this particular change)

Copy link
Contributor

github-actions bot commented Mar 19, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MatthewMckee4 MatthewMckee4 marked this pull request as ready for review March 20, 2025 11:23
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

this is looking great!!

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Nice!

I think we can use SysPrefixPath in more places to avoid having to reduce the number of arguments that need to be routed through the different levels.

@MatthewMckee4
Copy link
Contributor Author

Could anyone help with testing, I'm unsure about how to go about testing this

@AlexWaygood
Copy link
Member

AlexWaygood commented Mar 20, 2025

Could anyone help with testing, I'm unsure about how to go about testing this

I'm curious if @MichaReiser has any ideas but my best idea here is lots of manual testing:

  • If we manually create an environment and activate it, can red-knot automatically resolve imports that refer to third-party packages in the venv's site_packages directory?
  • If the environment is implicitly activated via uv run, can can red-knot automatically resolve imports that refer to third-party packages in the venv's site_packages directory?
  • If we deliberately "break" a venv in one of the ways that we try to detect using the site_packages::SitePackagesDiscoveryError enum and then activate the venv and run red-knot, what do the error messages from red-knot look like?

@MichaReiser
Copy link
Member

An automated CLI test would be great, but it's probably somewhat painful to set up because SysPrefixPath does a lot of validation, and the behavior is even platform-dependent. I'm fine skipping an automated test here because it doesn't do much more than --python <path>

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

I manually tested with both "good" and "broken" virtual environments, and it all looks great. Thank you!

MatthewMckee4 and others added 2 commits March 20, 2025 13:49
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@AlexWaygood AlexWaygood enabled auto-merge (squash) March 20, 2025 13:52
@AlexWaygood AlexWaygood merged commit cdafd8e into astral-sh:main Mar 20, 2025
22 checks passed
@MatthewMckee4 MatthewMckee4 deleted the discovery-activated-venv branch March 20, 2025 14:00
dcreager added a commit that referenced this pull request Mar 21, 2025
* main: (26 commits)
  Use the common `OperatorPrecedence` for the parser (#16747)
  [red-knot] Check subtype relation between callable types (#16804)
  [red-knot] Check whether two callable types are equivalent (#16698)
  [red-knot] Ban most `Type::Instance` types in type expressions (#16872)
  Special-case value-expression inference of special form subscriptions (#16877)
  [syntax-errors] Fix star annotation before Python 3.11 (#16878)
  Recognize `SyntaxError:` as an error code for ecosystem checks (#16879)
  [red-knot] add test cases result in false positive errors (#16856)
  Bump 0.11.1 (#16871)
  Allow discovery of venv in VIRTUAL_ENV env variable (#16853)
  Split git pathspecs in change determination onto separate lines (#16869)
  Use the correct base commit for change determination (#16857)
  Separate `BitXorOr` into `BitXor` and `BitOr` precedence (#16844)
  Server: Allow `FixAll` action in presence of version-specific syntax errors (#16848)
  [`refurb`] Fix starred expressions fix (`FURB161`) (#16550)
  [`flake8-executable`] Add pytest and uv run to help message for `shebang-missing-python` (`EXE003`) (#16855)
  Show more precise messages in invalid type expressions (#16850)
  [`flake8-executables`] Allow `uv run` in shebang line for `shebang-missing-python` (`EXE003`) (#16849)
  Add `--exit-non-zero-on-format` (#16009)
  [red-knot] Ban list literals in most contexts in type expressions (#16847)
  ...
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.

Discovery of local venv
4 participants