Commit Graph
3499 Commits
Author SHA1 Message Date
Micha Reiser a9447d7df1 [ty] Use typed arena nodes in semantic and IDE queries
Use parser row IDs for stored AST references. Read the corresponding
borrowed node in inference, reference search and IDE queries.
2026-11-20 16:16:30 +00:00
Micha Reiser b6352c04b1 Use stored argument order in call inference and linting
Check the stored arguments directly when matching a call shape and
reserve class base space from the argument count.
2026-11-20 16:16:30 +00:00
Micha Reiser 504a6b153d Use source-ordered arguments for call checks and rewriting 2026-11-20 16:16:30 +00:00
Micha Reiser 649d08f711 Read source-ordered call arguments once in Ruff and ty 2026-11-20 16:16:30 +00:00
Micha Reiser d6a10a5ac1 Consume source-ordered arguments directly in Ruff and ty 2026-11-20 16:16:30 +00:00
Micha Reiser 462f7ef367 Use the stored call shape for arity and unpacking checks 2026-11-20 16:16:30 +00:00
Micha Reiser 97e1369fc4 Store call arguments in source order
Store positional and keyword arguments in one list for calls and class headers. Source-order visitors can walk the list directly, and evaluation-order visitors visit starred arguments before keywords.

Box keyword values so a positional argument does not carry the full size of a keyword entry.
2026-11-20 16:16:30 +00:00
Micha Reiser d6dfc0920e Consume parameter roles in binding and linter checks 2026-11-20 16:16:30 +00:00
Micha Reiser 0835f5fe98 Consume parameter roles directly in signature checks and fixes 2026-11-20 16:16:30 +00:00
Micha Reiser 35e592cec3 Filter keyword parameters in a single pass 2026-11-20 16:16:30 +00:00
Micha Reiser 4c25f9b780 Consume parameter roles together in the formatter, linter and ty 2026-11-20 16:16:30 +00:00
Micha Reiser 97ac08bf0c Store function and lambda parameters in source order 2026-11-20 16:16:30 +00:00
Micha Reiser f2ab2a7d31 Read class pattern arguments directly for bindings, arity and searches
Expression searches inspect each argument’s pattern in written order.
On recovered syntax this can visit a keyword before a positional pattern.
2026-11-20 16:16:30 +00:00
Micha Reiser a9d8532b29 Store class pattern arguments in source order 2026-11-20 16:16:30 +00:00
Micha Reiser ddbd91227c Visit mapping pattern entries once for captures and searches
For expression searches, test each key followed by its pattern. This lets
the search stop within an entry and avoids a second pass over the list.
2026-11-20 16:16:30 +00:00
Micha Reiser 7a1b37b5d4 Store mapping pattern entries and captures in source order 2026-11-20 15:17:27 +00:00
Micha Reiser 86378a0010 Use dictionary entry children directly in the formatter and rules 2026-11-20 15:17:27 +00:00
Micha Reiser 191c8ba42b Represent dictionary entries as Python AST nodes 2026-11-20 14:28:04 +00:00
Micha Reiser e905eb561c [ty] Fix a panic when reporting invalid type parameter order in unpacked class bases (#30630)
## Summary

Today, ty panics on `class C(*(int, Generic[T, U]))` when `T` has a
default and `U` is required. To locate the error, ty looks for the
second base in the class header. The header contains a single starred
expression that expands to two bases.

This PR changes the diagnostic to use the source expression ty recorded
for each base during expansion, following #24695 and #24699. The
diagnostic now underlines `T, U` and reports that a required type
parameter cannot follow one with a default.

Added an mdtest that checks the diagnostic on the unpacked generic base.
2026-11-20 15:43:20 +01:00
Alex Waygood d8c6e237e7 [ty] Split mismatched-type-name by type constructor (#29957)
## Summary

This PR splits `mismatched-type-name` into eight independently
configurable warnings for `NewType`, functional enums, `TypeVar`,
`ParamSpec`, `TypeVarTuple`, `namedtuple`/`NamedTuple`, `TypedDict`, and
`TypeAliasType`.

~~This PR is actually quite a bit less breaking for users than other
rule splits we've done over the last few days. Because
`mismatched-type-name` is no longer the canonical name for _any_ rule on
this branch, we're able to make it a deprecated alias for _all_ the new
rules, meaning that it will continue to work on the CLI, in ty
configuration files, and ty suppression comments (we'll just emit a
warning diagnostic telling users to upgrade their configuration
file/suppression comment).~~

~~A more specific rule takes precedence over the deprecated name if both
are given within the same configuration table. On the command line, the
last matching flag wins (e.g. `--error=mismatched-enum-name
--ignore=mismatched-type-name` will suppress diagnostics for mismatched
enum names).~~

(After review from @MichaReiser, I backed out these changes. It's now a
somewhat breaking change, because the rule is now marked as "removed"
rather than "deprecated".)

As well as splitting the rule into eight separate rules, it also cleans
up our error recovery for several of these type factories. For example,
in the following snippet, we fail to recognize `T` as a `TypeVar` due to
bad error recovery from the non-literal name. But we don't really need a
nonliteral name in this case to determine that it's a validly
constructed TypeVar. It's probably a mistake, but there's no _soundness_
issue here:

```py
from typing import TypeVar

def f(x: str):
    # main emits an error: `invalid-typevar`
    # this PR emits a warning: `mismatched-typevar-name`
    T = TypeVar(x)

    def g(x: T) -> T:
        return x
    
    reveal_type(g(42))  # main: Todo. This PR: `Literal[42]`.
```

## Motivation

The main motivation here is that having a namedtuple or enum with the
"wrong name", for example, just seems much less "obviously wrong" than
having a typevar or paramspec with the "wrong name". The [ecosystem
report](https://github.com/astral-sh/ruff/pull/29957#issuecomment-6603964612)
is evidence of this. namedtuples and enums define runtime classes that
can be used in runtime contexts and instantiated; these constructs
aren't _just_ for type checkers.
TypeVars/ParamSpecs/TypeVarTuples/NewTypes/TypedDicts are all _only_ for
type checkers and there's no plausible reason why you'd want to give the
type a different name to the variable it's assigned to.

`mismatched-namedtuple-name` and `mismatched-enum-name` are therefore
now disabled by default, matching the default severity we already
settled on for `mistmatched-sentinel-name` in
https://github.com/astral-sh/ruff/pull/27206. `mismatched-typevar-name`,
`mismatched-paramspec-name`, `mismatched-typevartuple-name`,
`mismatched-enum-name`, `mismatched-namedtuple-name` and
`mismatched-newtype-name` all remain enabled by default with `warning`
severity.
2026-11-20 12:41:39 +00:00
Alex Waygood 157cae3c06 [ty] Honor inline ignores on mismatched ParamSpec components (#30595)
When `**kwargs` is annotated with `P.kwargs`, the matching `*args`
annotation must be `P.args`. We emit a diagnostic on `P.args` as a
result -- but if you add a suppression comment to that line... a new
diagnostic pops up on `P.kwargs`! That's not how suppression comments
are really meant to work. This PR fixes that.

## Test plan

### Before (`aa46aae60`)

Adding `# ty: ignore[invalid-paramspec]` on the `*args` line moves the
diagnostic to `**kwargs`.


https://github.com/user-attachments/assets/50ee1858-8126-45bb-896f-2517b98950fe

### After (`684c14273`)

Adding the same ignore clears the diagnostic.



https://github.com/user-attachments/assets/0a7fc2b0-ffd0-49b4-87ae-714835b7421f
2026-11-20 12:13:58 +00:00
Charlie MarshandCharlie Marsh d1da20cd89 [ty] Use control flow checkpoints for statement calls (#30561)
## Summary

When a generated UI method calls hundreds of setters, ty walks back
through earlier calls to check whether any of them would prevent
execution from reaching the current statement. We cache an intermediate
result every 256 calls. Between checkpoints we can walk the same calls
many times, doing a binary search at each step to see whether we've
reached a checkpoint.

We now select checkpoints for calls using the same rule we use for other
control flow. After walking eight predicates, we look for a checkpoint
based on the predicate ID. Nearby statements can reuse a cached result
after a shorter walk, and we can drop the search through the list of
calls. The closer spacing can retain more Salsa memos in long methods.

Cold checks of Shuidi’s generated PyQt6 form used 16.12% fewer
instructions. Checking all 89 Python files in Nugget used 7.60–7.76%
fewer instructions. [Shuidi
revision](https://github.com/MAOGE555/shuidi/blob/6de90e8b9a939c9e57cb2d4f2f2a965668f772f8/new.py)
· [Nugget
revision](https://github.com/leminlimez/Nugget/tree/26e0c50ec114ca3a4ff6ab2fb4ae309abfd39fa1)

Median paired peak RSS rose by 742 KiB on Shuidi. On Nugget the median
increase of 1,014 KiB was within the variation between baseline runs.

| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/248231e9936ecc0e44ecd3a69bc07899736e4e23)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/ca233b092f1897fbfd658ced1be292c9a38e293d)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| 3,179,208,188.5 | 3,160,468,835.5 | -0.59% | -0.71% to -0.47% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
(inconclusive) | 8,234,941,180 | 8,248,831,934 | +0.13% | -0.02% to
+0.17% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
(inconclusive) | 160,010,941,358 | 160,033,050,064 | +0.01% | 0.00% to
+0.02% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| 1,841,091,500 | 1,837,145,491 | -0.21% | -0.22% to -0.21% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,930,535,516.5 | 1,921,623,158 | -0.46% | -0.87% to
-0.06% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
(inconclusive) | 30,072,961,014.5 | 30,071,468,706 | 0.00% | -0.03% to
+0.02% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,923,102,449 | 2,931,714,627 | +0.29% | 0.00% to
+0.42% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| 4,896,852,662.5 | 4,894,400,733 | -0.05% | -0.07% to -0.03% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 98,352,722,060.5 | 97,951,489,525 | -0.41% | -0.43% to -0.38% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,665,191,727 | 1,665,699,910 | 0.00% | -0.02% to
+0.05% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,532,304,774 | 8,508,863,575.5 | -0.27% | -0.30% to
-0.25% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
(inconclusive) | 2,965,330,722.5 | 2,960,677,073.5 | -0.16% | -0.29% to
-0.03% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 21,783,704,471 | 21,796,835,567.5 | +0.06% | -0.06% to
+0.18% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,711,695,060.5 | 2,712,229,180.5 | +0.02% | -0.29% to
+0.33% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
(inconclusive) | 13,995,374,688.5 | 13,990,397,766 | -0.04% | -0.11% to
+0.04% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
(inconclusive) | 46,802,092,756.5 | 46,798,119,486.5 | -0.01% | -0.02%
to +0.01% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 15,977,459,855 | 15,987,345,132 | +0.07% | +0.06% to +0.10% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 8,824,268,687 | 8,832,404,265 | +0.07% | +0.07% to +0.25% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
(inconclusive) | 4,150,396,345.5 | 4,143,422,104.5 | -0.17% | -0.28% to
-0.06% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| 14,571,156,422.5 | 14,507,603,093.5 | -0.44% | -0.44% to -0.43% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
(inconclusive) | 3,001,834,506 | 3,003,583,249 | +0.06% | -0.06% to
+0.17% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
(inconclusive) | 4,828,355,423.5 | 4,826,417,677 | -0.04% | -0.09% to
+0.01% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
(inconclusive) | 35,978,090,519 | 35,948,401,773 | -0.08% | -0.13% to
-0.03% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
(inconclusive) | 16,468,944,617.5 | 16,445,025,380 | -0.15% | -0.22% to
-0.08% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| 13,821,617,408 | 13,840,973,032 | +0.14% | +0.12% to +0.17% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
(inconclusive) | 75,190,375,068 | 75,148,405,537.5 | -0.06% | -0.14% to
+0.03% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
(inconclusive) | 8,980,074,016.5 | 8,973,542,688.5 | -0.07% | -0.13% to
-0.01% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
(inconclusive) | 105,060,132,657.5 | 105,002,927,918 | -0.05% | -0.08%
to -0.03% |

---------

Co-authored-by: Charlie Marsh <charliemarsh@openai.com>
2026-11-20 07:09:44 -05:00
Carl Meyer cbcf866379 [ty] Preserve type variable constraints through identity aliases (#30590)
When a generic function expects a callback that returns `Id[R]`, where
`type Id[T] = T`, ty can lose the relationship between `R` and a type
variable from the caller.

```py
from collections.abc import Callable
from typing import reveal_type

type Id[T] = T

def run[R](callback: Callable[[], Id[R]]) -> R:
    return callback()

def check[T](callback: Callable[[], T]):
    reveal_type(run(callback))  # Before: Unknown. After: T@check.
```

When an alias bound expands to a type variable, record the relationship
between the two variables. In this example, the solver can infer `R`
from the callback's return type `T`.

This is just an edge case Codex noticed, not something that showed up in
real code, so it's quite low priority; it just seemed like a very simple
and clearly correct fix.
2026-11-20 09:17:34 +01:00
Ibraheem Ahmed c8ba04dc06 [ty] Memoize more argument assignability checks (#30483)
Memoize some more hot-path argument assignability checks with Salsa.
2026-11-19 21:32:27 -05:00
Alex Waygood aa46aae607 [ty] Honor shadowed-type-variable independently for nested classes (#30588)
Disabling `invalid-generic-class` currently also erroneously suppresses
`shadowed-type-variable` diagnostics on nested classes, even when
`shadowed-type-variable` is enabled. With this fix,
`shadowed-type-variable` controls those diagnostics independently.

## Test plan

Save this example as `example.py`.

```python
class Outer[T]:
    class Inner[T]:
        pass
```

Run `ty check --python-version 3.12 --ignore invalid-generic-class
--error shadowed-type-variable example.py` with a build of ty from each
branch. Before the fix, ty reports no diagnostics. With the fix, it
reports `shadowed-type-variable` for `Inner`’s `T`. Replacing `--error
shadowed-type-variable` with `--ignore shadowed-type-variable`
suppresses the diagnostic.
2026-11-19 20:23:56 +00:00
Charlie Marsh bcd21d9524 [ty] Skip assignability queries from string and bytes literals to their builtin types (#30556)
## Summary

When ty checks `names: list[str] = ["a", "b"]`, it interns a pair of
types and caches an assignability result for each distinct string. The
relation checker always accepts a string literal where `str` is
expected. We now return that answer before creating the cache key, and
do the same for bytes literals where `bytes` is expected.

On the emoji table below, ty’s memory report shows that the number of
cached assignability results fell from 5,186 to zero. We measured the
change in instructions for a cold `ty check` with Cachegrind, running
each pair in both orders.

| Workload | Base first | PR first |
| --- | ---: | ---: |
| [transformers-ci emoji table, one
file](https://github.com/huggingface/transformers-ci/blob/c740ae2df90c21aaa72b80a79cd5a106cc589912/src/transformersci/_emoji_shortcodes.py)
| −9.51% | −9.64% |
| [Faker Chinese SSN provider, one
file](https://github.com/joke2k/faker/blob/affa2c5aada23fc5c00127298a8e904375d32832/faker/providers/ssn/zh_CN/__init__.py)
| −5.63% | −5.65% |
| [Packaging source
tree](https://github.com/pypa/packaging/tree/929fd4b1410ac7ef61ef3f45b2f5d7e87711a9b5/src/packaging)
| −0.26% | −0.77% |
| [Jinja source
tree](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044/src/jinja2)
| −0.56% | +0.34% |

| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/248231e9936ecc0e44ecd3a69bc07899736e4e23)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/1d21e19ae5d7ca40a5cfbacf0c43cbbd276f8c5c)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
(inconclusive) | 3,173,151,578 | 3,175,888,000 | +0.09% | -0.05% to
+0.22% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
(inconclusive) | 8,239,711,483.5 | 8,239,440,162.5 | 0.00% | -0.05% to
+0.05% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
(inconclusive) | 160,008,739,827 | 159,978,023,809.5 | -0.02% | -0.03%
to -0.01% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
(inconclusive) | 1,839,699,389.5 | 1,839,905,941.5 | +0.02% | -0.14% to
+0.14% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,928,546,886.5 | 1,924,975,857.5 | -0.18% | -0.62% to
+0.25% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
(inconclusive) | 30,051,485,397.5 | 30,075,559,912 | +0.08% | +0.02% to
+0.14% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,928,928,527 | 2,925,684,756.5 | -0.11% | -0.20% to
-0.02% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
(inconclusive) | 4,897,331,183 | 4,895,390,760 | -0.04% | -0.06% to
-0.02% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
(inconclusive) | 98,277,500,067 | 98,234,791,522.5 | -0.04% | -0.06% to
-0.02% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,665,231,258 | 1,664,963,316.5 | -0.02% | -0.05% to
+0.01% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,499,333,648 | 8,580,075,092.5 | +1.19% | +0.26% to
+2.19% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
(inconclusive) | 2,966,282,343.5 | 2,965,691,453 | -0.02% | -0.45% to
+0.42% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 21,762,181,516.5 | 21,790,127,170.5 | +0.13% | -0.12%
to +0.37% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,708,444,565.5 | 2,706,033,376 | -0.09% | -0.15% to
-0.03% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
(inconclusive) | 14,005,150,628 | 14,001,213,219.5 | -0.03% | -0.21% to
+0.15% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
(inconclusive) | 46,783,816,013 | 46,804,695,220 | +0.05% | -0.05% to
+0.07% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
(inconclusive) | 15,975,096,807 | 15,976,682,707 | +0.01% | -0.03% to
+0.05% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
(inconclusive) | 8,830,018,805.5 | 8,828,577,298 | -0.02% | -0.05% to
+0.02% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
(inconclusive) | 4,151,483,329.5 | 4,148,222,628 | -0.08% | -0.14% to
-0.01% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| 14,576,818,999 | 14,588,029,367.5 | +0.08% | +0.06% to +0.09% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
(inconclusive) | 3,005,682,538.5 | 3,006,104,157 | +0.01% | 0.00% to
+0.03% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
(inconclusive) | 4,821,578,278.5 | 4,824,210,310.5 | +0.05% | -0.02% to
+0.13% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
(inconclusive) | 35,975,691,969.5 | 35,969,789,129.5 | -0.02% | -0.03%
to 0.00% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| 16,474,226,042 | 16,467,508,218 | -0.04% | -0.05% to -0.03% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
(inconclusive) | 13,828,543,728.5 | 13,829,204,898 | 0.00% | -0.01% to
+0.02% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
(inconclusive) | 75,182,874,115 | 75,167,868,298 | -0.02% | -0.03% to
-0.01% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| 8,989,371,245 | 8,972,410,908 | -0.19% | -0.21% to -0.16% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
(inconclusive) | 105,046,845,230.5 | 105,049,287,974 | 0.00% | -0.01% to
+0.01% |
2026-11-19 15:07:21 -05:00
Charlie MarshandCharlie Marsh ad6a3af804 [ty] Avoid checking identical mutable TypedDict field types twice (#30554)
## Summary

When comparing two `TypedDict`s, ty checks their mutable field types in
both directions so that both reads and writes are safe. For example,
Pydantic’s `SetSchema` and `FrozenSetSchema` both declare `items_schema:
CoreSchema`. Checking that field in both directions can make ty walk the
same recursive union twice. We now reuse the first result when the field
types are identical and the check succeeds unconditionally.

| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/248231e9936ecc0e44ecd3a69bc07899736e4e23)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/8070ac3923ca5a0ad0e466d3e79e777173490984)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
(inconclusive) | 3,173,151,578 | 3,168,898,476 | -0.13% | -0.48% to
+0.21% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
(inconclusive) | 8,239,711,483.5 | 8,251,530,324.5 | +0.14% | +0.12% to
+0.17% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
(inconclusive) | 160,008,739,827 | 159,998,135,483.5 | -0.01% | -0.01%
to -0.01% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| 1,838,176,557 | 1,840,563,359.5 | +0.13% | +0.10% to +0.16% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,928,546,886.5 | 1,928,575,227.5 | 0.00% | -0.53% to
+0.54% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
(inconclusive) | 30,051,485,397.5 | 30,079,114,268.5 | +0.09% | +0.07%
to +0.11% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,928,928,527 | 2,926,333,550.5 | -0.09% | -0.14% to
-0.03% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
(inconclusive) | 4,897,331,183 | 4,893,387,171 | -0.08% | -0.10% to
-0.06% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
(inconclusive) | 98,277,500,067 | 98,270,332,282 | -0.01% | -0.07% to
+0.06% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,665,231,258 | 1,665,643,636.5 | +0.02% | -0.02% to
+0.07% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,543,578,111 | 8,520,457,749.5 | -0.24% | -0.75% to
+1.09% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
(inconclusive) | 2,966,282,343.5 | 2,964,321,512.5 | -0.07% | -0.11% to
-0.03% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 21,762,181,516.5 | 21,754,343,170 | -0.04% | -0.09% to
+0.02% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,708,444,565.5 | 2,704,362,913.5 | -0.15% | -0.21% to
-0.09% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
(inconclusive) | 14,005,150,628 | 14,000,520,608.5 | -0.03% | -0.25% to
+0.18% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
(inconclusive) | 46,783,031,518 | 46,820,580,379 | +0.08% | 0.00% to
+0.16% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
(inconclusive) | 15,975,096,807 | 15,974,327,186.5 | 0.00% | -0.06% to
+0.05% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
(inconclusive) | 8,830,018,805.5 | 8,833,628,682 | +0.04% | -0.07% to
+0.16% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
(inconclusive) | 4,148,325,664.5 | 4,149,698,890.5 | +0.03% | -0.07% to
+0.13% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
(inconclusive) | 14,576,818,999 | 14,579,604,714.5 | +0.02% | -0.05% to
+0.09% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
(inconclusive) | 3,005,682,538.5 | 3,005,214,413 | -0.02% | -0.06% to
+0.03% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
(inconclusive) | 4,821,578,278.5 | 4,822,305,196 | +0.02% | -0.04% to
+0.07% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
(inconclusive) | 35,975,691,969.5 | 35,974,979,610 | 0.00% | -0.03% to
+0.02% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| 16,474,226,042 | 16,466,653,505.5 | -0.05% | -0.07% to -0.02% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| 13,823,114,256 | 12,495,730,368.5 | -9.60% | -9.64% to -9.56% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
(inconclusive) | 75,182,874,115 | 75,200,496,899 | +0.02% | -0.06% to
+0.11% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
(inconclusive) | 8,989,371,245 | 8,991,509,760.5 | +0.02% | 0.00% to
+0.05% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
(inconclusive) | 105,046,845,230.5 | 105,055,906,168.5 | +0.01% | -0.04%
to +0.05% |

Co-authored-by: Charlie Marsh <charliemarsh@openai.com>
2026-11-19 15:06:27 -05:00
Charlie Marsh 0cbf85880e [ty] Collect each reachable constraint once in source order (#30564)
## Summary

Before walking a constraint diagram, ty collects the constraint from
each node, sorts the list by source order, and examines each
constraint’s bound. Nodes with different successors can test the same
constraint. While checking Bokeh’s `formatters.py`, one sort received
7,340,031 entries for 46 distinct constraints. [Bokeh
example](https://github.com/astral-sh/ty/issues/5063)

We now record each reachable constraint at its source-order index as we
visit the diagram. Reading the filled positions gives us each constraint
once, in the order used to derive relationships between pairs of
constraints. We can skip the sort and examine each bound once per
constraint.

On a cold check of Bokeh’s `src` and `tools/release` (492 files),
Cachegrind counted 10.41% and 10.48% fewer instructions in opposite
orders. Two adjacent runs of the head differed by 0.056%. Both builds
produced the same 686 diagnostics. [Pinned Bokeh
revision](https://github.com/bokeh/bokeh/tree/378373772846cdfedc1c641ce9219dcbc34b6b9c)

| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/248231e9936ecc0e44ecd3a69bc07899736e4e23)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/cbd4c9800ec05fd53edef1fe505c811f1aae547e)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
(inconclusive) | 3,171,706,977.5 | 3,173,312,393.5 | +0.05% | +0.04% to
+0.06% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
(inconclusive) | 8,234,158,986 | 8,241,164,549 | +0.03% | +0.01% to
+0.21% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
(inconclusive) | 1,840,391,653 | 1,838,657,018 | -0.09% | -0.12% to
-0.07% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,929,921,234 | 1,931,822,739 | +0.10% | -0.03% to
+0.23% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
(inconclusive) | 30,082,819,307 | 30,059,978,903 | -0.08% | -0.11% to
-0.04% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,924,591,569 | 2,923,498,040.5 | -0.04% | -0.12% to
+0.05% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
(inconclusive) | 4,894,335,305 | 4,894,911,011.5 | +0.01% | -0.02% to
+0.04% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
(inconclusive) | 98,352,950,073.5 | 98,338,876,450.5 | -0.01% | -0.06%
to +0.03% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,665,396,087 | 1,665,423,664.5 | 0.00% | -0.02% to
+0.03% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,533,055,257 | 8,550,832,351 | +0.21% | -0.29% to
+0.71% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
(inconclusive) | 2,957,070,580.5 | 2,966,308,391 | +0.31% | +0.12% to
+0.51% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 21,790,076,583 | 21,800,899,655.5 | +0.05% | -0.17% to
+0.27% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,713,637,106.5 | 2,708,034,423 | -0.21% | -0.46% to
+0.05% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
(inconclusive) | 46,794,398,039.5 | 46,804,980,842.5 | +0.02% | 0.00% to
+0.05% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
(inconclusive) | 15,977,923,329 | 15,984,212,294 | +0.04% | -0.04% to
+0.12% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
(inconclusive) | 8,837,889,051.5 | 8,837,673,785.5 | 0.00% | 0.00% to
0.00% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| 4,149,082,443 | 4,153,649,153.5 | +0.11% | +0.07% to +0.15% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| 3,006,251,725 | 3,004,144,737.5 | -0.07% | -0.09% to -0.05% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
| 4,831,230,636.5 | 4,825,843,035.5 | -0.11% | -0.16% to -0.06% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 35,973,468,066 | 35,980,669,260.5 | +0.02% | +0.01% to +0.03% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
(inconclusive) | 16,467,121,598.5 | 16,464,743,699.5 | -0.01% | -0.05%
to +0.02% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| 13,830,350,467.5 | 13,841,355,668 | +0.08% | +0.04% to +0.11% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
(inconclusive) | 75,188,876,981.5 | 75,162,033,677 | -0.04% | -0.05% to
-0.02% |
2026-11-19 12:09:06 -05:00
astral-automations-bot[bot]andBrent Westbrook a7ba68b491 Bump version to 0.17.6 (#30578)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
2026-11-19 10:32:47 -05:00
Carl Meyer 3067e15977 [ty] Fix lambda parameter inference with an unsolved ParamSpec (#30547)
## Summary

In this code on ty 0.0.107, `wrap(lambda n: n + 1)` alone works fine,
but passing that same result straight to `register` makes ty report an
error on `n + 1`.

```python
from collections.abc import Callable

def wrap[**P](f: Callable[P, int]) -> Callable[P, int]:
    return f

def register[**P](f: Callable[P, int]) -> None: ...

wrap(lambda n: n + 1)            # OK
register(wrap(lambda n: n + 1))  # Error in ty 0.0.107
```

`P` stands for all the parameters of the callback. Neither `wrap` nor
`register` says what type `n` has. In the nested call, we wrongly gave
`n` the type of the whole parameter list, `(*args: object, **kwargs:
object)`, and then rejected the addition.

With this fix, `n` stays `Unknown` in both calls. If the callback
signature does give a concrete type for a parameter, ty still uses it.

Fixes astral-sh/ty#5075.
2026-11-19 07:15:48 -08:00
Shivansh ShuklaandAlex Waygood 7aae7a5312 Treat relative imports from .__future__ as ordinary imports (#30260)
Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
2026-11-19 15:03:01 +00:00
Alex Waygood 700c1e8424 [ty] Move NamedTupleLike to ty_extensions._internal (#30567) 2026-11-19 12:51:10 +00:00
Carl Meyer 46c8e45ab5 [ty] Clarify gradual callable assignability tests (#30543)
Address post-land review comments on #30509.
2026-11-19 08:48:40 +01:00
Carl Meyer 3e94fa78fd [ty] Preserve argument inference for unannotated overload parameters (#30549)
ty could infer `Unknown` for a call when one overload left a parameter
unannotated and another supplied a type for it. In this example,
`first()` has no arguments to infer `T` from, so its return type
defaults to `int` unless the caller supplies an expected type.

```python
from typing import overload, reveal_type
from typing_extensions import Never, TypeVar

T = TypeVar("T", default=int)

def first(*values: T) -> T:
    return values[0]

@overload
def choose(value, index: int) -> int: ...
@overload
def choose(value: Never, index: object) -> object: ...
def choose(value, index):
    return index

reveal_type(choose(first(), 1))  # previously Unknown, now int
```

Both overloads initially match. The first gives `first()` no expected
type, so it infers `int`. The second infers `Never` for the same
expression. Previously, ty reused `Never` while checking the first
overload and treated the call as ambiguous. It now uses the argument
type inferred for each overload, so the first overload wins.

## Test plan

Checked the original example with a nested `create()` whose return type
is a type variable bounded by `int`. ty reveals `int` for
`choose(create(), 1)`, including when `create()` is replaced by
`next(iter([]))`.
2026-11-18 20:32:53 -08:00
Charlie Marsh 0528644c0d [ty] Skip enum-complement recognition without exclusions (#30539)
## Summary

An enum complement represents an enum with one or more members excluded.
When an intersection contains only positive types, ty resolves aliases
and looks up enum metadata before it can rule out that representation.
We now return as soon as we see that the intersection excludes nothing.

| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/5203c350a703ece7092d8ef33b51f0878efe56d7)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/f8c20b44a7f94ad56bc7bf6cc38955c864834567)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
(inconclusive) | 3,169,252,588.5 | 3,165,880,256 | -0.11% | -0.24% to
+0.03% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
(inconclusive) | 8,244,187,955 | 8,233,671,915 | -0.13% | -0.25% to
0.00% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
(inconclusive) | 13,984,635,621 | 13,967,593,043 | -0.12% | -0.26% to
+0.01% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
(inconclusive) | 2,970,043,385 | 2,964,955,556.5 | -0.17% | -0.36% to
+0.02% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| 1,668,314,018.5 | 1,666,115,454.5 | -0.13% | -0.22% to -0.04% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
(inconclusive) | 1,842,386,237 | 1,842,311,269 | 0.00% | -0.03% to
+0.02% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,921,913,908 | 1,922,481,069.5 | +0.03% | -0.07% to
+0.13% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,540,711,202.5 | 8,508,536,779.5 | -0.44% | -0.70% to
+0.44% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
(inconclusive) | 15,950,314,544 | 15,949,645,524.5 | 0.00% | -0.03% to
+0.03% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
(inconclusive) | 30,077,107,284.5 | 30,054,346,725 | -0.08% | -0.08% to
-0.07% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,924,414,939 | 2,924,205,357 | -0.01% | -0.16% to
+0.04% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
(inconclusive) | 4,890,802,299 | 4,888,655,706.5 | -0.04% | -0.10% to
+0.02% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
(inconclusive) | 8,838,095,226.5 | 8,824,778,889.5 | -0.15% | -0.17% to
-0.13% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 98,130,124,186.5 | 97,988,884,182.5 | -0.14% | -0.16% to -0.13% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,716,917,706 | 2,715,071,981 | +0.16% | -0.10% to
+0.20% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 21,758,498,756 | 21,758,137,452 | 0.00% | -0.02% to
+0.02% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
(load-sensitive) | 46,766,161,702.5 | 46,705,024,120 | -0.13% | -0.16%
to -0.10% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
(inconclusive) | 4,146,476,878.5 | 4,146,717,063.5 | +0.01% | -0.04% to
+0.05% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
(inconclusive) | 14,584,417,608 | 14,573,009,928 | -0.08% | -0.08% to
-0.08% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
(inconclusive) | 2,999,038,100.5 | 2,998,375,245.5 | -0.02% | -0.06% to
+0.01% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
(inconclusive) | 4,824,919,019.5 | 4,814,929,659 | -0.21% | -0.29% to
-0.12% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 35,885,235,003.5 | 35,849,258,989.5 | -0.10% | -0.10% to -0.10% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
(inconclusive) | 16,439,632,022 | 16,430,069,160.5 | -0.06% | -0.08% to
-0.04% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
(inconclusive) | 13,868,047,966 | 13,867,703,384.5 | 0.00% | -0.07% to
+0.07% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| 75,296,392,345 | 75,035,880,330.5 | -0.35% | -0.40% to -0.29% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
(inconclusive) | 8,969,239,350.5 | 8,971,986,996 | +0.03% | -0.11% to
+0.17% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
(inconclusive) | 104,942,117,909 | 104,912,773,105 | -0.03% | -0.14% to
+0.09% |
2026-11-18 15:20:47 -05:00
Carl Meyer e1f6bf0d63 [ty] Fix a hang when specializing generic constructors (#30510)
On main, we hang on this snippet:

```python
class Property[T]:
    value: T

class Either[T](Property[T]):
    def __init__[A, B, C](
        self: "Either[A | B | C]",
        first: type[Property[A]],
        second: type[Property[B]],
        third: type[Property[C]],
    ) -> None: ...

result = Either(Property[float], Property[str | None], Property[bytes])
```

The three arguments determine `A`, `B`, and `C`, so `result` should have
type `Either[float | str | None | bytes]`. Previously, we kept
rebuilding the same union in different orders, treating each order as a
new result, and cycling forever.

ty now substitutes one type variable at a time. It lets other bindings
resolve a cycle before choosing a fixed point or falling back to
`Unknown`. When two variables refer back to each other, ty also checks
their bounds. For example, a constructor’s `int` bound still applies
when the class parameter itself is unbounded.

Fixes astral-sh/ty#5057

I tried all six orders of `A`, `B`, and `C` in the `__init__`
declaration above. With the call unchanged, ty inferred `Either[float |
str | None | bytes]` every time.

## Ecosystem report

Bokeh finished in 20 seconds after timing out at 180 seconds on the
base. Across five other projects, 58 existing diagnostics changed which
unresolved type variables ty displayed. Both revisions rejected the same
operations, and typed array inputs retained their concrete dtype. No
stable diagnostics were added or removed. [Full report for
`baf1faa7`](https://github.com/astral-sh/ruff/pull/30510#issuecomment-6965510325)
2026-11-18 11:58:08 -08:00
Douglas Creager 18c2f5d1a9 [ty] Reject union narrowing attempts with failed nested calls (#30217)
When inferring a specialization for a generic call, we try each element
of a union return context individually before falling back to the entire
union. If a nested call fails during one of those attempts, its error
recovery type can make the proposed specialization appear valid even
though it isn't.

There is an open constraint solver bug (astral-sh/ty#4696) where we
don't verify that all solutions collectively "cover" all of the valid
specializations of any _non-inferable_ typevars. This can cause us to
infer a provisional specialization that is incompatible with the actual
arguments of a nested call. We then incorrectly accept the narrowed
specialization because the nested call's recovery type satisfies the
expected parameter type.

For now, we work around this by recording when call binding fails, and
threading that information through `InferContext` so that `try_narrow`
can reject speculative inference attempts that depend on error recovery.
We track these failures even when their diagnostics are suppressed, but
ignore failures in unreachable code. For calls with a single candidate
signature, we also allow narrowing to succeed when the failed argument
corresponds to a parameter whose type contains no typevars, since that
argument cannot influence the proposed specialization.

There are still limitations around callable unions and overloads, which
#27942 will address.

Closes astral-sh/ty#4971
2026-11-18 11:27:25 -05:00
Micha Reiser b6d8e21241 [ty] Add project-manager.script = "uv" (#30489)
## Summary

This PR adds a new `project-manager` table:

```toml
[project-manager]
project = "uv" | "auto" | "none"
project-mode = "sync" | "metadata"
script = "uv" | "none"
```

This PR only implements the script part, but I listed all options to
outline where we want to go with this in the future.

`script = "uv"` opts in to installing PEP 723 script dependencies with
uv (to reduce the need for `TY_UV`). For now, `project-manager.script`
defaults to `"none"`, but I plan to change this to `"uv"` in the near
future. The new option follows the same configuration precedence as
other script settings:

* A script with its own `tool.ty` section uses the value from its own
table to control whether it uses uv to sync its dependencies or not.
* Scripts without a `tool.ty` section use the project level
`project-manager.script`

For now, overriding the setting based on file patterns isn't supported
(I have no strong reason against it, it's mainly to cut scope).

`TY_UV` takes precedence over any setting in the configuration (similar
to other CLI options that support environment variables).

The recommended way to check untrusted code is to set `TY_UV=off`, which
overrides `project-manager.script`.

For now, we keep supporting `experimental.useUv` for scripts, but the
in-editor `configuration.project-manager.script` takes precedence over
`experimental.useUv`.

## Why is the setting not under `environment.

For projects, setting `project-manager.project = "metadata"` also
changes how we discover the project (we prefer the workspace over the
closest ty project), and `metadata` doesn't initialize the Python
environment.

## LSP watch mode

Changing `project-manager.script` at the project level requires saving
any open script (or closing and reopening) for the sync to start. This
simplified the implementation a fair bit and seems an okay trade off.
2026-11-18 12:42:42 +01:00
Carl Meyer c92a47546c [ty] Preserve fixed parameters when comparing gradual callables (#30509)
The "gradual parameters" `*args: Any, **kwargs: Any` are special-cased
to match any possible set of arguments, even when they follow other
"normal" parameters in a signature. But they do not make the _entire_
signature gradual -- calls still must satisfy the other normal
parameters present.

We currently are "too gradual" in our treatment of these signatures,
allowing a callable with an initial typed parameter plus gradual
parameters to be assigned to a callable type that has the wrong type for
its first parameter:

```python
from typing import Any, Callable

def lowercase(value: str, *args: Any, **kwargs: Any) -> str:
    return value.lower()

callback: Callable[[int], str] = lowercase  # Previously accepted by ty
callback(1)  # AttributeError
```

This PR rewrites callable assignability (with a significant net
reduction in Rust LOC!) and now correctly reports the error on this
assignment. The first positional argument binds to `value`, which
requires a `str`. The gradual `*args` and `**kwargs` only describe the
remaining arguments.

Closes astral-sh/ty#5052.

## Ecosystem report

All 104 public ecosystem changes on `8681ef368f` are expected. Most new
errors identify callbacks or overrides that cannot accept an argument
their types promise. [Full ecosystem
report](https://github.com/astral-sh/ruff/pull/30509#issuecomment-6966654399)
2026-11-18 12:27:53 +01:00
David Peter b0015ad905 [ty] Fix variance inference for frozen Pydantic models on 3.12 and earlier (#30452)
## Summary

Fixes astral-sh/ty#5035.

When targeting Python 3.12 or earlier, infer covariance for read-only
fields on Pydantic models frozen through a class keyword or
`ConfigDict`. Use the same setter lookup as assignment checking so a
custom `__setattr__` keeps the model invariant. On Python 3.13 and
later, the synthesized `__replace__` still makes these models invariant.
2026-11-18 11:06:08 +01:00
Charlie Marsh f4f75dd70e [ty] Handle TypedDict union pairs before other simplifications (#30516)
## Summary

When building a union, ty already drops equal `TypedDict` identities and
keeps distinct ones. We now make that same decision before trying union
simplifications whose shapes cannot match a pair of `TypedDict`s.

<!-- ty-pinned-project-instructions:start -->

| Project | Base instructions | PR instructions | Change | Paired range
|
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| 3,168,443,716 | 3,170,270,249 | +0.06% | -0.09% to +0.21% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| 8,243,563,963 | 8,222,116,502.5 | -0.26% | -0.30% to -0.22% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| 13,967,044,319 | 13,952,000,490 | -0.11% | -0.12% to -0.10% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| 3,015,835,881 | 2,973,036,387.5 | -1.42% | -1.44% to -1.40% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| 1,674,254,024.5 | 1,669,154,693 | -0.30% | -0.59% to -0.01% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| 1,840,760,352 | 1,840,617,253 | -0.01% | -0.02% to -0.00% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
| 1,918,904,014 | 1,928,471,948 | +0.50% | +0.08% to +0.92% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 15,959,170,358.5 | 15,941,375,574.5 | -0.11% | -0.13% to -0.09% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 30,035,957,211.5 | 30,012,775,625.5 | -0.08% | -0.09% to -0.07% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
| 2,921,034,266.5 | 2,926,554,731 | +0.19% | +0.06% to +0.32% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| 4,894,489,657 | 4,894,340,186 | -0.00% | -0.01% to +0.01% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 8,841,145,231 | 8,833,881,159 | -0.08% | -0.11% to -0.06% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
| 2,704,118,065 | 2,707,355,951 | +0.12% | -0.12% to +0.36% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| 21,737,033,782.5 | 21,720,847,925 | -0.07% | -0.10% to -0.05% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
| 4,145,976,337.5 | 4,151,133,331 | +0.12% | -0.02% to +0.27% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| 14,549,212,075.5 | 14,544,351,206 | -0.03% | -0.08% to +0.01% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| 3,007,532,033.5 | 3,009,551,370.5 | +0.07% | -0.01% to +0.15% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
| 4,831,950,023.5 | 4,835,710,164.5 | +0.08% | +0.07% to +0.09% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 35,657,484,032 | 35,662,007,811.5 | +0.01% | -0.00% to +0.03% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
| 14,369,578,333.5 | 13,852,874,484.5 | -3.60% | -3.69% to -3.51% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| 75,144,682,617.5 | 75,109,650,224 | -0.05% | -0.11% to +0.02% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| 8,971,613,976 | 8,951,751,970 | -0.22% | -0.23% to -0.21% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
| 104,905,657,814 | 104,906,429,408 | +0.00% | -0.02% to +0.02% |

<!-- ty-pinned-project-instructions:end -->
2026-11-17 22:09:21 -05:00
Carl Meyer d84471e739 [ty] Preserve compatible overloads when partially binding a union argument (#30446)
On main, `functools.partial` rejects a union-valued keyword when
different overloads accept its members and a required argument is left
for the caller. For example, ty accepts binding `option="text"` or
`option=None` separately, but reports `Expected str, found str | None`
here.

```python
from functools import partial
from typing import overload

@overload
def f(value: int, /, *, option: str) -> str: ...
@overload
def f(value: int, /, *, option: None) -> int: ...
def f(value: int, /, *, option: str | None) -> str | int:
    return value if option is None else option

def apply(option: str | None) -> str | int:
    bound = partial(f, option=option)
    return bound(1)
```

When overload resolution retries with each union member, it matches
parameters again. This used to eliminate both overloads because `value`
was missing. The fix defers the missing-argument check on every match
and keeps all compatible overloads for the eventual call. In the
example, ty infers `str | int` for `bound(1)` because the result depends
on the stored keyword. An explicit override selects the corresponding
overload, so `bound(1, option=None)` returns `int`.

For an overloaded function whose two-positional-argument form requires
`option=None`, a partial `p` that stores an `option` of type `str |
None` rejects `p(1, 2)`. It accepts `p(1, 2, option=None)` because the
caller replaces the stored value.

## Test plan

Checked `partial(datetime.replace, tzinfo=tz)` in Pydantic with the
proposed typeshed sync from #30443, which splits `tzinfo` and `None`
into separate overloads. The fix removed the diagnostic on this call. A
direct check confirmed that the partial accepts a datetime receiver and
a `tzinfo=None` override, and rejects a missing or integer receiver.
[Pydantic call
site](https://github.com/pydantic/pydantic/blob/560f0f96a37d6913afdfddb2de22030ef08e68c3/pydantic/experimental/pipeline.py#L313)
2026-11-17 16:48:01 -08:00
Alex Waygood 45f24aea12 [ty] Split incompatible TypeIs narrowings into two new rules (#29949) 2026-11-17 22:20:52 +00:00
Carl Meyer a5f0610586 [ty] Avoid exponential constraint search in generic constructor inference (#30474)
ty could spend minutes inferring the type of a Bokeh `Either`
constructed from a property with 28 literal marker names and a property
accepting `str`. To rule out a candidate type, the solver explored
combinations of bounds for every marker. Rejecting both bounds for just
one marker is already a counterexample.

Before searching for a counterexample, ty now tries one path that makes
each atomic constraint on it false. It follows that path only while the
diagram already has the next node, so it never joins branches or
backtracks. If the path reaches false and its assignments are
consistent, ty can rule out the candidate immediately. Otherwise it uses
the existing exact search.

Fixes astral-sh/ty#5046.

## Test plan

I checked [Bokeh’s original
`dataspec.py`](https://github.com/bokeh/bokeh/blob/1c2309202d8623ede74a996a367f3b60eaf79411/src/bokeh/core/property/dataspec.py#L408-L409)
with the base and PR Linux binaries, the same checker configuration, and
Bokeh’s dependencies in a Python 3.12 environment. The base binary
exceeded a 20-second timeout. Removing just the reported constructor
call let it finish in 0.69 seconds. The PR binary checked the unmodified
file in 0.80 seconds with no diagnostics. Using Bokeh’s classes
directly, it also accepted `assert_type(Either(MarkerType,
Regex("^@.*$")), Either[str, str])`.
2026-11-17 12:25:37 -08:00
Carl Meyer 1e0a2ce2e3 [ty] Check fixed prefixes when assigning ParamSpec callables to gradual Concatenate (#30470)
## Summary

ty previously accepted `call_with_int(callback)` below, even though a
caller can pass `forward` a function that takes no arguments.

```python
from typing import Callable, Concatenate

def call_with_int(callback: Callable[Concatenate[int, ...], None]) -> None:
    callback(1)

def forward[**P](callback: Callable[P, None]) -> None:
    call_with_int(callback)  # now an error
```

Check the fixed positional parameters before the caller's `ParamSpec`
against the prefix required by `Concatenate`. This also rules out
overloads that require more positional parameters than the callback
guarantees.

This fixes a bug that otherwise caused new false-positive regressions on
https://github.com/astral-sh/ruff/pull/30423.

## Ecosystem report

The public run for `deb2cd0da5` found three expected changes across 165
projects. ty now reports a missing class receiver in a
pytest-autoprofile wrapper annotation and removes two incorrect
overload-related errors in discord.py. [Full ecosystem
report](https://github.com/astral-sh/ruff/pull/30470#issuecomment-6927397479).
2026-11-17 09:09:32 -08:00
Carl Meyer 51b50bd771 [ty] Fix stack overflow checking a generic base initializer (#30473)
ty used to abort with a stack overflow while checking this
`super().__init__` call. It now reports `invalid-argument-type` on
`value` in `super().__init__(value)`.

```python
class Box[T]:
    def __init__[A, B](self: "Box[A]", value: A | tuple[B]) -> None: ...

class Child[T](Box[bytes]):
    def __init__(self, value: T | tuple[T]) -> None:
        super().__init__(value)
```

The receiver for that call is a `Child[T]`, which is already a
`Box[bytes]`. That fixes `A` to `bytes`. A one-element tuple is valid
because the initializer can infer `B = T`. But `Child[str]("hello")`
would forward a plain string where the initializer expects `bytes` or a
tuple.

ty now uses the receiver’s existing class type arguments when checking a
constructor method. Looking up the method on a bare generic class still
allows the call to infer those arguments.

The stack overflow happened while ty was trying to explain the failed
call. It combined type-variable bindings inferred from individual
arguments, then repeatedly substituted them into each other. A cycle
through a tuple element can add another layer of `tuple[...]` on every
substitution. The original reproducer had the same problem with a
callable’s return type. ty now drops the parts of a binding that would
keep growing and uses the remaining evidence to report the error.

Fixes https://github.com/astral-sh/ty/issues/5047

## Test plan

Compared the CLI on the merge base and this change using the example
above and the original reproducer in both PEP 695 and legacy `TypeVar`
syntax. All three aborted on the merge base and produced argument-type
diagnostics on this change. The example above passes with `Box[T]` as
the base.

## Ecosystem report

On `3c94d4a654a3c2cc6a05b4845d4057271599ebb0`, six new diagnostics
identify constructors that accept arguments incompatible with the
receiver’s specialization. The change also removes a false return
warning in PyMongo and simplifies two existing setuptools diagnostics.
All nine changes were reproduced, with no new regressions found. [Full
ecosystem
report](https://github.com/astral-sh/ruff/pull/30473#issuecomment-6931764426)
2026-11-17 08:00:12 -08:00
David Peter 1d40d153a9 [ty] Document runtime support in ty-extensions (#30497)
## Summary

The `ty-extensions` package lets Python evaluate and inspect
`Intersection[A, B]` and `Not[A]` at runtime. Update the stub docstrings
and the `experimental-syntax` documentation to point readers to the
package.
2026-11-17 14:41:58 +00:00
Micha ReiserandAlex Waygood 0222c19dd8 Share ty configuration for Ruff’s standalone scripts (#30463)
## Summary

Use a shared ty override for Ruff’s standalone scripts now that PEP 723
scripts can inherit project settings. Fix the diagnostics in the two PR
automation scripts so they can use the same checks.

## Notes to reviewers

All 28 standalone scripts passed ty with locked dependencies, and the
changed-file hooks and mocked workflow checks passed.

---------

Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
2026-11-16 17:35:08 +00:00
Alex Waygood a42b3d2779 [ty] Split invalid dataclass __post_init__ signatures into a separate rule (#30335) 2026-11-16 15:45:42 +00:00
Carl Meyer 82bb2810cc [ty] Speed up materialization of nested generic types (#30462)
Checking nested calls to a generic overload can spend most of its time
materializing the same type arguments repeatedly. For a type such as
`Row[Row[Any]]` with a covariant parameter, each layer could visit its
argument twice to compute the same top materialization. This doubles the
work with each layer.

Compute the top materialization lazily and reuse it when comparing the
argument with its materialization or constructing the result. This also
skips the top traversal when only the bottom materialization is needed.

Improves astral-sh/ty#5041.

## Test plan

I timed the reproducer from the issue on macOS arm64 using an optimized
debug build. Checking eight nested overloaded calls went from 29.05 s to
1.55 s. To isolate materialization from overload resolution, I also
checked `Top` applied to 23 nested `Row` types. That went from 3.08 s to
0.03 s. Both examples passed before and after the change.
2026-11-16 07:44:14 -08:00