[ty] Completely remove the NoReturn shortcut optimization#23378
Merged
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 85.05%. The percentage of expected errors that received a diagnostic held steady at 78.05%. The number of fully passing files held steady at 63/132. |
Memory usage reportSummary
Significant changesClick to expand detailed breakdownflake8
trio
sphinx
prefect
|
|
Merging this PR will not alter performance
Comparing Footnotes
|
61263d4 to
88cf7b2
Compare
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
call-non-callable |
0 | 2 | 0 |
invalid-argument-type |
0 | 1 | 0 |
invalid-return-type |
0 | 0 | 1 |
type-assertion-failure |
0 | 1 | 0 |
unused-type-ignore-comment |
1 | 0 | 0 |
| Total | 1 | 4 | 1 |
26011a1 to
f87e255
Compare
NoReturn shortcut optimizationNoReturn shortcut optimization
f87e255 to
b679d98
Compare
NoReturn shortcut optimizationNoReturn shortcut optimization
sharkdp
added a commit
that referenced
this pull request
Feb 20, 2026
## Summary
If a generic function's return type depends on a type variable, and the
argument passed resolves that type variable to `Never`, the call should
still be treated as terminal:
```py
def identity[T](x: T) -> T:
return x
def f() -> Never:
identity(exit()) # should be detected as terminal
```
This is a tiny win for correctness, but unfortunately a small drop in
performance and slight increase in memory usage, because we need to
infer more call expressions upfront. If we think it's not important
enough, I'm also okay to change this to a test-only PR that documents
this as a known limitation. If we merge it, I might follow up with an
idea to simplify the code in a [slightly larger refactoring
PR](#23378).
## Memory usage
Insignificant changes on some large internal projects, a 2% *decrease*
in memory usage when running on home-assistant/core.
## Ecosystem
No ecosystem changes, as far as I can tell.
## Test Plan
New Markdown tests
b679d98 to
8ef8dbe
Compare
This comment was marked as resolved.
This comment was marked as resolved.
knutwannheden
pushed a commit
to openrewrite/ruff
that referenced
this pull request
Feb 20, 2026
## Summary
If a generic function's return type depends on a type variable, and the
argument passed resolves that type variable to `Never`, the call should
still be treated as terminal:
```py
def identity[T](x: T) -> T:
return x
def f() -> Never:
identity(exit()) # should be detected as terminal
```
This is a tiny win for correctness, but unfortunately a small drop in
performance and slight increase in memory usage, because we need to
infer more call expressions upfront. If we think it's not important
enough, I'm also okay to change this to a test-only PR that documents
this as a known limitation. If we merge it, I might follow up with an
idea to simplify the code in a [slightly larger refactoring
PR](astral-sh#23378).
## Memory usage
Insignificant changes on some large internal projects, a 2% *decrease*
in memory usage when running on home-assistant/core.
## Ecosystem
No ecosystem changes, as far as I can tell.
## Test Plan
New Markdown tests
62840c1 to
7836851
Compare
This comment was marked as outdated.
This comment was marked as outdated.
7836851 to
4eb2a72
Compare
carljm
approved these changes
Mar 14, 2026
Contributor
carljm
left a comment
There was a problem hiding this comment.
Nice!
Codex found one "regression" here, where we no longer consider a call terminal if the callable is overloaded, all overloads return Never, but the call arguments make overload resolution ambiguous, which resolves to Unknown instead of Never. IMO this is not worth caring about.
carljm
added a commit
that referenced
this pull request
Mar 16, 2026
* main: (131 commits) [ty] Fixup examples in `invalid-key` docs (#23968) [ty] Fix compiler warning about unused variable (#23967) [ty] Sync vendored typeshed stubs (#23963) Add a `.git-blame-ignore-revs` file (#23959) Revert "[ty] Completely remove the `NoReturn` shortcut optimization" (#23955) [ty] Completely remove the `NoReturn` shortcut optimization (#23378) [ty] Introduce fast path for protocol non-assignability (#23952) Bump typing conformance suite SHA (#23951) Minor followup to severity display - use preview function in server instead of checking preview disabled directly (#23950) Document editor features for markdown code formatting (#23924) [ty] Add `with_recursion_guard()` helpers to `relation.rs` (#23945) [ty] Remove `check_optional_method_pair` methods (#23947) [ty] Remove unused `CycleDetector::try_visit` method (#23944) [ty] Ensure TypedDict subscripts for unknown keys return Unknown (#23926) [ty] Fix variance of frozen dataclass-transform models (#23931) Display output severity in preview (#23845) Revert "[`ruff`] use `bitcode` instead of `bincode`" (#23935) Fix shell injection via `shell=True` in subprocess calls (#23894) [ty] Refactor `relation.rs` to store state on a struct rather than passing around 7 arguments every time we recurse (#23837) Don't return code actions for non-Python documents (#23905) ...
sharkdp
added a commit
that referenced
this pull request
Mar 16, 2026
Function calls are relevant for control flow analysis because they may
return `Never`/`NoReturn`, and can therefore be terminal. In order to
support this, we record `ReturnsNever(…)` constraints during semantic
index building for statement-level calls (in almost all situations).
These constraints keep track of the call expression such that they can
be evaluated during reachability analysis and type narrowing.
For example, if we see a call `f(a, b, c)`, we keep track of this full
call expression. The return type could depend on the arguments
(overloads, generics), so to determine if a call is terminal, we need to
evaluate the full call expression.
For performance reasons, this analysis contained a short-cut where we
looked at the return type annotation of the invoked callable (`f`).
Under certain conditions, we could immediately see that a call would
definitely be terminal. This previously helped with performance, but is
now apparently detrimental.
The optimization recently caused problems for generic function calls, so
we had to exclude those. It now turns out there was another bug, as I
figured out by looking at the ecosystem results on this PR. When the
callable expression could not be upcasted to a callable type, we assumed
that the call would never be terminal. However, if the callable itself
is `Never`, that should also be considered a terminal call. This can
happen in unreachable code. Consider this rather weird case, that I
extracted from a hydpy ecosystem hit:
```py
from typing import Never, Literal
def fail() -> Never:
raise
def _(x: Literal["a", "b"]):
if x == "a":
if 1 + 1 == 2:
return
fail()
if x == "b":
return
reveal_type(x)
```
On `main`, the revealed type of `x` is `Literal["a"]`, which is wrong.
Since the `fail()` call itself happens in unreachable code, we infer
`Never` for `fail` itself, and therefore considered the `if x == "a"`
branch to *not* be terminal. On this branch, that bug is fixed and we
correctly reveal `Never` for the type of `x`.
So the idea here is to get rid of this optimization all together and to
simply evaluate the full call expression in all cases, which also makes
this much easier to reason about.
(I find the `AlwaysFalse`/`AlwaysTrue`-answer of this evaluation very
rather confusing, because it's sort of negated twice; I plan to change
this in a follow-up)
We previously merged #19867 to
address [a performance
problem](astral-sh/ty#968) in this part of the
codebase. It seems to me that the original problem was related to the
short-cut path, but I'm not sure if this could bring back the lock
congestion problem.
In any case, this PR seems to be a performance win across the board on
our usual benchmarks. And there is also a small decrease in memory
usage.
<img width="818" height="818" alt="image"
src="proxy.php?url=https://github.com/user-attachments/assets/b5b041ca-9e06-472d-8a5a-348ce29e2433"
/>
The disappearing diagnostics all seem to be related to cases like above,
where the terminal call happened in unreachable code.
Existing tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Function calls are relevant for control flow analysis because they may return
Never/NoReturn, and can therefore be terminal. In order to support this, we recordReturnsNever(…)constraints during semantic index building for statement-level calls (in almost all situations). These constraints keep track of the call expression such that they can be evaluated during reachability analysis and type narrowing.For example, if we see a call
f(a, b, c), we keep track of this full call expression. The return type could depend on the arguments (overloads, generics), so to determine if a call is terminal, we need to evaluate the full call expression.For performance reasons, this analysis contained a short-cut where we looked at the return type annotation of the invoked callable (
f). Under certain conditions, we could immediately see that a call would definitely be terminal. This previously helped with performance, but is now apparently detrimental.The optimization recently caused problems for generic function calls, so we had to exclude those. It now turns out there was another bug, as I figured out by looking at the ecosystem results on this PR. When the callable expression could not be upcasted to a callable type, we assumed that the call would never be terminal. However, if the callable itself is
Never, that should also be considered a terminal call. This can happen in unreachable code. Consider this rather weird case, that I extracted from a hydpy ecosystem hit:On
main, the revealed type ofxisLiteral["a"], which is wrong. Since thefail()call itself happens in unreachable code, we inferNeverforfailitself, and therefore considered theif x == "a"branch to not be terminal. On this branch, that bug is fixed and we correctly revealNeverfor the type ofx.So the idea here is to get rid of this optimization all together and to simply evaluate the full call expression in all cases, which also makes this much easier to reason about.
(I find the
AlwaysFalse/AlwaysTrue-answer of this evaluation very rather confusing, because it's sort of negated twice; I plan to change this in a follow-up)Performance
We previously merged #19867 to address a performance problem in this part of the codebase. It seems to me that the original problem was related to the short-cut path, but I'm not sure if this could bring back the lock congestion problem.
In any case, this PR seems to be a performance win across the board on our usual benchmarks. And there is also a small decrease in memory usage.
Ecosystem impact
The disappearing diagnostics all seem to be related to cases like above, where the terminal call happened in unreachable code.
Test Plan
Existing tests.