Skip to content

Commit

Permalink
[DRAFT] Initial sketch of iscoroutinefunction shim for PY312
Browse files Browse the repository at this point in the history
cc @andrewgodwin
Refs: python/cpython#99247

This would be needed for Django 4.2, which would support PY312 on release of that.
  • Loading branch information
carltongibson committed Nov 23, 2022
1 parent 30ecdef commit 7f52d4a
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions asgiref/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,41 @@ def _restore_context(context):
cvar.set(context.get(cvar))


# Python 3.12 deprecates asyncio.iscoroutinefunction() as an alias for
# inspect.iscoroutinefunction(), whilst also removing the _is_coroutine marker.
# The latter is replaced with the inspect.markcoroutinefunction decorator.
# Until 3.12 is the minimum supported Python version, provide a shim.
# Django 4.0 only supports 3.8+, so don't concern with the _or_partial backport.

# Type hint: should be generic: whatever T it takes it returns. (Same id)
def markcoroutinefunction(func: Any) -> Any:
if hasattr(inspect, "markcoroutinefunction"):
return inspect.markcoroutinefunction(func)
else:
func._is_coroutine = asyncio.coroutines._is_coroutine
return func


def iscoroutinefunction(func: Any) -> bool:
if hasattr(inspect, "markcoroutinefunction"):
return inspect.iscoroutinefunction(func)
else:
return asyncio.iscoroutinefunction(func)


def _iscoroutinefunction_or_partial(func: Any) -> bool:
# Python < 3.8 does not correctly determine partially wrapped
# coroutine functions are coroutine functions, hence the need for
# this to exist. Code taken from CPython.
if sys.version_info >= (3, 8):
return asyncio.iscoroutinefunction(func)
return iscoroutinefunction(func)
else:
while inspect.ismethod(func):
func = func.__func__
while isinstance(func, functools.partial):
func = func.func

return asyncio.iscoroutinefunction(func)
return iscoroutinefunction(func)


class ThreadSensitiveContext:
Expand Down

1 comment on commit 7f52d4a

@andrewgodwin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I've been following the cPython ticket - I think this is a reasonable outcome!

Please sign in to comment.