[ty] Reachability analysis for generic function calls#23419
Merged
Conversation
Typing conformance resultsNo changes detected ✅ |
|
Memory usage reportSummary
Significant changesClick to expand detailed breakdownprefect
trio
sphinx
flake8
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-assignment |
0 | 1 | 0 |
type-assertion-failure |
1 | 0 | 0 |
| Total | 1 | 1 | 0 |
sharkdp
commented
Feb 19, 2026
| def f1(self: "ManyCycles2"): | ||
| # TODO: should be Unknown | list[Unknown | int] | list[Divergent] | ||
| reveal_type(self.x3) # revealed: Unknown | list[Unknown | int] | list[Divergent] | list[Divergent] | ||
| reveal_type(self.x3) # revealed: Unknown | list[Unknown | int] | list[Unknown] | list[Divergent] |
Contributor
Author
There was a problem hiding this comment.
So far, I have not tried to understand what is going on here.
Member
There was a problem hiding this comment.
seems like it gets... closer to fixing the TODO comment? 😅
Contributor
Author
|
I added additional memory usage results in the PR description. |
AlexWaygood
approved these changes
Feb 20, 2026
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
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
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: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.
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