Skip to content

gh-81371: implementing checking async generator methods #102129

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

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 8 additions & 0 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,20 @@ def markcoroutinefunction(func):
func._is_coroutine_marker = _is_coroutine_marker
return func

_agen_methods = ("__anext__", "aclose", "asend", "athrow")

def iscoroutinefunction(obj):
"""Return true if the object is a coroutine function.

Coroutine functions are normally defined with "async def" syntax, but may
be marked via markcoroutinefunction.
"""
#check if obj is async gen method and returns awaitable
Copy link
Member

Choose a reason for hiding this comment

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

Please, at least add tests for this feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as soon as I fully understand the original question

obj = functools._unwrap_partial(obj)
is_valid = isbuiltin(obj) or ismethodwrapper(obj)
if is_valid and isasyncgen(obj.__self__) and obj.__name__ in _agen_methods:
return True
#now regular case
return _has_code_flag(obj, CO_COROUTINE) or _has_coroutine_mark(obj)

def isasyncgenfunction(obj):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This PR may fix the issue 81371
Copy link
Member

Choose a reason for hiding this comment

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

May? Or does it? Please, also specify what the issue is. These are user-facing notes. It must be clear without a github link.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

May? Or does it? Please, also specify what the issue is. These are user-facing notes. It must be clear without a github link.

I'm not 100% sure if it does. In the original issue those methods just returns some awaitable objects that doesn't pass the check. And I fixed it, but only for this certain example (I will add the tests soon). The obstacle is that I don't know if there are similar functions somewhere in the inerpreter. I'm not an internals expert. It would be great if you or someone else could suggest me such places.