Commit Graph
10 Commits
Author SHA1 Message Date
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
Carl Meyer 17761270a5 [ty] Preserve short-circuit reachability for direct conditions (#28082)
ty can incorrectly treat a branch guarded by an impossible compound
condition as reachable when an operand has potentially mutable
truthiness. For example, `if value and False:` always skips its body in
CPython; the first falsy operand selects the control-flow path
immediately, it doesn't fully evaluate `value and False` and then
re-evaluate the truthiness of the result. Saving `value and False` to a
variable and then testing the saved object can instead call
`value.__bool__` twice and get different answers.

Distinguish expressions consumed directly as conditions from expressions
that produce values. Evaluate direct boolean and conditional expressions
structurally, and retain the combined truthiness of chained comparisons
even when a comparison returns an arbitrary object. Operands with types
equivalent to `Never` cannot produce a result; preserve that distinction
from ambiguous truthiness while composing conditions. Value contexts
continue to allow repeated truthiness checks to disagree.

To limit retained memory, comparison truthiness uses sparse overrides in
optional inference metadata, stored as boxed slices. Ordinary comparison
chains use the truthiness of their inferred value type, and root
chained-comparison conditions reuse expression inference without a
separate condition-analysis memo. Cycle widening preserves overrides
when needed to keep the effective truthiness monotonic. Tracking
operands that cannot produce a result adds only transient evaluation
state.

Prerequisite for #28045, which addresses astral-sh/ty#4380.

## Ecosystem results

Five expected diagnostic removals across pwndbg, pytest, and Spark; no
additions. Each comes from a branch that is unreachable under the
checked platform or declared types. See the [Codex ecosystem
analysis](https://github.com/astral-sh/ruff/pull/28082#issuecomment-5434341797)
for details and minimized reproducers.

## Test plan

- Mdtests cover compound conditions, nested boolean operations and
`not`, conditional expressions, and chained comparisons across branches,
loops, assertions, comprehension filters, and match guards.
- Reachability assertions use revealed variable types, including
reachable saved results, assignment expressions, nested value
expressions, and call arguments whose truthiness can be tested again.
- Cases with operands narrowed to `Never`, aliases and unions of aliases
of `Never`, and type variables bounded by `Never` assert branch
reachability using unrelated variables and check precise
conditional-expression result types.
- Additional cases cover unreachable loop assignments and conditional
expressions in stringified `Annotated` metadata.
- A Rust invariant test covers sparse comparison-truthiness overrides
appearing or disappearing across inference-cycle results, including
preservation of previous-only overrides.
2026-08-27 17:03:22 -07:00
Charlie MarshandShunsuke Shibayama 7e31571759 [ty] Propagate narrowing through statically known branches (#27752)
## Summary

Previously, narrowing was lost after a statically known branch because
the unreachable control-flow path still contributed its original type:

```python
def narrow(value: int | None) -> None:
    if 1 + 1 == 2:
        if value is None:
            return

    reveal_type(value)  # Previously: int | None; now: int
```

We now gate narrowing constraints with their corresponding reachability
conditions and reuse predicate identities across both analyses. This
allows narrowing to propagate through constant conditions, short-circuit
expressions, match guards, and statically non-empty `range()` loops
while preserving ambiguous branches.

Statement-level calls now participate in normal reachability analysis in
every scope, replacing the separate scope-wide narrowing machinery for
module- and class-level calls. As a result, the improved narrowing also
works inside module- and class-level loops, while preserving
terminal-call analysis, overload selection, and loop-carried forward
references.

Because these calls now contribute to reachability graphs, we raise the
nested-binding cutoff from 2,048 to 4,096 to preserve precision in
call-heavy scopes. The loop-reachability and loop-inference cutoffs
remain unchanged to protect performance on loop-heavy projects such as
isort and parso.

We also track the places each predicate can narrow, avoiding unnecessary
constraint inference and preventing unrelated reachability gates from
introducing cycles through deferred annotations. Lambda and generator
conditions are resolved without inferring their bodies, avoiding
recursive-callable inference cycles.

This also fixes exhaustive nested checks where proving that an outer
branch terminates depends on narrowing a separate inner value,
preventing spurious `assert_never` errors.

Based on @mtshiba's work in #27207 and #23201.

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

---------

Co-authored-by: Shunsuke Shibayama <sbym1346@gmail.com>
2026-08-20 08:42:07 -04:00
Carl Meyer c4b8749168 [ty] Fix nested short-circuit flow snapshots (#26956)
## Summary

Fixes astral-sh/ty#4025.

Nested boolean expressions were split from their already-merged flow
state, so an impossible unbound path from an inner short circuit could
reach an outer condition body. Compose the inner truthy/falsy snapshots
when evaluating each operand, including the final operand, and restore
the merged post-expression state afterward.

This removes false `possibly-unresolved-reference` diagnostics for
mutually exclusive walrus assignments while preserving genuine
short-circuit warnings.

## Test plan

Added mdtests.
2026-07-20 12:11:01 -07:00
Charlie Marsh ab1e16d2cc [ty] Preserve short-circuit bindings in all condition consumers (#25160)
## Summary

We already compute precise truthy and falsy flow snapshots for
short-circuiting boolean expressions, so walrus bindings are only
visible on paths where the right-hand side actually ran. Prior to this
change, we mostly used those snapshots for `if` conditions, and not
(e.g.) in the following case:

```python
def f(flag: bool):
    assert flag and (x := 1)
    reveal_type(x)
```

This PR generalizes that logic into condition-flow snapshots and applies
it anywhere we evaluate a condition before
continuing under its truthy or falsy outcome: `assert`, `while`, `match`
guards, conditional expressions, and comprehension filters.
2026-05-18 14:12:58 +01:00
Charlie Marsh f0ef3e9c40 [ty] Preserve walrus bindings through negated short-circuit conditions (#25163)
## Summary

When an `if` condition used unary `not` around a short-circuiting
boolean expression, we failed to reuse the precise truthy and falsy flow
snapshots that we already compute for the inner boolean operation, as
in;

```python
def f(x: str):
    if not (x and (y := x)):
        raise ValueError

    reveal_type(y)
```

We now look through the `not` when retrieving boolean-op snapshots,
swapping the truthy and falsy snapshots for the outer condition.

Closes https://github.com/astral-sh/ty/issues/3456.
2026-05-14 15:51:18 +01:00
Tamir DubersteinandCarl Meyer f508a69b70 [ty] Model bool-op branch snapshots (#24458)
Co-authored-by: Carl Meyer <carl@astral.sh>
2026-04-26 19:14:08 -07:00
Carl Meyer 2abcd86c57 Revert "[ty] Better control flow for boolean expressions that are inside if (#18010)" (#18150)
This reverts commit 9910ec700c.

## Summary

This change introduced a serious performance regression. Revert it while
we investigate.

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

## Test Plan

Timing on the snippet in https://github.com/astral-sh/ty/issues/431
again shows times similar to before the regression.
2025-05-17 08:27:32 -04:00
TomerBinandCarl Meyer 9910ec700c [ty] Better control flow for boolean expressions that are inside if (#18010)
## Summary
With this PR we now detect that x is always defined in `use`:
```py
if flag and (x := number):
    use(x)
```

When outside if, it's still detected as possibly not defined
```py
flag and (x := number)
# error: [possibly-unresolved-reference]
use(x)
```
In order to achieve that, I had to find a way to get access to the
flow-snapshots of the boolean expression when analyzing the flow of the
if statement. I did it by special casing the visitor of boolean
expression to return flow control information, exporting two snapshots -
`maybe_short_circuit` and `no_short_circuit`. When indexing
boolean expression itself we must assume all possible flows, but when
it's inside if statement, we can be smarter than that.

## Test Plan
Fixed existing and added new mdtests.
I went through some of mypy primer results and they look fine

---------

Co-authored-by: Carl Meyer <carl@astral.sh>
2025-05-16 11:59:21 +00:00
Micha Reiser b51c4f82ea Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00