-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
inspect.iscoroutinefunction / isgeneratorfunction / isasyncgenfunction can't handle partialmethod objects #82545
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
Comments
This is a follow-up to bpo-33261, which added general support for detecting generator / coroutine / async generator functions wrapped in partials. It appears that partialmethod objects were missed out. While a partialmethod object will produce a functools.partial() object on binding to an instance, the .func attribute of that partial is a bound method, not a function, and the current _has_code_flag implementation unwraps methods *before* it unwraps partials. Next, binding to a class produces a partialmethod._make_unbound_method.._method wrapper function. _unwrap_partial can't unwrap this, as it doesn't handle this case; it could look for the Test case: import inspect
import functools
class Foo:
async def bar(self, a): return a
ham = partialmethod(bar, "spam")
print(inspect.iscoroutinefunction(Foo.bar) # True
print(inspect.iscoroutinefunction(Foo.ham) # False
instance = Foo()
print(inspect.iscoroutinefunction(instance.bar) # True
print(inspect.iscoroutinefunction(instance.ham) # False |
I've recently ran into this via unittest.mock.patch.object. Mocking an asynchronous partial method returns MagicMock and not AsyncMagicMock, causing some pretty hard to debug failures. |
see also #67707 |
The above test case slightly changed: def test_dummy():
import inspect
import functools
class Foo:
async def bar(self, a): return a
ham = functools.partialmethod(bar, "spam")
assert inspect.iscoroutinefunction(Foo.bar) # True
assert not inspect.iscoroutinefunction(Foo.ham) # False
instance = Foo()
assert inspect.iscoroutinefunction(instance.bar) # True
assert inspect.iscoroutinefunction(instance.ham) # False IS NOT failing for Python |
Why did you change the test to add |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: