[ty] Detect terminal await calls to async functions#23479
Merged
Conversation
Typing conformance resultsNo changes detected ✅ |
|
Memory usage reportSummary
Significant changesClick to expand detailed breakdownprefect
trio
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-await |
40 | 0 | 0 |
invalid-return-type |
1 | 3 | 0 |
type-assertion-failure |
1 | 0 | 0 |
| Total | 42 | 3 | 0 |
Member
|
Nice, three false positives going away on mongo-python-driver and trio! |
sharkdp
approved these changes
Feb 23, 2026
| } | ||
|
|
||
| if no_overloads_return_never && !any_overload_is_generic { | ||
| if no_overloads_return_never && !any_overload_is_generic && !is_await { |
Contributor
There was a problem hiding this comment.
Instead of excluding async functions from the fast-path check, I think we could adapt the check above to get the return type of the coroutine and unwrap it, to see if it's Never.
Contributor
There was a problem hiding this comment.
I'm playing around with removing that optimization though, so maybe let's keep this as is.
Comment on lines
246
to
266
| ## Narrowing after awaiting a `NoReturn` async function | ||
|
|
||
| When an async function is annotated as returning `NoReturn`, awaiting it should be treated as | ||
| terminal, just like calling a synchronous `NoReturn` function. | ||
|
|
||
| ```py | ||
| from typing import NoReturn | ||
|
|
||
| async def stop() -> NoReturn: | ||
| raise NotImplementedError | ||
|
|
||
| async def main(flag: bool): | ||
| if flag: | ||
| x = 1 | ||
| await stop() | ||
| else: | ||
| x = 2 | ||
| pass | ||
|
|
||
| reveal_type(x) # revealed: Literal[2] | ||
| ``` |
Contributor
There was a problem hiding this comment.
This test has nothing to do with narrowing, and should therefore go into the reachability section.
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
Closes astral-sh/ty#2789.