[ty] Completely remove the NoReturn shortcut optimization (episode 2)#23994
Merged
[ty] Completely remove the NoReturn shortcut optimization (episode 2)#23994
NoReturn shortcut optimization (episode 2)#23994Conversation
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="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.
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 85.29%. The percentage of expected errors that received a diagnostic held steady at 78.13%. The number of fully passing files held steady at 64/132. |
|
Memory usage reportSummary
Significant changesClick to expand detailed breakdownflake8
trio
sphinx
prefect
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-await |
0 | 40 | 0 |
call-non-callable |
0 | 2 | 0 |
invalid-return-type |
0 | 1 | 1 |
invalid-argument-type |
0 | 1 | 0 |
type-assertion-failure |
0 | 1 | 0 |
unused-type-ignore-comment |
1 | 0 | 0 |
| Total | 1 | 45 | 1 |
carljm
added a commit
that referenced
this pull request
Mar 16, 2026
* main: (24 commits) Update astral-sh/setup-uv action to v7.6.0 (#24003) [ty] discover /usr/local/lib dist-packages on Debian/Ubuntu (#23797) [ty] Rename and invert logic of ReturnsNever constraints (#23997) [ty] Make ecosystem-analyzer the default workflow (#23996) [ty] Include CPython projects in ecosystem-analyzer runs (#23995) Update docker/setup-buildx-action action to v4 (#23992) Update actions/download-artifact digest to 484a0b5 (#23974) Update docker/metadata-action action to v6 (#23991) Update docker/login-action action to v4 (#23990) Update docker/build-push-action action to v7 (#23989) [ty] Completely remove the `NoReturn` shortcut optimization (episode 2) (#23994) Update dependency mkdocs-material to v9.7.4 (#23978) Update dependency ruff to v0.15.6 (#23979) Update dependency astral-sh/uv to v0.10.10 (#23977) Update cargo-bins/cargo-binstall action to v1.17.7 (#23975) Update CodSpeedHQ/action action to v4.11.1 (#23976) Update Rust crate jiff to v0.2.23 (#23982) Update Rust crate getrandom to v0.4.2 (#23981) Update actions/setup-node action to v6.3.0 (#23987) Update Rust crate toml to v1.0.6 (#23984) ...
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
Re-apply #23378, this time without a semantic merge conflict.