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
make asyncio.iscoroutinefunction
a deprecated alias of inspect.iscoroutinefunction
and remove asyncio.coroutines._is_coroutine
#94912
Comments
asyncio.iscoroutinefunction
a deprecated alias of inspect.iscoroutinefunction
and remove asyncio.coroutines._is_coroutine
changing async def _wrap_awaitable(awaitable):
"""Helper for asyncio.ensure_future().
Wraps awaitable (an object with __await__) into a coroutine
that will later be wrapped in a Task by ensure_future().
"""
return await awaitable would have a few subtle changes - eg the |
the |
aha I've just checked and the |
So that's the ancient external asyncio project, which nobody should use any more. Does the corresponding code (or corresponding commits) exist in the stdlib version? |
Yep, the fix was ported everywhere but the now redundant workarounds were left in, there's more info here https://github.com/python/cpython/pull/94926/files#r922839081 |
Before making it a deprecated alias, it would be better to investigate:
|
how about if I just add a deprecation notice and leave the implementation alone?
|
That's rarely a good strategy. Many people don't read the docs and keep using the function (if they are using it) and so once the deprecated function is deleted their code breaks without warning. |
The alternative is |
I thought we were talking about putting a warning in it? It should only warn when it's going to return True but the inspect function would return False. |
ok I've pushed that change |
Python 3.11 has removed support for `@asyncio.coroutine` and with it removed the documentation references for `asyncio.iscoroutinefunction()` such that the intersphinx reference is unavailable. The implementation of `asyncio.iscoroutinefunction()` still exists, however: https://github.com/python/cpython/blob/a1092f62492a3fcd6195bea94eccf8d5a300acb1/Lib/asyncio/coroutines.py#L21-L24 It looks as though it will be removed in the future: python/cpython#94912 As we still need to support Python < 3.11 where `@asyncio.coroutine` may be used we will likely need to vendor this until Python 3.11 is the minimum supported version when `inspect.iscoroutinefunction()` can be used directly instead. That will likely not be required until 3.12 though.
The Is there an official way to do this? (I need to dig into the inspect version of the check to see if we can mimic what we're doing already, but on the initial pass substituting in If not can we pause before removing it? @gvanrossum had #67707 (comment) — which suggests just calling the thing and seeing if it returned a coroutine object, but in Django's case we have a middleware and view functions which we're adapting for later use. Having a way of saying, No this is a coroutine function just by inspecting is pretty essential. Thanks! |
The question is, what do those libraries do differently when the result is true or false? Asyncio itself only uses But I'm guessing you have a different use in mind.
I'm happy to pause this work until you come back with an answer to the above question (but don't wait until 3.12 is deemed feature complete please).
Let me see if I understand this. You have some code that essentially wants to tread
differently from
Maybe if it's an async def you want to But now there's a special case
and you want to treat
But why? Couldn't you just write
? |
Hi @gvanrossum — Thank you for your thoughtful reply, very insightful.
Absolutely. Let's see if we can resolve this now, not least because selfishly Your initial analysis looks correct: async def callback1(...): ...
async def _real_callback3(...): ...
def callback3(...):
return _real_callback3(...)
callback3._is_coroutine = asyncio._is_coroutine This is exactly what we're doing.
Paraphrasing:
Let me give you the examples. Django ViewsSo, Django being a web-framework, views are callables that turn requests into responses. At its simplest, you declare a function to do this:
You can also define equivalent async views:
Both types of view can be run under both WSGI and ASGI:
So far so good, if you have an But Django also allows you to define class-based views. Normally these define handlers per-HTTP verb, so: from django.views import View
class MyView(View):
def get(self, *args, **kwargs): ... These can also define async handlers:
In order to have per-request view instances, allowing you to store state on There are several (sync) layers involved here — We're doing that currently by marking the wrapper function returned from In this case, I think we could branch on the definition of the returned view to make it My concern would be the amount of duplication, over simply setting an attribute on the function. Django MiddlewareThe example with middleware is a little more complex. Before calling a view Django passes the request through a series of middleware, Like views, middleware can be either sync or async, but they can also support In order to avoid switching more than necessary between sync and async contexts For class based middleware (which unlike views are single instance callables)
|
Some quick thoughts: The root of the "problem" is something that seems more prevailing in web frameworks than in the core of Python, i.e., introspecting something and taking a different action based on the outcome. A very old example is web template libraries that can name objects that may or may not be callable, and have no explicit "call it" syntax -- they just implicitly call when the object is callable. In the core we prefer explicit over implicit, or we'd use a method that each type can redefine. But the web world is different, and I'm okay whit that. I think that the way forward is a decorator that sets the marker, and that decorator ought to be part of the inspect API, so that inspect can look for it. Would you be willing to help design such an API and implement it? |
Just to extend from what Carlton said on this - as the original author of all the async piping through Django, the key thing I needed to know when I first designed all this is if a callable is going to return a coroutine or not without calling it, as if it's a synchronous function then we've already accidentally triggered the side-effects if we call it. While I like the simplicity of the Python design of "asynchronous callables are just callables that return a coroutine", this is one case where it's quite hard to duck-type as you can't inspect the object without potential side-effects. It's essentially missing type information - we could use type hints at runtime to, in theory, perform the same marking, and we could also indeed just vendor our own mechanism for this and continue placing an attribute on the callable object (after all, most of what is happening here is Django view classes being inspected by Django request flows, so we do control both ends). You are totally correct that in a nice, clean implementation from a blank slate, we'd have something explicit like two different kinds of URL router or middleware definition, or maybe even just make it all coroutine-by-default - alas, backwards compatibility meant we had to continue some of Django's existing design patterns to avoid making a huge amount of required upgrade work for people. When you propose a decorator that sets a marker, are you thinking something similar to |
Yes, that's exactly what I was thinking. We need to think a bit carefully about deprecating |
Thanks for your understanding @gvanrossum.
Yes. The Django template language ≈ does this.
Ha. Sneaky. /me looks at https://peps.python.org/pep-0693/#schedule |
This shouldn't require a PEP or anything like that. Just think through the API a bit. You could start by writing the code and reason back from what you learned by trying to do that. |
I can certainly begin. Let me track down the tests for |
Great. Please add me to the PR. |
…99247) This introduces a new decorator `@inspect.markcoroutinefunction`, which, applied to a sync function, makes it appear async to `inspect.iscoroutinefunction()`.
* main: pythongh-89727: Fix os.walk RecursionError on deep trees (python#99803) Docs: Don't upload CI artifacts (python#100330) pythongh-94912: Added marker for non-standard coroutine function detection (python#99247) Correct CVE-2020-10735 documentation (python#100306) pythongh-100272: Fix JSON serialization of OrderedDict (pythonGH-100273) pythongh-93649: Split tracemalloc tests from _testcapimodule.c (python#99551) Docs: Use `PY_VERSION_HEX` for version comparison (python#100179) pythongh-97909: Fix markup for `PyMethodDef` members (python#100089) pythongh-99240: Reset pointer to NULL when the pointed memory is freed in argument parsing (python#99890) pythongh-99240: Reset pointer to NULL when the pointed memory is freed in argument parsing (python#99890) pythonGH-98831: Add DECREF_INPUTS(), expanding to DECREF() each stack input (python#100205) pythongh-78707: deprecate passing >1 argument to `PurePath.[is_]relative_to()` (pythonGH-94469)
graingert commentedJul 17, 2022
•
edited by bedevere-bot
currently asyncio.iscoroutinefunction and inspect.iscoroutinefunction behave differently in confusing and hard to document ways. It's possible to bring them into alignment but I think it would be better to make
asyncio.iscoroutinefunction
a deprecated alias ofinspect.iscoroutinefunction
and removeasyncio.coroutines._is_coroutine
.This is now possible with the recent removal of
@asyncio.coroutine
and support for AsyncMock and other duck-type functions in inspect.iscoroutinefunctionthe only caveats are - what should happen to users of the
asyncio.coroutines._is_coroutine
mark? eg:cpython/Lib/asyncio/tasks.py
Lines 686 to 696 in 1c0cf0a
and all of these:
The text was updated successfully, but these errors were encountered: