Skip to content

Conversation

@zanieb
Copy link
Member

@zanieb zanieb commented May 8, 2025

Summary

Updates the --python flag to accept Python executables in virtual environments. Notably, we do not query the executable and it must be in a canonical location in a virtual environment. This is pretty naive, but solves for the trivial case of ty check --python .venv/bin/python3 which will be a common mistake (and ty check --python $(which python))

I explored this while trying to understand Python discovery in ty in service of astral-sh/ty#272, I'm not attached to it, but figure it's worth sharing.

As an alternative, we can add more variants to the SearchPathValidationError and just improve the error message, i.e., by hinting that this looks like a virtual environment and suggesting the concrete alternative path they should provide. We'll probably want to do that for some other cases anyway (e.g., 3.13 as described in the linked issue)

This functionality is also briefly mentioned in astral-sh/ty#193

Closes astral-sh/ty#318

Test Plan

e.g.,

uv run ty check --python .venv/bin/python3

needs test coverage still

@zanieb zanieb added the ty Multi-file analysis & type inference label May 8, 2025
// to deny cases like `.venv/bin/foo`
target
.file_name()
.is_some_and(|name| name.starts_with("python"))
Copy link
Member Author

@zanieb zanieb May 8, 2025

Choose a reason for hiding this comment

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

We could be more strict here, and require python.exe on Windows and python | python3 | python3.XX on Unix. The last case is a bit annoying. We use a regular expression in uv, but I think that's probably not worth moving here yet? We might end up needing it here for nice error messages for other cases regardless though.

@github-actions
Copy link
Contributor

github-actions bot commented May 8, 2025

mypy_primer results

No ecosystem changes detected ✅

@zanieb
Copy link
Member Author

zanieb commented May 11, 2025

I'm going to mark this as ready for review, for consensus on an approach. I'll add test coverage and update documentation before merging.

@zanieb zanieb marked this pull request as ready for review May 11, 2025 15:57
@AlexWaygood
Copy link
Member

(I haven't reviewed yet — currently in an airport on my phone — but I'm on board with the idea!)

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.

This looks good to me. We should update the CLI and settings documentation

// and search for a virtual environment in the root directory. Ideally, we'd
// invoke the target to determine `sys.prefix` here, but that's more complicated
// and may be deferred to uv.
.is_file(target)
Copy link
Member

Choose a reason for hiding this comment

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

Should we also test if the file has the executable bit set (path_metadata instead of is_file)

Copy link
Member Author

@zanieb zanieb May 12, 2025

Choose a reason for hiding this comment

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

Seems reasonable, but is a little more painful cross-platform for little gain since we're just going to fail downstream anyway if we can't find site-packages

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me know if you think it's worth pursuing regardless.

@zanieb zanieb force-pushed the zb/resolve-python branch 3 times, most recently from c2f8c21 to 6235433 Compare May 12, 2025 20:22
@zanieb zanieb force-pushed the zb/resolve-python branch from 6235433 to 4874038 Compare May 12, 2025 20:23
@zanieb zanieb force-pushed the zb/resolve-python branch from 4874038 to d517ed2 Compare May 12, 2025 20:26
@zanieb zanieb merged commit 6b64630 into main May 12, 2025
35 checks passed
@zanieb zanieb deleted the zb/resolve-python branch May 12, 2025 20:39
@zanieb zanieb changed the title Update --python to accept paths to executables in virtual environments Update --python to accept paths to executables in environments May 12, 2025
dcreager added a commit that referenced this pull request May 13, 2025
…eepish

* origin/main:
  [ty] Induct into instances and subclasses when finding and applying generics (#18052)
  [ty] Allow classes to inherit from `type[Any]` or `type[Unknown]` (#18060)
  [ty] Allow a class to inherit from an intersection if the intersection contains a dynamic type and the intersection is not disjoint from `type` (#18055)
  [ty] Narrowing for `hasattr()` (#18053)
  Update reference documentation for `--python-version` (#18056)
  [`flake8-bugbear`] Ignore `B028` if `skip_file_prefixes` is present (#18047)
  [`airflow`] Apply try-catch guard to all AIR3 rules (`AIR3`) (#17887)
  [`pylint`] add fix safety section (`PLW3301`) (#17878)
  Update `--python` to accept paths to executables in virtual environments (#17954)
  [`pylint`] add fix safety section (`PLE4703`) (#17824)
  [`ruff`] Implement a recursive check for `RUF060` (#17976)
  [`flake8-use-pathlib`] `PTH*` suppress diagnostic for all `os.*` functions that have the `dir_fd` parameter (#17968)
  [`refurb`] Mark autofix as safe only for number literals in `FURB116` (#17692)
  [`flake8-simplify`] Fix `SIM905` autofix for `rsplit` creating a reversed list literal (#18045)
  Avoid initializing progress bars early (#18049)
dcreager added a commit that referenced this pull request May 13, 2025
…eep-dish

* origin/main:
  [ty] Infer parameter specializations of generic aliases (#18021)
  [ty] Understand homogeneous tuple annotations (#17998)
  [ty] Induct into instances and subclasses when finding and applying generics (#18052)
  [ty] Allow classes to inherit from `type[Any]` or `type[Unknown]` (#18060)
  [ty] Allow a class to inherit from an intersection if the intersection contains a dynamic type and the intersection is not disjoint from `type` (#18055)
  [ty] Narrowing for `hasattr()` (#18053)
  Update reference documentation for `--python-version` (#18056)
  [`flake8-bugbear`] Ignore `B028` if `skip_file_prefixes` is present (#18047)
  [`airflow`] Apply try-catch guard to all AIR3 rules (`AIR3`) (#17887)
  [`pylint`] add fix safety section (`PLW3301`) (#17878)
  Update `--python` to accept paths to executables in virtual environments (#17954)
  [`pylint`] add fix safety section (`PLE4703`) (#17824)
  [`ruff`] Implement a recursive check for `RUF060` (#17976)
  [`flake8-use-pathlib`] `PTH*` suppress diagnostic for all `os.*` functions that have the `dir_fd` parameter (#17968)
  [`refurb`] Mark autofix as safe only for number literals in `FURB116` (#17692)
  [`flake8-simplify`] Fix `SIM905` autofix for `rsplit` creating a reversed list literal (#18045)
  Avoid initializing progress bars early (#18049)
@dhruvmanila dhruvmanila added the cli Related to the command-line interface label May 13, 2025
Glyphack pushed a commit to Glyphack/ruff that referenced this pull request May 21, 2025
…nts (astral-sh#17954)

## Summary

Updates the `--python` flag to accept Python executables in virtual
environments. Notably, we do not query the executable and it _must_ be
in a canonical location in a virtual environment. This is pretty naive,
but solves for the trivial case of `ty check --python .venv/bin/python3`
which will be a common mistake (and `ty check --python $(which python)`)

I explored this while trying to understand Python discovery in ty in
service of astral-sh/ty#272, I'm not attached
to it, but figure it's worth sharing.

As an alternative, we can add more variants to the
`SearchPathValidationError` and just improve the _error_ message, i.e.,
by hinting that this looks like a virtual environment and suggesting the
concrete alternative path they should provide. We'll probably want to do
that for some other cases anyway (e.g., `3.13` as described in the
linked issue)

This functionality is also briefly mentioned in
astral-sh/ty#193

Closes astral-sh/ty#318

## Test Plan

e.g.,

```
uv run ty check --python .venv/bin/python3
```

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

Labels

cli Related to the command-line interface ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Python executable paths in --python

5 participants