-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Should asyncio.iscoroutinefunction
return some kind of TypeGuard
?
#8009
Comments
I've filed #8057 to improve the situation. It should fix the error emitted for the
Here's how mypy handles your code snippet with my proposed #8057: https://mypy-play.net/?mypy=latest&python=3.10&gist=43dc2daa3e5708d97bfbac1709790590 |
Thanks for making a PR! I read the PR and linked issues but I still don't understand what makes the negative case difficult. I'm not well versed in typing and type theory so if you could explain it in simple terms it would be greatly appreciated. |
I think Eric's explanation here is really good, and I'm not sure I can really do any better if I'm honest :) python/typing#926 (comment)
This is really complicated stuff! I wish I could explain it better :( |
So is the problem essentially that if |
Yes. In general, this can't be guaranteed to be type-safe, so type checkers decline to perform type-narrowing in the negative case for these situations. |
I guess a dirty hack to solve this would be to add a |
This actually wouldn't do much to help the situation since, from the perspective of type checkers, coroutine functions are subtypes of common-or-garden functions! from collections.abc import Callable, Coroutine
from typing import Any, TypeGuard
AnyFunction = Callable[..., Any]
AnyCoroutineFunction = Callable[..., Coroutine[Any, Any, Any]] # A subtype of AnyFunction!
def iscoroutinefunction(obj: object) -> TypeGuard[AnyCoroutineFunction]:
"""Narrow the type to a coroutine function"""
def isnotcoroutinefunction(obj: object) -> TypeGuard[???]:
"""We can't narrow the type to <not a coroutine function>, since <coroutine function> is a subtype of <any old function>""" |
This is further evidence that there is need for StrictTypeGuard or something similar in the type system. |
I have the following use-case of a class that takes a regular function or a coroutine as an attribute, and then provides access to it through two properties. Basically the code looks like this:
Now, I expected mypy to give no errors due to type narrowing from `iscoroutinefunction, but instead I get
After finding this, I started searching through the issues in both mypy and typeshed but couldn't find anything related to
iscoroutinefunction
. I then looked into typeshed and saw that the return type ofiscoroutinefunction
is justbool
, and not aTypeGuard
, despite the similariscoroutine
having one. This makes me think thatiscoroutinefunction
is annotated like that on purpose.So my question is, should the return type of
asyncio.iscoroutinefunction
be aTypeGuard
of some kind?I'm new to the more advanced features of Python typing, and my naive implementation would be something like
If you feel like this should be changed, I'd be happy to submit a PR once I know what the type should be.
The text was updated successfully, but these errors were encountered: