Commit Graph
223 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 455e8a66f6 Reuse evaluation-order traversal and compare aligned call arguments directly
Existence searches over calls and class headers can inspect each slot
once in written order. For f(key=a, *b), the predicate sees a first.
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 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 863c499231 Share pattern capture lookup across binding visitors
Match the binding introduced by an as, star, or mapping pattern
in one place. Cover the order of recovered mapping captures in
structural comparisons.
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
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
Charlie Marsh 0180bc7ff6 [ty] Reuse scope reachability when applying pending constraints (#30552)
## Summary

In a method that alternates assignments and calls, ty can do quadratic
work when it finishes indexing the scope.

```python
def setup(self):
    self.title = Label()
    self.title.show()
    self.body = Label()
    self.body.show()
```

Each `show()` adds a condition to the scope's reachability because the
call might never return. At the end of the method, ty replays both
conditions for `self.title` and the last one for `self.body`, even
though it already computed the result for the scope.

We now reuse the scope's result when a place's only binding and
declaration had the scope's reachability at their last update and have
been subject to the same conditions since. A branch join that changes
the scope's reachability starts a new sequence. This adds eight bytes
per pending condition during indexing.

Two cold checks of all 89 Python files in Nugget used 14.80% and 14.89%
fewer instructions under Cachegrind, reversing the binary order between
pairs. The adjacent runs of the same binary differed by 0.04%. Both
versions produced the same diagnostics.

| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/248231e9936ecc0e44ecd3a69bc07899736e4e23)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/7204d9f77218687fc12c4ee60d71e7abfeff56b0)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| 3,170,751,582 | 3,164,003,340 | -0.21% | -0.30% to -0.13% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
(inconclusive) | 8,248,193,503.5 | 8,233,254,668.5 | -0.18% | -0.23% to
-0.14% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 159,662,332,735.5 | 159,138,607,459.5 | -0.33% | -0.34% to -0.32% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
(inconclusive) | 1,840,424,770.5 | 1,838,894,887.5 | -0.08% | -0.14% to
-0.03% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,928,303,587 | 1,922,372,176 | -0.31% | -0.59% to
-0.03% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 30,091,096,443.5 | 30,032,785,691 | -0.19% | -0.31% to -0.08% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,913,540,638 | 2,915,535,997.5 | +0.07% | -0.02% to
+0.16% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| 4,896,057,762 | 4,880,091,968 | -0.33% | -0.38% to -0.27% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 98,234,249,892 | 97,948,540,126.5 | -0.29% | -0.35% to -0.23% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,668,077,370 | 1,664,790,522.5 | -0.20% | -0.36% to
-0.04% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,569,072,870 | 8,515,572,449 | -0.62% | -1.57% to
+0.34% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| 2,954,032,974 | 2,946,387,194 | -0.26% | -0.33% to -0.19% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 21,779,742,935.5 | 21,762,682,593.5 | -0.08% | -0.17%
to +0.01% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,707,623,722.5 | 2,700,628,044.5 | -0.26% | -0.31% to
-0.21% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
(inconclusive) | 13,978,502,353 | 13,957,922,790.5 | -0.15% | -0.18% to
-0.12% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| 46,780,732,674.5 | 46,652,725,052.5 | -0.27% | -0.29% to -0.26% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 15,983,710,137.5 | 15,966,100,836.5 | -0.11% | -0.13% to -0.09% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 8,830,661,627.5 | 8,810,034,919 | -0.23% | -0.30% to -0.17% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
(inconclusive) | 4,146,032,735.5 | 4,145,029,409.5 | -0.02% | -0.08% to
+0.03% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
| 14,566,867,346.5 | 14,538,830,606.5 | -0.19% | -0.30% to -0.08% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
| 3,011,283,488.5 | 3,007,261,582 | -0.13% | -0.18% to -0.09% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
| 4,836,275,455 | 4,825,465,232.5 | -0.22% | -0.24% to -0.21% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
| 35,983,453,181.5 | 35,927,622,058 | -0.16% | -0.18% to -0.13% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
| 16,460,020,869 | 16,422,672,953.5 | -0.23% | -0.27% to -0.19% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
(inconclusive) | 13,814,957,166.5 | 13,818,460,877.5 | +0.03% | -0.03%
to +0.08% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
| 75,184,950,008 | 75,132,955,103.5 | -0.07% | -0.08% to -0.06% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
| 8,975,543,123.5 | 8,954,374,939 | -0.24% | -0.25% to -0.22% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
| 105,026,968,413.5 | 104,933,406,957.5 | -0.09% | -0.13% to -0.05% |
2026-11-19 17:13:40 -05:00
Charlie Marsh 84dbc07bc4 [ty] Avoid a quadratic scan when nested scopes capture the same name (#30550)
## Summary

When many functions reference the same global, ty scans all earlier
captures of that name to see if the same function has already
contributed one. This makes capture registration quadratic in the number
of functions. Each scope contributes a name at most once from its symbol
table, and each enclosing scope forwards that capture at most once. We
now append the capture directly.

For example, thousands of functions in PyMuPDF’s generated bindings
reference `_mupdf`. A cold check of its [barcode
test](https://github.com/pymupdf/PyMuPDF/blob/026467aeb6730f34bc3a0c470cf48ad87cd4367f/tests/test_barcode.py)
with PyMuPDF 1.26.6 installed used 10.62–10.70% fewer instructions under
Cachegrind in opposite run orders. Repeating the baseline varied by
0.01%.

A cold check of a complete [Pulumi Datadog monitoring
example](https://github.com/Koality-Assured/pulumi-examples/tree/29e9e75ccb54e0682da01a945ce55953e46cc295/01-resource-types/saas/datadog-monitoring)
with `pulumi-datadog==4.38.0` installed used 22.58–22.59% fewer
instructions, with 0.03% variation between consecutive baseline runs.

| Project | [Base
instructions](https://github.com/astral-sh/ruff/commit/248231e9936ecc0e44ecd3a69bc07899736e4e23)
| [PR
instructions](https://github.com/astral-sh/ruff/commit/8831d4123e011cd25f9f73882ff27a599537e12b)
| Change | Paired range |
| --- | ---: | ---: | ---: | ---: |
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
(inconclusive) | 3,171,871,942 | 3,172,660,735 | +0.06% | -0.07% to
+0.28% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
(inconclusive) | 8,246,976,047.5 | 8,247,469,860 | +0.01% | -0.11% to
+0.12% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
(inconclusive) | 159,831,645,645 | 159,836,213,509 | 0.00% | -0.01% to
+0.01% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
(inconclusive) | 1,841,937,273.5 | 1,841,800,765 | -0.01% | -0.02% to
+0.01% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
(inconclusive) | 1,922,762,258 | 1,933,479,728 | +0.28% | +0.05% to
+0.64% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 30,072,398,015 | 30,113,239,179 | +0.14% | +0.06% to +0.14% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
(inconclusive) | 2,923,120,371 | 2,922,602,362 | -0.02% | -0.24% to
+0.21% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
(inconclusive) | 4,896,100,274.5 | 4,897,796,137.5 | +0.03% | +0.01% to
+0.06% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
(inconclusive) | 98,198,515,492 | 98,262,036,801 | +0.05% | -0.01% to
+0.18% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
(inconclusive) | 1,666,680,241.5 | 1,670,107,613.5 | +0.21% | +0.10% to
+0.31% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
(inconclusive) | 8,495,183,041.5 | 8,482,760,233 | -0.13% | -1.20% to
+0.93% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
(inconclusive) | 2,956,354,109 | 2,964,821,080.5 | +0.29% | +0.11% to
+0.46% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
(inconclusive) | 21,798,033,526.5 | 21,774,262,667.5 | -0.11% | -0.16%
to -0.06% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
(inconclusive) | 2,708,181,828 | 2,705,801,768 | -0.09% | -0.24% to
+0.07% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
(inconclusive) | 14,012,113,173.5 | 13,985,445,383.5 | -0.19% | -0.30%
to -0.08% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
(inconclusive) | 46,807,533,380 | 46,812,535,284 | +0.01% | -0.03% to
+0.05% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
(inconclusive) | 15,992,800,971 | 15,998,368,605 | +0.06% | -0.12% to
+0.07% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
(inconclusive) | 8,841,042,537 | 8,837,616,844.5 | -0.04% | -0.06% to
-0.01% |
|
[aiohttp](https://github.com/aio-libs/aiohttp/tree/fe099491d275ef2c21bf683e024e34d51ac05d43)
(inconclusive) | 4,152,783,425.5 | 4,156,717,619.5 | +0.09% | -0.05% to
+0.24% |
|
[Ansible](https://github.com/ansible/ansible/tree/b4b2b1e82a535d27f902a71fc8396c3af138fca2)
(inconclusive) | 14,576,899,748 | 14,578,298,734.5 | +0.01% | 0.00% to
+0.02% |
|
[botocore](https://github.com/boto/botocore/tree/e250abbe956da791c16086575e6ddb1f2da81a78)
(inconclusive) | 2,999,367,861 | 3,003,956,335 | +0.15% | +0.03% to
+0.19% |
|
[Celery](https://github.com/celery/celery/tree/9c2f238334331db43f15956f131235fd3bb70fd1)
(inconclusive) | 4,825,343,417 | 4,825,885,599 | +0.01% | -0.10% to
+0.13% |
|
[Dask](https://github.com/dask/dask/tree/249a51a1db4261791b93b4ce04e67341729939c3)
(inconclusive) | 35,985,719,601.5 | 35,963,660,712 | -0.06% | -0.07% to
-0.05% |
|
[NetworkX](https://github.com/networkx/networkx/tree/6855cd586982cc2e4710b8eb87251ccf56674bf1)
(inconclusive) | 16,470,376,396 | 16,472,782,715.5 | +0.01% | 0.00% to
+0.03% |
|
[Pydantic](https://github.com/pydantic/pydantic/tree/560f0f96a37d6913afdfddb2de22030ef08e68c3)
(inconclusive) | 13,824,027,712 | 13,827,950,342.5 | +0.03% | -0.02% to
+0.07% |
|
[scikit-learn](https://github.com/scikit-learn/scikit-learn/tree/ee56c8b5d809dc7b970a140ed501269b63c69cf8)
(inconclusive) | 75,198,588,187 | 75,150,252,834.5 | -0.06% | -0.11% to
-0.02% |
|
[Scrapy](https://github.com/scrapy/scrapy/tree/e63a574c767d7a9438c21db158f605f1c44c6e6a)
(inconclusive) | 8,988,272,177.5 | 8,985,765,318.5 | -0.03% | -0.05% to
0.00% |
|
[Transformers](https://github.com/huggingface/transformers/tree/3a134bc37f7c22c801b618e289b0255425f22647)
(inconclusive) | 105,049,542,095.5 | 105,045,468,516.5 | 0.00% | -0.01%
to 0.00% |
2026-11-19 12:15:57 -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
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
Brent Westbrook 244668339a Adopt new APIs from Rust 1.98 (#30419)
Summary
--

- The first commit uses [strip_circumfix] to remove paired delimiters in
one call.
- The second commit uses [map_or_default] to combine many `map` and
`unwrap_or_default` calls.
- The third commit uses [bool::ok_or] to turn boolean checks into
`Result<(), E>` values.
- The fourth commit uses [str::substr_range] to get the byte range of a
TODO directive directly from the comment.

The last commit is slightly more involved than the others, as Codex
found a panic in the old code if
a `HACK` TODO comment used the Kelvin sign as its `K` and added a test
showing that `substr_range`
helped to fix it. That seems absurdly niche, but the new code is simpler
anyway.

There's also a new [format_into] API for formatting integers into a
`NumBuffer` and bypassing some
`Display` overhead. We could use this in the `UP031` fix, but it makes
the code a bit more
complicated and seemed worth trying and benchmarking separately to see
if it's worth the trouble.

[strip_circumfix]:
https://doc.rust-lang.org/stable/std/primitive.str.html#method.strip_circumfix
[map_or_default]:
https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.map_or_default
[bool::ok_or]:
https://doc.rust-lang.org/stable/std/primitive.bool.html#method.ok_or
[str::substr_range]:
https://doc.rust-lang.org/stable/std/primitive.str.html#method.substr_range
[format_into]:
https://doc.rust-lang.org/stable/core/primitive.usize.html#method.format_into
2026-11-16 10:04:15 -05:00
0ebb77d35c Bump version to 0.17.5 (#30380)
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>
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2026-11-12 17:26:23 +00:00
Charlie Marsh 55abaf59cc [ty] Share use-def layouts for stub function parameters (#30248)
## Summary

A function with a stub body has no local name uses after its parameters
are declared. Its binding and declaration tables depend on which
parameters have annotations. Functions with the same pattern can share
those tables while keeping their own parameter definitions. A signature
with duplicate names uses the normal scope builder.

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

| Project | Base instructions | PR instructions | Change | Paired range
|
|---|---:|---:|---:|---:|
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| 3,972,170,946 | 3,934,724,146 | -0.95% | -1.31% to -0.79% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| 10,727,056,902 | 10,672,339,242 | -0.50% | -0.88% to -0.15% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| 17,975,757,708 | 17,969,949,139 | -0.05% | -1.24% to +1.27% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| 4,150,650,394 | 4,129,190,259 | -0.51% | -1.01% to -0.09% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 198,250,102,436 | 197,813,131,960 | -0.07% | -1.11% to +0.62% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| 2,160,685,464 | 2,129,113,919 | -1.55% | -2.00% to -0.87% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| 2,266,667,126 | 2,234,942,245 | -1.23% | -1.84% to -1.05% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
| 2,433,621,432 | 2,402,995,600 | -1.26% | -1.59% to -0.91% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
| 11,037,931,862 | 10,748,697,932 | -2.70% | -2.90% to -2.43% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 20,864,937,211 | 20,688,391,813 | -0.48% | -1.65% to -0.12% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 36,559,120,456 | 36,199,801,896 | -0.96% | -1.43% to -0.64% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
| 4,184,637,483 | 4,058,481,840 | -2.86% | -3.49% to -1.93% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| 6,331,090,970 | 6,296,937,026 | -0.42% | -0.73% to -0.12% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 11,095,797,874 | 10,999,856,852 | -0.54% | -1.15% to -0.35% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 115,613,211,531 | 115,827,027,921 | +0.30% | -0.81% to +0.80% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
| 3,390,517,033 | 3,356,566,940 | -0.98% | -1.82% to -0.59% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| 25,919,023,333 | 25,832,313,568 | -0.27% | -0.87% to -0.04% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| 55,398,262,533 | 55,431,684,846 | +0.03% | -0.72% to +0.35% |

<!-- ty-table-revisions base=1e484546d85056bbcd463e17af4a783d2c0ed169
head=40eaab770c678a5bfb3ee3c087d485c29f4f958b default-pairs=8
single-worker-pairs=4 -->

All 18 projects produced byte-identical output and matching exit codes.

Across all 18 projects, [the PR at
`40eaab770`](https://github.com/astral-sh/ruff/commit/40eaab770c678a5bfb3ee3c087d485c29f4f958b)
used 0.29% fewer instructions than [main at
`1e484546d`](https://github.com/astral-sh/ruff/commit/1e484546d85056bbcd463e17af4a783d2c0ed169)
with the default worker count (median of eight paired sweeps). With one
worker, it used 0.29% fewer instructions (median of four paired sweeps).
PyTorch's median was +0.30% in the eight pairs above and -0.14% in four
additional pairs.

<!-- ty-pinned-project-instructions:end -->
2026-11-11 14:02:02 -05:00
Charlie Marsh 3cd7c51898 [ty] Record exception checkpoints for nested unpacking targets (#30206)
## Summary

Python assigns unpacking targets from left to right. By the time it
tries to unpack `pair` below, `value` already holds `"bad"`.

```python
def f(pair: tuple[int, ...]) -> None:
    value: int | str = 1
    try:
        value, (a, b) = "bad", pair
    except ValueError:
        value.bit_length()  # Raises AttributeError when pair has the wrong length.
```

ty accepted this call because it took a checkpoint only before the outer
unpack, when `value` was still `1`. We now take a checkpoint before
unpacking each tuple or list target. The handler sees `Literal[1,
"bad"]`, so ty flags the call.

We also take the nested checkpoint when the source is an exact-length
literal. If we replace `pair` with `(1, 2)`, the unpack always succeeds,
but ty can still include `"bad"` in the handler even if a later
assignment overwrites it before anything raises.

Closes https://github.com/astral-sh/ty/issues/4958.
2026-11-10 11:38:31 +01:00
Charlie Marsh 73614a78ea [ty] Preserve distinct bytes keys during narrowing (#30207)
## Summary

ty treats distinct bytes keys as the same key when decoding them as
UTF-8 produces the same replacement character. In this example, checking
one entry convinces ty that the other is an `int`.

```python
def increment(counts: dict[bytes, int | None]) -> int:
    if counts[b"\xaa"] is not None:
        return counts[b"\xff"] + 1  # The second entry can still be None.
    return 0
```

We now escape the bytes when building the key for narrowing. This
preserves the value of each bytes literal, including literal
backslashes.

Closes https://github.com/astral-sh/ty/issues/2579.
2026-11-09 09:26:13 -05:00
Charlie MarshandCharlie Marsh 56bab0bd9a [ty] Reuse the incoming flow after boolean expressions without assignments (#30097)
## Summary

In `x is not None and x.value`, ty needs the narrowing from the first
operand while it visits the second. Once the paths rejoin, the bindings
are the same as they were on entry. We used to take a snapshot at every
point where the expression could short-circuit and merge them afterward.

We now save the entry state once and restore it after visiting the
operands. A walrus expression can introduce a binding, and an active
exception handler can observe an intermediate state. Both cases still
need the individual snapshots.

<!-- ty-pinned-project-instructions:start -->
The table shows median instructions per cold `ty check`.

| Project | Parent instructions | PR instructions | Change | Paired
range |
|---|---:|---:|---:|---:|
|
[Black](https://github.com/psf/black/tree/55996d1fb8342725c8a19b6023cd9439c3c1d6f7)
| 3,327,629,858 | 3,228,489,024 | -2.99% | -3.13% to -2.81% |
|
[discord.py](https://github.com/Rapptz/discord.py/tree/65232c38702be5844cf2ce865a4777eb1928b5d0)
| 8,526,139,909 | 8,463,328,726 | -0.76% | -0.88% to -0.45% |
|
[Django](https://github.com/django/django/tree/b501b8493f31cf8cba31211d59b3b78642b8cb02)
| 14,526,261,720 | 14,406,319,474 | -0.76% | -0.95% to -0.74% |
|
[FastAPI](https://github.com/fastapi/fastapi/tree/7a0a3f0b286c6c865cf4398ea0e957a87b8bffa4)
| 3,208,278,164 | 3,185,735,825 | -0.69% | -0.73% to -0.62% |
| [Home
Assistant](https://github.com/home-assistant/core/tree/c19ae285ede74cd64c3045a3a4aa43290c224f1b)
| 160,432,235,038 | 159,688,122,787 | -0.46% | -0.51% to -0.45% |
|
[HTTPX](https://github.com/encode/httpx/tree/b5addb64f0161ff6bfe94c124ef76f6a1fba5254)
| 1,759,762,092 | 1,714,778,980 | -2.53% | -2.58% to -2.46% |
|
[isort](https://github.com/pycqa/isort/tree/a514c71ed86c2216d0040c7d60e302ed76a4f7ab)
| 2,115,726,444 | 2,071,864,550 | -2.08% | -2.16% to -1.86% |
|
[Jinja](https://github.com/pallets/jinja/tree/5ef70112a1ff19c05324ff889dd30405b1002044)
| 1,921,906,044 | 1,921,630,812 | 0.00% | -0.17% to +0.11% |
|
[NumPy](https://github.com/numpy/numpy/tree/f9f1772ce010001208475f751d19253469788410)
| 14,350,543,068 | 14,337,534,500 | -0.09% | -0.66% to -0.02% |
| [OpenAI Python
SDK](https://github.com/openai/openai-python/tree/f6e8480f9a5b2a4d02b17a1da54a1e57964bc312)
| 17,911,735,261 | 17,808,248,965 | -0.54% | -2.55% to +1.45% |
|
[pandas](https://github.com/pandas-dev/pandas/tree/a0225b65720018f4654390bb16d254daa6ee5e54)
| 30,143,629,900 | 30,011,481,130 | -0.48% | -0.56% to -0.31% |
|
[pandas-stubs](https://github.com/pandas-dev/pandas-stubs/tree/8dd6d91b1c05b26e30f7ddbe3dc3d4042a92bdd6)
| 3,586,149,998 | 3,574,843,506 | -0.35% | -0.54% to -0.25% |
|
[Prefect](https://github.com/PrefectHQ/prefect/tree/a9452ac21852755311913cb27887b083b5007c03)
| 5,087,567,478 | 5,030,294,525 | -1.14% | -1.18% to -1.01% |
|
[pytest](https://github.com/pytest-dev/pytest/tree/8b005ed7f972987033ec34dc1e68b0635fb8142b)
| 8,663,862,776 | 8,614,190,006 | -0.56% | -0.71% to -0.51% |
|
[PyTorch](https://github.com/pytorch/pytorch/tree/ab92945cea42e47ae398aead69af0872d340ea0f)
| 98,260,011,188 | 97,510,158,026 | -0.77% | -0.85% to -0.68% |
|
[Rich](https://github.com/Textualize/rich/tree/9d8f9a372cc5916fd4781fec207ced7ddac2f08f)
| 2,778,090,819 | 2,756,011,028 | -0.73% | -1.37% to -0.67% |
|
[SQLAlchemy](https://github.com/sqlalchemy/sqlalchemy/tree/10f4d1e2d706a7722ca78e79ce05b00333a7aa5b)
| 21,141,258,225 | 21,076,436,106 | -0.29% | -0.49% to -0.25% |
|
[SymPy](https://github.com/sympy/sympy/tree/85d3998a4517ffbc8a9709b8efa769bb417c174a)
| 47,484,365,830 | 47,286,307,006 | -0.40% | -0.48% to -0.38% |

These measurements use PR revision `517ed31f6968`.
<!-- ty-pinned-project-instructions:end -->

---------

Co-authored-by: Charlie Marsh <charliemarsh@openai.com>
2026-11-06 13:31:46 -05:00
Charlie MarshandCharlie Marsh 72db5cb153 [ty] Index lazy enclosing snapshots by symbol name (#30088)
## Summary

A nested function can see a later assignment to a name it closes over.
When ty indexed an assignment to `x`, it walked every enclosing-scope
snapshot in the file to find the ones that could see the new value of
`x`.

We now group the snapshots for lazy scopes by name. An assignment to `x`
visits only snapshots for `x`, and a binding in an intervening scope
stops the update from reaching a shadowed name.

A cold `ty check` of NumPy used 8.2–9.1% fewer instructions.

---------

Co-authored-by: Charlie Marsh <charliemarsh@openai.com>
2026-11-06 09:14:14 -05:00
Charlie Marsh 583b7972e8 [ty] Keep source place IDs out of the use-def map (#30077)
## Summary

Consider a file that imports `x` from this module:

```python
x: int = 1
list
dict
list
```

Changing the last `list` to `dict` changes which name that expression
reads. Since #29956 stores the place ID of each read in the use-def map,
the edit also invalidates the importer's lookup of `x`, even though its
declaration and bindings are the same.

We now store the place ID alongside the use ID in the AST index. The
use-def map records the bindings that reach each use, so ty can reuse
the cached lookup of `x` across this edit.

With an unannotated `x = 1`, the end-of-scope bindings query also stays
cached. The full import lookup for that case still reruns because it
reads the file’s semantic index to check the visibility of the scope.

Follow-up to [Carl’s review of
#29956](https://github.com/astral-sh/ruff/pull/29956#pullrequestreview-5777183605).
2026-11-06 02:29:26 +00:00
Lérè 7d31eb4255 [ty] Compute reaching definitions from inferred deferredness. (#29685)
## Summary

This adds an on-demand lookup for the definitions that may reach a name.
It is intended for language-server features such as [file
rename](https://github.com/astral-sh/ty/issues/1560) and
[control-flow-aware
references](https://github.com/astral-sh/ty/issues/3410).

This supersedes [#29545](https://github.com/astral-sh/ruff/pull/29545).

## Approach

During ordinary type inference, ty records which place loads are
deferred in cached inference results.
[`SemanticModel::reaching_definitions`](https://github.com/astral-sh/ruff/blob/de0a0f7c6bc1655c7eb2dd2855447d4ba6fd7192/crates/ty_python_semantic/src/semantic_model.rs#L466-L488)
uses that information to choose between the bindings that reach the use
and all reachable bindings in the scope. It then resolves the name using
the same lookup sources and source evaluation as type inference. The
lookup reuses cached inference where available, or runs ordinary
inference on a cache miss.

The deferred-load information is retained in inference results,
including in the CLI. The impact of that extra storage is surfaced in
the [memory
report](https://github.com/astral-sh/ruff/pull/29685#issuecomment-6375989769)
below.

Please note that there are a few bugs in type inference that affect the
correctness of this PR, but which I consider out of scope here:

- https://github.com/astral-sh/ty/issues/4786
- https://github.com/astral-sh/ty/issues/4875
- https://github.com/astral-sh/ty/issues/4876
- https://github.com/astral-sh/ty/issues/4877

Closes https://github.com/astral-sh/ty/issues/4788.

## Test Plan

See included tests.
2026-11-05 14:29:36 -08:00
Carl Meyer b638a3a586 [ty] Invalidate aliased conditions across loop iterations (#30074)
A condition saved before a loop can keep narrowing a value after the
loop body has reassigned it. For example, after `is_none = x is None`,
ty can treat `x + 1` as safe even though a previous iteration set `x` to
`None`.

Invalidate the saved condition when a reachable loop backedge can carry
a write to a place it narrows. This uses the same whole-condition
invalidation as a reassignment in straight-line code.

Fixes astral-sh/ty#4934

## Test plan

Compared a guarded `x += 1` in a loop with a type-preserving `x += 1`
between saving and testing the condition in straight-line code. Ty
invalidates the saved narrowing in both cases.

## Ecosystem report

The public ecosystem run for `aa1cb33eeb` found one expected change.
StaticFrame no longer receives a false `redundant-condition-strict`
warning when a previous loop iteration can initialize `dtype`. The
result matched the reproduction with the workflow binaries.

[Full ecosystem
analysis](https://github.com/astral-sh/ruff/pull/30074#issuecomment-6666621923)
2026-11-05 14:09:38 -08:00
Charlie Marsh f925705d23 [ty] Record lexical place IDs with their uses (#29956)
## Summary

When ty indexes a read like `obj.child`, it already knows which place
the expression refers to and which bindings can reach that read. Type
inference then reconstructs the place from the syntax and looks it up
again. We now record the place ID alongside the reaching bindings and
reuse it when inferring a load in the same scope. Separately parsed
string annotations still resolve from syntax.

We store use IDs in node order, with a bitmap marking which nodes have
uses. We use two-byte records when the IDs for the place and the set of
reaching bindings each fit in a byte for every use in a scope.
Otherwise, each record in that scope takes eight bytes. Across the
source files we checked in Home Assistant, Prefect, Sphinx, Trio, and
Flake8, only 28 of 135,894 scopes with recorded uses (0.021%) needed
eight-byte records. Those scopes accounted for 0.99% of all recorded
uses.

On Home Assistant, `ty check` on macOS arm64 executed 2.49% fewer
instructions and used 7.8 MiB less peak memory than `main`.
2026-11-05 14:45:21 -05:00
astral-automations-bot[bot]andBrent Westbrook 05cdc36a45 Bump version to 0.17.4 (#30073)
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-05 13:22:15 -05:00
Alex Waygood c86652abce [ty] Skip active Conda and Pixi environments during discovery if the environment doesn't contain Python (#30066) 2026-11-05 15:58:48 +00:00
Charlie MarshandCharlie Marsh 5cecf5e25c [ty] Reuse matching constraints at branch joins (#29875)
## Summary

At the end of this `if`, ty merges the narrowing and reachability
constraints for `number`, then repeats the same work for `text`. Both
parameters reach the join through the same paths, and each keeps its
original binding and declaration in both branches.

```python
def pair(flag: bool, number: int, text: str):
    if flag:
        result = (number, text)
    else:
        result = (text, number)

    reveal_type(number)  # int
    reveal_type(text)  # str
    return result
```

We now remember the last merge at the join for which both branches agree
on a single binding and a single declaration. If another name has the
same input constraints and pending paths in both branches, we reuse the
merged constraints and keep that name’s own definitions. We also inline
the simple cases and cache lookups for constraint `AND` and `OR`
operations.

(We used 2.11% fewer instructions for a cold check of PyTorch.
Instruction counts also fell on Black (3.31%) and Jinja (0.90%).)

---------

Co-authored-by: Charlie Marsh <charliemarsh@openai.com>
2026-11-03 20:42:51 -05:00
Carl Meyer 9d353a6f13 [ty] Resolve class names in generic type aliases (#29915)
A generic type alias defined in a class could report a class-local name
as undefined or silently resolve it to a module-level name. For example,
in:

```py
class Outer:
    class Inner:

    type Pair[T] = tuple[T, Inner]
```

ty skipped the class scope that defined `Inner` because the alias's
type-parameter scope sat between its value and the class.

Name lookup now sees the enclosing class through consecutive annotation
scopes. The semantic index and type inference use the same visibility
rule.

Fixes https://github.com/astral-sh/ty/issues/4886.
2026-11-02 14:39:34 -08:00
Charlie MarshandCharlie Marsh 560a2fa8b3 [ty] Reuse class-header and scope data during member lookup (#29864)
## Summary

A cold `ty check` of SQLAlchemy executes 4.75% fewer instructions in a
release build (26.24 billion to 25.00 billion) and produces identical
diagnostics. Four-worker runs suggested a 2.34% speedup, though the
confidence interval included no change.

On `main`, looking up a generated member such as a dataclass’s
`__init__` can classify the class twice, once before searching its body
and again when that search misses. Across the SQLAlchemy check, ty
classified classes 3.19 million times. We now read the relevant
class-header fields together and reuse the classification after a miss.

A lookup like `obj.x` can find `x` in the class body or in a method that
assigns to `self.x`. We now fetch the class-body symbol table and the
names assigned to method receivers in one Salsa query per class. For
instance lookups, we also skip the `NamedTuple` field check when the
class body has no symbol named `x`, since every field is declared there.

Sharing the two inputs means that adding an unrelated class attribute
can invalidate a lookup that only needed receiver names. Applying
`dataclass` to the same class with different options can also cache
those names once per variant.

---------

Co-authored-by: Charlie Marsh <charliemarsh@openai.com>
2026-11-01 09:01:01 -05:00
Charlie Marsh 691b60a3f3 [ty] Fix a panic when checking truthiness in a loop (#29759)
## Summary

Ty could panic on `assert predicate` inside a loop where `predicate`
starts as a lambda and a later iteration rebinds it to a class. When
cycle recovery added the class to the operand type, ty looked up its
metaclass to determine its truthiness. That lookup could introduce a new
inference cycle while Salsa was recovering the first one.

Reachability now evaluates the predicate from the operand types that
expression inference has already normalized. Outside loops, it caches
the result so repeated walks of the same condition can reuse it. Inside
a loop, a condition can depend on an assignment it guards, so the caller
reads the inferred operands directly. Expression inference no longer
stores predicate truthiness or recomputes it during recovery.

The 163-project ecosystem run produced identical diagnostics. Checking
isort took 291 ms, up from 248 ms on the base revision, which gives back
some of the speedup from #29706. The average timing factor across
projects was 1.02×. CodSpeed found no significant changes among the 214
benchmarks it compared. [Ecosystem
diff](https://dqhfo45wdv0ek.cloudfront.net/ruff/pull-requests/29759/ty-ecosystem/40467940782/1/diff.html)
·
[Timings](https://dqhfo45wdv0ek.cloudfront.net/ruff/pull-requests/29759/ty-ecosystem/40467940782/1/timing.html)
·
[CodSpeed](https://github.com/astral-sh/ruff/pull/29759#issuecomment-6435864937)

Closes https://github.com/astral-sh/ty/issues/4834.
2026-10-27 07:30:58 -07:00
Charlie Marsh fbf83e2268 [ty] Avoid replaying conditions in long boolean expressions (#29754)
## Summary

Checking Home Assistant’s Anthropic integration took 283.1 ms before
this change and 252.6 ms after (10.8% less time, with a 95% paired
interval of 7.8–13.7% less). Each of 24 blocks ran `ty check
homeassistant/components/anthropic` twice with the baseline build and
once with the changed build, in balanced, randomized order. All runs
emitted the same 41 lines of diagnostics. The full Home Assistant
timings did not resolve this patch’s individual contribution.

The Anthropic SDK’s synchronous and asynchronous streaming functions
each test 53 event names in one `or` expression. At each operand, ty
used to reapply every condition leading up to it, recording 1,378
conditions per expression. The continuation already carries those
earlier conditions, so adding just the preceding operand’s condition
brings the count down to 52.

Checking a file that imports the two original function bodies went from
95.9 ms to 19.3 ms. Changing just those two `or` chains to tuple
membership checks made both builds take about 12 ms. Toggling prefix
replay in a single release executable also reproduced the integration
improvement (270.1 ms to 237.5 ms).

An assignment expression (`:=`) can introduce a new binding partway
through a chain. Those chains keep the earlier conditions so the
reassigned name gets the same reachability information.
2026-10-26 23:14:59 -07:00
Charlie Marsh 8aeb8e2ebc [ty] Reuse exception checkpoints when a new place is read (#29750)
## Summary

Cold CLI checks of Home Assistant’s KNX integration took 14% less time,
and checks of its Reolink integration took 4.5% less time. Each row
averages 12 paired runs of `ty check
homeassistant/components/<integration>` with the project’s dependencies
installed. Both integrations emitted the same diagnostics before and
after.

| Integration | Before | After |
| --- | ---: | ---: |
| KNX | 389.5 ms | 334.9 ms |
| Reolink | 396.0 ms | 378.1 ms |

The KNX decoder has a long sequence of branches inside a `try` block.
Reading a name like `GroupValueRead` for the first time used to trigger
another snapshot of the bindings visible to the exception handler.
Restoring an earlier snapshot already gives that name the same undefined
state. We now leave the checkpoint’s binding revision alone on that
first read and advance it when the place gets a definition.

In isolation, a cold CLI check importing the original
`xknx.APCI.from_knx` body went from 84.5 ms to 36.4 ms (57%). For
`reolink_aio.Host.map_channel_json_response`, it went from 41.9 ms to
32.4 ms (23%).
2026-10-26 21:48:18 -07:00
astral-automations-bot[bot]andBrent Westbrook ba65abf8ba Bump version to 0.17.3 (#29745)
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-10-26 20:14:27 -04:00
Carl Meyer 95888b2edf [ty] Preserve narrowing after guarded match subject captures (#29558)
When a match case captures the subject under its own name and its guard
fails, later cases now narrow that capture. Previously, those cases
could retain a wider type because narrowing targeted only the binding
read before the match.

Retain bindings that capture the entire subject under its original name,
including `as` patterns and OR alternatives. Captures of extracted
values and assignments made by guards remain distinct from the saved
subject.

Fixes astral-sh/ty#4761.
2026-10-26 15:21:49 -07:00
Charlie Marsh 2f0a29ece1 [ty] Record predicate truthiness alongside expression inference (#29706)
## Summary

A full ty check of isort went from 204 to 172 ms in the ecosystem run.
Across all 163 projects, the average timing factor was 1.00×. Local
checks of two isort revisions on macOS took 16–23% less time. [Ecosystem
timings](https://dqhfo45wdv0ek.cloudfront.net/ruff/pull-requests/29706/ty-ecosystem/40342132072/1/timing.html)

In `if bool(stop()):`, the call to `bool` has type `bool`, but neither
branch can run if `stop()` returns `Never`. Ty uses the operand types to
work this out. In a loop, those types can depend on an assignment
guarded by the same condition. Computing types and truthiness in
separate queries can make ty revisit that loop repeatedly.

We now record each predicate’s evaluation context when we index it.
Expression inference computes the predicate’s truthiness alongside its
operand types and recomputes it if cycle recovery widens them. Ordinary
conditions and match guards both read that result.

The only warning that changed consistently across ecosystem runs was in
psycopg. It now lists the `int` class separately even though `type[str |
Any | int]` already includes it. [Ecosystem
diff](https://dqhfo45wdv0ek.cloudfront.net/ruff/pull-requests/29706/ty-ecosystem/40342132072/1/diff.html)
2026-10-26 15:11:17 -07:00
Charlie Marsh 115138631c [ty] Collect receiver annotations once per class (#29703)
## Summary

Home Assistant’s `Entity` class reads or writes 106 different `self`
attributes across 70 methods, but none of those methods annotate one.
When checking overrides, ty could search all 70 methods for each name
just to determine that there was no declaration.

Collect receiver annotations once per class and reuse that list when
checking overrides. When inferring an implicit attribute, skip names
that the class only reads: a read of `self.x` does not define `x`.

On a class with 256 methods that each read eight different attributes,
median CPU time dropped from 896 ms to 126 ms over six runs. ty still
reported the one incompatible annotation. Two full Home Assistant checks
on Linux went from 47.0 to 45.4 s and from 49.4 to 49.2 s, with about 21
MiB less peak memory and the same 13,419 diagnostics.
2026-10-24 22:13:44 -04:00
astral-automations-bot[bot]andBrent Westbrook 30516b42ab Bump version to 0.17.2 (#29637)
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-10-22 13:54:24 -04:00
astral-automations-bot[bot] 4683270c69 Bump version to 0.17.1 (#29420)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
2026-10-15 10:56:41 -04:00
Alex Waygood a7b52a1565 [ty] Preserve distinct string literal displays without deprecated ICU data (#29206)
Canonical equivalence can make distinct string values look the same. ty
escapes their displays to distinguish them, but the current approach
relies on ICU4X's deprecated `NfcInert` property, which [ICU4X plans to
remove](https://github.com/unicode-org/icu4x/issues/7892), and can
escape characters that do not need escaping.

Replace inertness checks with NFC checks on runs of unescaped text. When
adding a character would make a run non-NFC, escape that character and
continue checking after it. The resulting string literal is in NFC and
evaluates to the original value, so canonically equivalent values remain
distinguishable.

For example, `\u212b` (the Angstrom sign) is canonically equivalent to
`Å`. ty now keeps the unrelated `é` visible while still distinguishing
the two values:

```py
reveal_type("éÅ")
# main:    Literal["éÅ"]
# this PR: Literal["éÅ"]

reveal_type("é\u212b")
# main:    Literal["\xe9\u212b"]
# this PR: Literal["é\u212b"]
```

The change also uses Unicode grapheme-break properties to keep
characters visually separate from the quotes and escape sequences around
them. It escapes the braille blank because it can look like an ordinary
space.
2026-10-13 15:04:47 +00:00
David Peter 350c634ad1 [ty] Extract small is_end_of_scope_reachable helper (#29320)
Extract `is_end_of_scope_reachable(db, ScopeId)` into the reachability
module and use it when checking ordinary and generator functions for
implicit `None` returns.

This will allow us to use this helper in the return type inference
implementation (#29253).
2026-10-13 10:26:05 +02:00
astral-automations-bot[bot]andBrent Westbrook 5b27c1ccfa Bump version to 0.17.0 (#29259)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2026-10-09 13:45:29 -04:00
Charlie Marsh 3ac6b4c312 [ty] Represent uninhabited truthiness explicitly (#28843)
## Summary

We now mark both branches of a condition with type `Never` as
unreachable:

```python
from typing import Never

def f(value: Never):
    if value:
        result = 1  # Unreachable.
    else:
        result = 2  # Also unreachable.
```

Previously, we treated `Never` as having ambiguous truthiness, allowing
either assignment to affect inference. In the reported loop, the
variables depend on each other, so including these impossible
assignments made their inferred types depend on which file we checked
first.

We add `Truthiness::Uninhabited` to represent the absence of either
boolean outcome. `Type::bool` and `Type::try_bool` return this variant
for `Never`, replacing the opt-in `bool_if_inhabited` API and making
callers handle the distinction explicitly.

Closes https://github.com/astral-sh/ty/issues/4577.
2026-10-08 20:46:56 -04:00
Zsolt Dollenstein e0da489892 [ty] Invalidate dependency metadata when environments change (#29126)
`Project::dependency_metadata` resolves Python environments inside a
Salsa query without tracking those inputs. This can leave
invalid-environment, missing-environment, and environment-mismatch
diagnostics stale after an environment changes.

Store the selected environment's resolution result in `ProgramSettings`
and track uv's environment directory through Salsa. Resolve uv's
canonical path whenever the query runs so it observes symlink changes
even when uv metadata is unchanged.

After a failed selection, retain the last usable environment for
watching and recovery, and preserve the checking program's search paths.
Dependency metadata reports the current environment error.

Fixes astral-sh/ty#4604.
2026-10-07 14:27:03 +00:00
Gore WilliamsandCharlie Marsh 433704ea75 [ty] Narrow tuple unions through truthiness and type checks (#29125)
## Summary

We now narrow unions of tuples when an element is checked for truthiness
or with `isinstance`, `issubclass`, or `TypeIs`. Eliminating an
alternative also narrows the tuple’s other elements, preserving the
relationship between a tag and its associated value. For example,
checking a boolean literal tag selects the corresponding callable
signature in either branch.

We reuse the tuple-union filtering used by equality checks and register
the containing tuple as a possible narrowing target for call predicates.
We keep uncertain alternatives and skip filtering for non-literal or
out-of-bounds indices. `TypeGuard` checks retain their existing
behavior.

For truthiness checks and calls, we skip the additional tuple narrowing
if the expression contains an assignment (`:=`). Evaluating the
expression can replace the tuple after it has been read. This
conservatively includes harmless assignments to other names; a TODO test
records that limitation.

Closes https://github.com/astral-sh/ty/issues/4674.

---------

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
2026-10-06 14:22:17 -04:00
Charlie Marsh f0214e0c6f [ty] Limit wildcard exports using literal __all__ (#28972)
## Summary

When a module imports a large namespace and then defines a small
`__all__`, we currently include every imported name when another module
uses a wildcard import. Packages with many such modules can create
hundreds of thousands of unnecessary definitions and reachability
constraints.

This PR instead recognizes an unchanged literal `__all__` before
semantic indexing and limits the possible exports to its names. Other
uses of `__all__`, including mutations and later wildcard imports, fall
back to the existing analysis. So this fixes narrow (but non-contrived)
cases.

For the `pulumi-cloudflare` reproduction in
[astral-sh/ty#4606](https://github.com/astral-sh/ty/issues/4606), peak
memory goes from 5,649-6,344 MiB to 453-459 MiB.

This also changes some diagnostics by avoiding cycles during import
resolution. Previously, determining the contents of `__all__` could
require type inference that depended on imports we were still resolving,
leaving some types as `Divergent`. Reading the literal directly breaks
those cycles, so we resolve more types successfully. This removes
spurious warnings and enables checks that were previously skipped
because the relevant types were unresolved.
2026-10-05 07:00:34 -04:00
astral-automations-bot[bot]andBrent Westbrook 3265ed1f94 Bump version to 0.16.10 (#29055)
Co-authored-by: astral-automations-bot[bot] <305554984+astral-automations-bot[bot]@users.noreply.github.com>
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2026-10-01 13:40:09 -04:00