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

gh-94912: Adjusted check for non-standard coroutine function marker. #100935

Merged
merged 1 commit into from
Jan 11, 2023
Merged
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
2 changes: 0 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,6 @@ def _has_coroutine_mark(f):
while ismethod(f):
f = f.__func__
f = functools._unwrap_partial(f)
if not (isfunction(f) or _signature_is_functionlike(f)):
Copy link
Member

@sobolevn sobolevn Jan 11, 2023

Choose a reason for hiding this comment

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

What else can we mark as coroutine-like? Functions, methods, other callables like partial, classes. Anything else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @sobolevn. I'm not sure I follow 🤔

The cases that have occurred up to Python 3.11, using the old asyncio version, have been callable classes and sync functions. Those are (now) covered. We also added tests for static and class method and lambdas (but I've not seen anyone using those).

The other case that would simplify client code is automatic detection of the __call__ method (either async def or marked) but I think the preference is not to add that.

return False
return getattr(f, "_is_coroutine_marker", None) is _is_coroutine_marker

def markcoroutinefunction(func):
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ async def __call__(self):
self.assertFalse(inspect.iscoroutinefunction(Cl))
# instances with async def __call__ are NOT recognised.
self.assertFalse(inspect.iscoroutinefunction(Cl()))
# unless explicitly marked.
self.assertTrue(inspect.iscoroutinefunction(
inspect.markcoroutinefunction(Cl())
))

class Cl2:
@inspect.markcoroutinefunction
Expand All @@ -232,6 +236,10 @@ def __call__(self):
self.assertFalse(inspect.iscoroutinefunction(Cl2))
# instances with marked __call__ are NOT recognised.
self.assertFalse(inspect.iscoroutinefunction(Cl2()))
# unless explicitly marked.
self.assertTrue(inspect.iscoroutinefunction(
inspect.markcoroutinefunction(Cl2())
))

class Cl3:
@inspect.markcoroutinefunction
Expand Down