forked from litl/backoff
-
Notifications
You must be signed in to change notification settings - Fork 2
fix: Preserve the types of annotated async functions wrapped with on_exception or on_predicate
#202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
edgarrmondragon
merged 2 commits into
python-backoff:main
from
MeisQuietude:fix/async-decorator-typing
Aug 28, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import asyncio | ||
| import sys | ||
|
|
||
| if sys.version_info >= (3, 11): | ||
| from typing import assert_type | ||
| else: | ||
| from typing_extensions import assert_type | ||
|
|
||
| import backoff | ||
|
|
||
|
|
||
| @backoff.on_exception(backoff.expo, ValueError) | ||
| def fetch_sync_with_exception(value: str, *, suffix: str) -> str: | ||
| return value + suffix | ||
|
|
||
|
|
||
| @backoff.on_predicate(backoff.expo) | ||
| def fetch_sync_with_predicate(value: str, *, suffix: str) -> str: | ||
| return value + suffix | ||
|
|
||
|
|
||
| @backoff.on_exception(backoff.expo, ValueError) | ||
| async def fetch_async_with_exception(value: str, *, suffix: str) -> str: | ||
| return value + suffix | ||
|
|
||
|
|
||
| @backoff.on_predicate(backoff.expo) | ||
| async def fetch_async_with_predicate(value: str, *, suffix: str) -> str: | ||
| return value + suffix | ||
|
|
||
|
|
||
| def consume_sync_decorated_functions() -> None: | ||
| exception_result = fetch_sync_with_exception("exception", suffix=" result") | ||
| predicate_result = fetch_sync_with_predicate("predicate", suffix=" result") | ||
| assert_type(exception_result, str) | ||
| assert_type(predicate_result, str) | ||
|
|
||
|
|
||
| async def consume_async_decorated_functions() -> None: | ||
| exception_result = await fetch_async_with_exception("exception", suffix=" result") | ||
| predicate_result = await fetch_async_with_predicate("predicate", suffix=" result") | ||
| assert_type(exception_result, str) | ||
| assert_type(predicate_result, str) | ||
|
|
||
| exception_task = asyncio.create_task( | ||
| fetch_async_with_exception("exception", suffix=" task") | ||
| ) | ||
| predicate_task = asyncio.create_task( | ||
| fetch_async_with_predicate("predicate", suffix=" task") | ||
| ) | ||
| assert_type(exception_task, asyncio.Task[str]) | ||
| assert_type(predicate_task, asyncio.Task[str]) | ||
| assert_type(await exception_task, str) | ||
| assert_type(await predicate_task, str) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import backoff | ||
|
|
||
|
|
||
| @backoff.on_exception(backoff.expo, ValueError) | ||
| def fetch_with_exception(value: str): | ||
| return value | ||
|
|
||
|
|
||
| @backoff.on_predicate(backoff.expo) | ||
| def fetch_with_predicate(value: str): | ||
| return value | ||
|
|
||
|
|
||
| def consume_sync_results() -> str: | ||
| exception_result = fetch_with_exception("exception") | ||
| predicate_result = fetch_with_predicate("predicate") | ||
| return exception_result.upper() + predicate_result.upper() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this actually exercise something? i.e. does this fail somehow without the decorator typing changes? Otherwise, it's not clear to me what this tests.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This covers the sync side of the decorator typing change. Pyrefly infers str for these unannotated sync functions, and the decorated calls must preserve that inferred type instead of becoming coroutines. The strict mypy test cannot cover this case because it requires explicit return annotations.
it guards against an alternative overload-based implementation of this fix.
For this sync function:
The results are:
Callable[[_CallableT], _CallableT]: Pyrefly preserves the inferred sync return type, so this passes.Coroutine has no attribute upper.This test prevents fixing the async case by accidentally breaking unannotated sync functions.
--
But ofc if you're willing to remove it, just say and I do it. I appeciate your work :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. I did consider exploring the
@overloadoption, so it's good to have in case I forget that it won't work 😅.Thanks!