Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-142418: Fix inspect.iscoroutinefunction for marked functools.partial and partialmethod objects#142503
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
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
gh-142418: Fix inspect.iscoroutinefunction for marked functools.partial and partialmethod objects #142503
Changes from all commits
a51db301c4e6b9245a70160843786117107dba7cb999611791f057285d5c324File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -306,10 +306,28 @@ def isgeneratorfunction(obj): | ||
| _is_coroutine_mark = object() | ||
| def _has_coroutine_mark(f): | ||
| while ismethod(f): | ||
| f = f.__func__ | ||
| f = functools._unwrap_partial(f) | ||
| return getattr(f, "_is_coroutine_marker", None) is _is_coroutine_mark | ||
| while True: | ||
Member
| ||
| # Methods: unwrap first (methods cannot be coroutine-marked) | ||
| if ismethod(f): | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. >>>fromfunctoolsimportpartialmethod>>>classMyClass: ... defa(self): ... ... b=partialmethod(a) >>>obj=MyClass() >>>obj.bfunctools.partial(<boundmethodMyClass.aof<__main__.MyClassobjectat0x7fd343f4cc20>>) >>>obj.b.func.__func__<functionMyClass.aat0x7fd343f42cf0>When unwrapping the reference to >>>fromfunctoolsimportpartialmethod>>>classMyFirstClass: ... deff(self, other): ... >>>first=MyFirstClass() >>>classMySecondClass: ... g=partialmethod(first.f) >>>second=MySecondClass() >>>second.g<boundmethodpartialmethod._make_unbound_method.<locals>._methodof<__main__.MySecondClassobjectat0x7fd343f4d400>>>>>second.g.__func__.__partialmethod__.func<boundmethodMyFirstClass.fof<__main__.MyFirstClassobjectat0x7fd343f4d2b0>>Well, it seems to me that the current implementation (in the main branch) is not very well thought out. Personally, I would not rely on it.
| ||
| f = f.__func__ | ||
| continue | ||
| # Direct marker check | ||
| if getattr(f, "_is_coroutine_marker", None) is _is_coroutine_mark: | ||
| return True | ||
| # Functions created by partialmethod descriptors keep a __partialmethod__ reference | ||
| pm = getattr(f, "__partialmethod__", None) | ||
| if isinstance(pm, functools.partialmethod): | ||
| f = pm | ||
| continue | ||
Comment on lines +319 to +323 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, this can also be moved forward by one block to avoid the time spent on obtaining the attribute when it is not necessary. I hope I have not bored you with these micro-optimizations. Comment on lines +319 to +323 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please read the discussion at #142505. | ||
| # partial and partialmethod share .func | ||
| if isinstance(f, (functools.partial, functools.partialmethod)): | ||
| ||
| f = f.func | ||
| continue | ||
Joshua-Ward1 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| return False | ||
| def markcoroutinefunction(func): | ||
| """ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Fix ``inspect.iscoroutinefunction()`` incorrectly returning ``False`` for | ||
| callables wrapped in ``functools.partial`` or ``functools.partialmethod`` when | ||
| explicitly marked with ``inspect.markcoroutinefunction()``. The function now | ||
| detects coroutine markers on wrappers at each unwrap stage. |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems unrelated changes