Commit Graph
2 Commits
Author SHA1 Message Date
Douglas Creager f3b3f3534b [ty] Break apart constraint representation (#28249)
This PR makes a rather large change to how we represent the individual
constraints in a constraint set.

Before, we had a single `Constraint` struct, and assumed that every
individual constraint was a single _range constraint_, which provided a
lower and upper bound for a typevar. Several open workstreams want to
add new kinds of individual constraint. (@ibraheemdev is adding a new
constraint that checks certain gradual bounds; @dcreager is adding a new
constraint that models existentials moar better; @dhruvmanila might need
a new constraint for `ParamSpec`s.)

So, in anticipation of that work, we go ahead and break `Constraint`
apart into an enum, that we can add new variants to in the future. While
we're here, we also move it into a separate submodule to tame the
runaway growth of `constraints.rs`.

We are not adding a single-variant enum. Instead, we have added separate
variants for the different kinds of range constraint that can appear.
Most important, we've broken it apart into separate variants for
only-lower-bound, only-upper-bound, and equivalence bound. That lets us
treat equivalences as a first-class concept, since those are important
to track e.g. for handling the declared constraints of a constrained
typevar. And it gives us an efficiency game in some places, where we had
O(L) lower bounds and O(U) upper bounds, and were having to calculate
O(L×U) different derived sequents from all combinations of those bounds.

We also add separate variants for a "typevar constraint", where one
typevar is being compared directly to another. (So, `T ≤ U`, but not `T
≤ list[U]`.) Those were special-cased in a couple of places, and most
importantly, had an internal representation in `ConstraintSetBuilder`
that was hard to make deterministic. The new representation makes it
easier to identify those special cases, and comes with a deterministic
ordering for free (at least for typevar _range_ constraints).

Given those preliminaries, we also update `SequentMap` so that we
calculate derived sequents globally, outside of any particular
`ConstraintSetBuilder`, and salsa-cache them. That gives us a
performance for certain ecosystem projects, which repeatedly make
comparisons involving complex protocols and `TypedDict` aliases. (Those
are expensive to analyze, and this lets us do that once globally and
share the results across all constraint set solves that use them.)
2026-09-15 14:57:08 -04:00
David Peter 0bac023cd2 [ty] Lockfiles for mdtests with external dependencies (#22077)
## Summary

Add lockfiles for all mdtests which make use of external dependencies.
When running tests normally, we use this lockfile when creating the
temporary venv using `uv sync --locked`. A new
`MDTEST_UPGRADE_LOCKFILES` environment variable is used to switch to a
mode in which those lockfiles can be updated or regenerated. When using
the Python mdtest runner, this environment variable is automatically set
(because we use this command while developing, not to simulate exactly
what happens in CI). A command-line flag is provided to opt out of this.

## Test Plan

### Using the mdtest runner

#### Adding a new test (no lockfile yet)

* Removed `attrs.lock` to simulate this
* Ran `uv run crates/ty_python_semantic/mdtest.py -e external/`. The
lockfile is generated and the test succeeds.

#### Upgrading/downgrading a dependency

* Changed pydantic requirement from `pydantic==2.12.2` to
`pydantic==2.12.5` (also tested with `2.12.0`)
* Ran `uv run crates/ty_python_semantic/mdtest.py -e external/`. The
lockfile is updated and the test succeeds.

### Using cargo

#### Adding a new test (no lockfile yet)

* Removed `attrs.lock` to simulate this
* Ran `MDTEST_EXTERNAL=1 cargo test -p ty_python_semantic --test mdtest
mdtest__external` "naively", which outputs:
> Failed to setup in-memory virtual environment with dependencies:
Lockfile not found at
'/home/shark/ruff/crates/ty_python_semantic/resources/mdtest/external/attrs.lock'.
Run with `MDTEST_UPGRADE_LOCKFILES=1` to generate it.
* Ran `MDTEST_UPGRADE_LOCKFILES=1 MDTEST_EXTERNAL=1 cargo test -p
ty_python_semantic --test mdtest mdtest__external`. The lockfile is
updated and the test succeeds.

#### Upgrading/downgrading a dependency

* Changed pydantic requirement from `pydantic==2.12.2` to
`pydantic==2.12.5` (also tested with `2.12.0`)
* Ran `MDTEST_EXTERNAL=1 cargo test -p ty_python_semantic --test mdtest
mdtest__external` "naively", which outputs a similar error message as
above.
* Ran the command suggested in the error message (`MDTEST_EXTERNAL=1
MDTEST_UPGRADE_LOCKFILES=1 cargo test -p ty_python_semantic --test
mdtest mdtest__external`). The lockfile is updated and the test
succeeds.
2025-12-19 14:29:52 +01:00