Skip to content

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented May 27, 2025

Summary

This PR implements support for the ephemeral environments uv creates when you use the --with argument with uv run. It will only work with a version of uv that includes astral-sh/uv@7bba3d0 (currently you have to build uv with the main branch; it hasn't been included in a release yet).

Fixes astral-sh/ty#320

Test Plan

I added unit tests. I also manually tested the PR by doing the following:

  1. I first built the main branch of uv using cargo build --release in my local uv clone.

  2. I next checked that ty on the main branch was not able to resolve imports if it was invoked with uv run --with=ty. To do this, I:
    a. Applied this diff to the main branch of my local Ruff clone:

    diff --git a/pyproject.toml b/pyproject.toml
    index 0b983b27a0..1bdf25692f 100644
    --- a/pyproject.toml
    +++ b/pyproject.toml
    @@ -3,7 +3,7 @@ requires = ["maturin>=1.0,<2.0"]
     build-backend = "maturin"
    
     [project]
    -name = "ruff"
    +name = "ty"
     version = "0.11.11"
     description = "An extremely fast Python linter and code formatter, written in Rust."
     authors = [{ name = "Astral Software Inc.", email = "hey@astral.sh" }]
    @@ -45,8 +45,8 @@ Changelog = "https://github.com/astral-sh/ruff/blob/main/CHANGELOG.md"
    
     [tool.maturin]
     bindings = "bin"
    -manifest-path = "crates/ruff/Cargo.toml"
    -module-name = "ruff"
    +manifest-path = "crates/ty/Cargo.toml"
    +module-name = "ty"
     python-source = "python"
     strip = true
     exclude = [

    b. cd'd into the local clone of a Python project with third-party dependencies (https://github.com/AlexWaygood/typeshed-stats)
    c. Ran ../uv/target/release/uv run --force-reinstall --with="ty@../ruff" ty check
    d. Observed that ty ran on my project, but was not able to resolve any third-party dependencies, and reported 104 diagnostics

  3. I next checked that this PR branch fixed the issue. I:
    a. Checked out this PR branch
    b. Applied the same pyproject.toml diff to this branch as I applied to the main banch in step (2)
    c. cd'd into my typeshed-stats again
    d. Again ran ../uv/target/release/uv run --force-reinstall --with="ty@../ruff" ty check
    e. Observed that ty was able to resolve all third-party dependencies, and reported only 11 diagnostics

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label May 27, 2025
@AlexWaygood AlexWaygood force-pushed the alex/ephemeral-uv-envs branch from d4f926c to 860ae77 Compare May 27, 2025 17:10
@AlexWaygood AlexWaygood force-pushed the alex/ephemeral-uv-envs branch from 860ae77 to a9d13be Compare May 27, 2025 17:12
@github-actions
Copy link
Contributor

github-actions bot commented May 27, 2025

mypy_primer results

No ecosystem changes detected ✅

@AlexWaygood AlexWaygood marked this pull request as ready for review May 27, 2025 17:49
Comment on lines +290 to +297
Err(err) => {
tracing::warn!(
"Failed to resolve the site-packages directories of this ephemeral uv virtual environment's \
parent environment. Imports will not be resolved correctly if they refer to packages installed \
into the parent environment. Underlying error: {err}"
);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated to your PR but we should make this proper diagnostics and not just tracing warnings (that are easy to miss in the LSP)

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll open a followup issue for this!

@AlexWaygood AlexWaygood force-pushed the alex/ephemeral-uv-envs branch from cf0b387 to 536b241 Compare May 28, 2025 14:19
@AlexWaygood AlexWaygood enabled auto-merge (squash) May 28, 2025 14:21
@AlexWaygood AlexWaygood merged commit a5ebb3f into main May 28, 2025
35 checks passed
@AlexWaygood AlexWaygood deleted the alex/ephemeral-uv-envs branch May 28, 2025 14:55
dcreager added a commit that referenced this pull request May 28, 2025
* main:
  [ty] Support ephemeral uv virtual environments (#18335)
  Add a `ViolationMetadata::rule` method (#18234)
  Return `DiagnosticGuard` from `Checker::report_diagnostic` (#18232)
  [flake8_use_pathlib]: Replace os.symlink with Path.symlink_to (PTH211) (#18337)
  [ty] Support cancellation and retry in the server (#18273)
  [ty] Synthetic function-like callables (#18242)
  [ty] Support publishing diagnostics in the server (#18309)
  Add Autofix for ISC003 (#18256)
  [`pyupgrade`]: new rule UP050 (`useless-class-metaclass-type`) (#18334)
  [pycodestyle] Make `E712` suggestion not assume a context (#18328)
carljm added a commit to MatthewMckee4/ruff that referenced this pull request May 28, 2025
* main: (246 commits)
  [ty] Simplify signature types, use them in `CallableType` (astral-sh#18344)
  [ty] Support ephemeral uv virtual environments (astral-sh#18335)
  Add a `ViolationMetadata::rule` method (astral-sh#18234)
  Return `DiagnosticGuard` from `Checker::report_diagnostic` (astral-sh#18232)
  [flake8_use_pathlib]: Replace os.symlink with Path.symlink_to (PTH211) (astral-sh#18337)
  [ty] Support cancellation and retry in the server (astral-sh#18273)
  [ty] Synthetic function-like callables (astral-sh#18242)
  [ty] Support publishing diagnostics in the server (astral-sh#18309)
  Add Autofix for ISC003 (astral-sh#18256)
  [`pyupgrade`]: new rule UP050 (`useless-class-metaclass-type`) (astral-sh#18334)
  [pycodestyle] Make `E712` suggestion not assume a context (astral-sh#18328)
  put similar dunder-call tests next to each other (astral-sh#18343)
  [ty] Derive `PartialOrd, Ord` for `KnownInstanceType` (astral-sh#18340)
  [ty] Simplify `Type::try_bool()` (astral-sh#18342)
  [ty] Simplify `Type::normalized` slightly (astral-sh#18339)
  [ty] Move arviz off the list of selected primer projects (astral-sh#18336)
  [ty] Add --config-file CLI arg (astral-sh#18083)
  [ty] Tell the user why we inferred a certain Python version when reporting version-specific syntax errors (astral-sh#18295)
  [ty] Implement implicit inheritance from `Generic[]` for PEP-695 generic classes (astral-sh#18283)
  [ty] Add hint if async context manager is used in non-async with statement (astral-sh#18299)
  ...
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.

Project dependencies not detected when using uv run --with ty

4 participants