10 Commits

Author SHA1 Message Date
Charlie Marsh 5f040fab4b [ty] Expand support for narrowing within walruses (#24968)
## Summary

Support narrowing for a few more already-supported sites, but in the
context of a walrus, as in:

```python
def f(t: tuple[int, int] | tuple[None, None]):
    if (first := t[0]) is not None:
        reveal_type(first)  # int
        reveal_type(t)      # tuple[int, int]
    else:
        reveal_type(first)  # None
        reveal_type(t)      # tuple[None, None]
```
2026-05-01 23:09:36 -04:00
Carl Meyer 398262cb8f [ty] handle finally blocks where all try/except are terminal (#24882) 2026-04-27 12:01:57 -07:00
Tamir Duberstein f508a69b70 [ty] Model bool-op branch snapshots (#24458)
Co-authored-by: Carl Meyer <carl@astral.sh>
2026-04-26 19:14:08 -07:00
Shunsuke Shibayama d12473e107 [ty] Support basic narrowing with aliased conditional expressions (#24302)
Co-authored-by: David Peter <sharkdp@users.noreply.github.com>
2026-04-25 18:32:36 +09:00
Ibraheem Ahmed 43b174cc7f [ty] Infer lambda parameter types with Callable type context (#24317)
Improves https://github.com/astral-sh/ruff/pull/22633 to infer the use
of lambda parameters in a lambda body with type context, e.g.,
```py
x: Callable[[str], str] = lambda x: reveal_type(x)  # revealed: str
reveal_type(x)  # revealed: (x: str) -> str
```

Unlike other definitions, lambda parameter types cannot be determined
purely syntactically in semantic indexing. Instead, they depend on the
inferred type of the lambda to access its parameter types.
Unfortunately, this makes lambda inference cyclic, as the body of the
lambda depends on the outer lambda type, and there is no obvious way of
splitting out inference of the lambda parameter types from its return
type.

To avoid initiating cycles on the entire scope containing the lambda,
this PR introduces a new inference query — statement-level inference.
Statements are a minimal unit of code that encapsulate any internal type
context. This makes them very useful to infer a given sub-expression
"naturally" without having to provide any external type context. There
are other places where we currently rely on scope-level inference for
this purpose (e.g., see `infer_complete_scope_types`, the current
implementation of https://github.com/astral-sh/ruff/pull/23761, and the
discussion in https://github.com/astral-sh/ty/issues/3124). Note that
statement-level inference is not perfectly fine-grained, e.g., the test
expression of an `if` statement does not require external type context
and is independent from its body, so statement-level inference may lead
to unnecessarily large cycles, but having the unit of code being
generalized to an AST structure allows us to avoid the need for such
special cases, but this can always change in the future.

Additionally, many statements are simply wrappers around definitions or
standalone expressions, so we can avoid extra salsa allocations in the
common case.
2026-04-23 16:06:24 -04:00
Charlie Marsh 81a81d21e2 [ty] Treat [*xs] as an irrefutable pattern (#24787)
## Summary

Closes https://github.com/astral-sh/ty/issues/3304.
2026-04-23 01:10:50 +00:00
Charlie Marsh dc4df9c701 [ty] Fix TypeGuard and TypeIs narrowing for unbound method calls (#24612)
## Summary

If a `TypeGuard` is being used to narrow a method, we assume that the
guard applies to the first argument of the call (e.g., `x` in `f(x)`).
But if the call is an unbound method, it needs to be applied to the
_second_ argument, as in `C.f(C(), x)`, since the first argument is the
receiver.
2026-04-13 23:33:12 +00:00
Alex Waygood 67aa7b2877 [ty] Revert addition of unnecessary complexity to module_docstring API (#24613) 2026-04-13 17:46:17 +00:00
Alex Waygood 28b5ce0307 [ty] Move some pub functions in ty_python_core to be pub(crate) functions in ty_python_semantic (#24604) 2026-04-13 13:20:02 +01:00
Alex Waygood 461994073e [ty] Break the semantic index out into its own crate (#24471) 2026-04-13 12:39:17 +01:00