[ty] Completely remove the NoReturn shortcut optimization#23378
Draft
[ty] Completely remove the NoReturn shortcut optimization#23378
NoReturn shortcut optimization#23378Conversation
Typing conformance resultsNo changes detected ✅ |
Memory usage reportSummary
Significant changesClick to expand detailed breakdownflake8
sphinx
trio
prefect
|
|
Merging this PR will degrade performance by 4.19%
Performance Changes
Comparing Footnotes
|
61263d4 to
88cf7b2
Compare
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
call-non-callable |
0 | 2 | 0 |
unresolved-attribute |
2 | 0 | 0 |
invalid-argument-type |
0 | 1 | 0 |
invalid-return-type |
0 | 0 | 1 |
type-assertion-failure |
0 | 1 | 0 |
| Total | 2 | 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
Contributor
Author
|
The ecosystem hits on mypy are very weird. They have something to do with loop control flow. I can reproduce with this example: from typing import Never
def _(x: str | None):
while True:
def fail() -> Never:
raise
if x is None:
fail()
reveal_type(x) # should be `str`, returns `str | None` on this branch
fail()I think it has something to do with loop-header bindings for |
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
8ef8dbe to
62840c1
Compare
62840c1 to
7836851
Compare
Contributor
Author
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
Test Plan