Code Monkey home page Code Monkey logo

Comments (1)

mentalisttraceur avatar mentalisttraceur commented on June 29, 2024

Python 3.12 finally added inspect.markcoroutinefunction to solve this.

async def foo():
    ...

inspect.markcoroutinefunction(foo)

Of course you could wrap decorator if you don't want to do this manually for every decorated async function.


Background

Python allows creating callables which are not coroutines (not defined with async) but still return awaitables (work with await).

So everyone who uses inspect.iscoroutinefunction to decide whether to use await is technically wrong (but Python itself has at least half the blame here - the need for something like inspect.markcoroutinefunction has been blatantly obvious for at least five years now, and arguably should've been obvious when first designing the async/await language addition back in 3.5).

inspect.iscoroutinefunction is a half check. If it returns True we know we should use await, but if it returns False we haven't actually disproven if we should use await.

So instead of

if inspect.iscoroutinefunction(f):
    result = await f(...)  # correct
else:
    result = f(...)  # could be wrong, f might return an awaitable

the universally correct solution is

result = f(...)
if inspect.isawaitable(result):
    result = await result

And if we're in a wrapper that needs to support both sync and async functions, you just return whatever f returned, which makes you transparent w.r.t. to whether an await is needed on your return value:

return f(...)

I infer decorator is using that last method, which is technically the most correct thing to do, but sadly it just pushes this obscure edge-case further up the call-stack, making it everyone else's problem.

So decorator could be written to progressively enhance: try importing inspect.markcoroutinefunction - if it's not available just don't use it, but if it is, check the wrapped callable with inspect.iscoroutinefunction and mark the wrapper to match.

from decorator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.