-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
what's the correct type for types.coroutine decorated generators? #2907
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
Comments
I believe you need to give it the type |
yep, that seems to do the trick. thank you very much! This type isn't presently documented for python 3.6's typing lib, and it doesn't show up in that lib's static attributes, either:
Should I open a new issue with python directly? |
Yeah, |
No, it's a crutch to make up for the lack of duck typing in mypy. |
Is there a preferred place/format for me to contribute some documentation? I always think I'll make time to contribute some code to mypy, but I never end up doing so, but I could probably type up some quick explanation of this type. |
I started a PR here with an example I found in the documentation updated and a little explanatory note added. Happy to add more or do it differently. |
Hm, the root cause of your original example is a forward reference to a decorated function, not anything having to do with All the magic processing for So, apart from the forward reference issue, the intention is really that a function decorated with |
Oops -- that's good to know! I won't tell people about it, then. >_> |
ho boy - well, thanks for the analysis and explanation! I really appreciate it, and I'm happy to have a way to move forward. Is the forward reference bug in the code in mpy/checker.py, as well? If nothing else, I'd like to understand it better. |
NP. The forward reference issue is emergent. Basically the code in that sets the return type of the |
I recently ran into a problem similar to this, but a little more devious, and so none of the suggestions could work for me. I have a function that wraps a callable inside a Coroutine and returns the generated Coroutine. A simplified version looks like: def _makeCoroutine(func: Callable[str, str], executor: ThreadPoolExecutor, depends: Set[str]) ->\
Callable[[str, str], Coroutine[Set[str], str, str]]:
@types.coroutine
def wrapper(a: str, b: str)-> str:
executor.submit(func, a, b)
result = yield depends
return result
return wrapper
async def get_result(func: Callable[str, str], executor: ThreadPoolExecutor, depends: Set[str]):
return await _makeCoroutine(func, executor, depends)("hello", "world") There are details left out about why the structure is like this, but this is the core of the problem. I am trying to type the return of the function I have tried a few return types for |
|
I cannot seem to get a
types.coroutine
decorated generator to typecheck correctly.I checked the tests out, and they seem to indicate that the right thing to do is to annotate the return as "typing.Generator", but when I do that, mypy complains that I cannot await that.
coro_example.py
:I thought perhaps I was subtly mistyping something, so I copy/pasted the code from the tests (from line 346 to line 403) and ran that, and got similar errors:
If I type the generator as a
typing.Awaitable
instead, mypy tells me I have to type the generator as a generator or one of its supertypes.I've tried this with python 3.6 and the latest github and pip versions:
mypy (0.480.dev0)
(installed viapip install git+https://github.com/python/mypy
)mypy (0.471)
(installed viapip install mypy
)Am I doing it wrong, or is there a bug?
The text was updated successfully, but these errors were encountered: