Skip to content
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

Python 3.11 support: Don't use asyncio.coroutine if it's not available #267

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions cached_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
import asyncio
except (ImportError, SyntaxError):
asyncio = None
if asyncio:
try:
iscoroutinefunction = asyncio.iscoroutinefunction
except AttributeError:
# Python 3.11: @asyncio.coroutine was removed
from inspect import iscoroutinefunction


class cached_property(object):
Expand All @@ -30,22 +36,14 @@ def __get__(self, obj, cls):
if obj is None:
return self

if asyncio and asyncio.iscoroutinefunction(self.func):
return self._wrap_in_coroutine(obj)
if asyncio and iscoroutinefunction(self.func):
value = asyncio.ensure_future(self.func(obj))
else:
value = self.func(obj)

value = obj.__dict__[self.func.__name__] = self.func(obj)
obj.__dict__[self.func.__name__] = value
return value

def _wrap_in_coroutine(self, obj):
@wraps(obj)
@asyncio.coroutine
def wrapper():
future = asyncio.ensure_future(self.func(obj))
obj.__dict__[self.func.__name__] = future
return future

return wrapper()


class threaded_cached_property(object):
"""
Expand Down
6 changes: 5 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
# Whether the async and await keywords work
has_async_await = sys.version_info[0] == 3 and sys.version_info[1] >= 5

# Whether "from asyncio import coroutine" *fails*
version_info = sys.version_info
dropped_asyncio_coroutine = version_info[0] == 3 and version_info[1] >= 11


print("conftest.py", has_asyncio, has_async_await)


collect_ignore = []

if not has_asyncio:
if not has_asyncio or dropped_asyncio_coroutine:
collect_ignore.append("tests/test_coroutine_cached_property.py")

if not has_async_await:
Expand Down
3 changes: 1 addition & 2 deletions tests/test_async_cached_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

def unittest_run_loop(f):
def wrapper(*args, **kwargs):
coro = asyncio.coroutine(f)
future = coro(*args, **kwargs)
future = f(*args, **kwargs)
loop = asyncio.get_event_loop()
loop.run_until_complete(future)

Expand Down