Repository navigation
Make iscoroutinefunction TypeIs - #15928
Open
InvincibleRMC wants to merge 6 commits into
Open
InvincibleRMC wants to merge 6 commits into
InvincibleRMC wants to merge 6 commits into
Conversation
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
…/typeshed into iscoroutinefunction-TypeIs
This comment has been minimized.
This comment has been minimized.
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
This comment has been minimized.
This comment has been minimized.
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: aiohttp (https://github.com/aio-libs/aiohttp)
- aiohttp/worker.py:76:26: error: Too few arguments [call-arg]
+ aiohttp/worker.py:73:14: error: Left operand of "or" is always false [redundant-expr]
- aiohttp/worker.py:76:26: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-call-arg for more info
+ aiohttp/worker.py:73:14: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-redundant-expr for more info
- aiohttp/worker.py:76:26: note: "wsgi" is considered instance variable, to make it class variable use ClassVar[...]
+ aiohttp/worker.py:76:13: error: Statement is unreachable [unreachable]
+ aiohttp/worker.py:88:9: error: Statement is unreachable [unreachable]
discord.py (https://github.com/Rapptz/discord.py)
- discord/ext/commands/core.py:2373: error: "Callable[[Command[Any, Any, Any] | Callable[..., Coroutine[Any, Any, Any]]], Command[Any, Any, Any] | Callable[..., Coroutine[Any, Any, Any]]]" has no attribute "predicate" [attr-defined]
- discord/ext/commands/core.py:2448: error: "Callable[[Command[Any, Any, Any] | Callable[..., Coroutine[Any, Any, Any]]], Command[Any, Any, Any] | Callable[..., Coroutine[Any, Any, Any]]]" has no attribute "predicate" [attr-defined]
|
| def iscoroutinefunction(obj: Callable[..., Coroutine[Any, Any, Any]]) -> bool: ... | ||
| @overload | ||
| def iscoroutinefunction(obj: Callable[_P, Awaitable[_T]]) -> TypeGuard[Callable[_P, CoroutineType[Any, Any, _T]]]: ... | ||
| def iscoroutinefunction(obj: Callable[_P, Awaitable[_T]]) -> TypeIs[Callable[_P, CoroutineType[Any, Any, _T]]]: ... |
Member
There was a problem hiding this comment.
This is incorrect because it does not return true for all callables that return a CoroutineType.
There was a problem hiding this comment.
Perhaps a new type needs to be added to represent async functions, because with the current typing system there is no way to type
def foo(a) -> Awaitable[int]:
return asyncio.Future()as being distinct from
async def foo(a) -> int:
return 5and no way to distinguish them at runtime other than iscoroutinefunction, which can't be accurately typed as the annotations indicate a narrowing on the return type, while the logic of iscoroutinefunction is using flags on the function's code object.
Thus, you can't know definitively if a function is asynchronous in the current system.
This branch has not been deployed
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.
Attempt to address #15529