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

Get iscoroutinefunction() from inspect rather than asyncio #2855

Merged

Conversation

musicinmybrain
Copy link
Contributor

Python 3.14.0a3 issues a DeprecationWarning for
asyncio.iscoroutinefunction(), stating that it is deprecated and slated for removal in Python 3.16, and recommending
inspect.iscoroutinefunction() instead. This change resolves that DeprecationWarning.

Summary

Running the tests with Python 3.14.0a3 fails with several errors like:

_______________________________________________________________________________ ERROR collecting tests/middleware/test_base.py _______________________________________________________________________________
tests/middleware/test_base.py:77: in <module>
    Route("/", endpoint=homepage),
starlette/routing.py:230: in __init__
    self.app = request_response(endpoint)
starlette/routing.py:66: in request_response
    func if is_async_callable(func) else functools.partial(run_in_threadpool, func)  # type:ignore
starlette/_utils.py:39: in is_async_callable
    return asyncio.iscoroutinefunction(obj) or (callable(obj) and asyncio.iscoroutinefunction(obj.__call__))
/usr/lib64/python3.14/asyncio/coroutines.py:23: in iscoroutinefunction
    warnings._deprecated("asyncio.iscoroutinefunction",
/usr/lib64/python3.14/warnings.py:668: in _deprecated
    warn(msg, DeprecationWarning, stacklevel=3)
E   DeprecationWarning: 'asyncio.iscoroutinefunction' is deprecated and slated for removal in Python 3.16; use inspect.iscoroutinefunction() instead

This PR just implements the recommended migration from asyncio.iscoroutinefunction() to inspect.iscoroutinefunction() in starlette/_utils.py. Note that starlete/routing.py already uses inspect.iscoroutinefunction() even before this PR:

return inspect.iscoroutinefunction(obj)

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!) I think this is a small enough change for a “drive-by” PR, but this can be moved to a discussion if you think a less trivial approach is needed for some reason.
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. N/A – this resolves test errors in Python 3.14.0a3, so no new tests are required.
  • I've updated the documentation accordingly. N/A – I don’t think any documentation updates are required.

Python 3.14.0a3 issues a DeprecationWarning for
asyncio.iscoroutinefunction(), stating that it is deprecated and slated
for removal in Python 3.16 and recommending
inspect.iscoroutinefunction() instead. This change resolves that
DeprecationWarning.
@Kludex Kludex merged commit cbddcaf into encode:master Jan 28, 2025
5 checks passed
@Kludex
Copy link
Member

Kludex commented Jan 28, 2025

Thanks @musicinmybrain

@Kludex
Copy link
Member

Kludex commented Jan 28, 2025

Do you need a release or you fetch from master?

@musicinmybrain
Copy link
Contributor Author

Do you need a release or you fetch from master?

Thanks for asking. I can easily apply https://github.com/encode/starlette/pull/2855.patch for now, so there’s no hurry to issue a release.

I think I’m stuck on Starlette 0.45.0 in Fedora for now anyway, until we can get our python-anyio package updated from 3.7.1 to a 4.x release. I think all the blockers for that have finally been dealt with, but it still requires an updated impact check and some coordination.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants