8000 gh-94912: Added marker for non-standard coroutine function detection by carltongibson · Pull Request #99247 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-94912: Added marker for non-standard coroutine function detection #99247

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

Merged
merged 16 commits into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added tests for lambda, @classmethod, and @staticmethod cases.
  • Loading branch information
carltongibson committed Nov 23, 2022
commit d156eab1859aaade6b3e096a25caefdc93731ede
2 changes: 2 additions & 0 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ def markcoroutinefunction(func):
"""
Decorator to ensure callable is recognised as a coroutine function.
"""
if hasattr(func, '__func__'):
func = func.__func__
func.__code__ = func.__code__.replace(
co_flags=func.__code__.co_flags | CO_COROUTINE
)
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ def fn3():
return _fn3()

self.assertTrue(inspect.iscoroutinefunction(fn3))
self.assertTrue(
inspect.iscoroutinefunction(
inspect.markcoroutinefunction(lambda: _fn3())
)
)

class Cl:
async def __call__(self):
Expand All @@ -226,6 +231,20 @@ def __call__(self):
self.assertFalse(inspect.iscoroutinefunction(Cl2))
self.assertTrue(inspect.iscoroutinefunction(Cl2()))

class Cl3:
@inspect.markcoroutinefunction
@classmethod
def do_something_classy(cls):
pass

@inspect.markcoroutinefunction
@staticmethod
def do_something_static():
pass

self.assertTrue(inspect.iscoroutinefunction(Cl3.do_something_classy))
self.assertTrue(inspect.iscoroutinefunction(Cl3.do_something_static))

self.assertFalse(
inspect.iscoroutinefunction(unittest.mock.Mock()))
self.assertTrue(
Expand Down
0