[New-bugs-announce] [issue38364] inspect.iscoroutinefunction / isgeneratorfunction / isasyncgenfunction can't handle partialmethod objects

Martijn Pieters report at bugs.python.org
Thu Oct 3 09:28:44 EDT 2019


New submission from Martijn Pieters <mj at python.org>:

This is a follow-up to #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.<locals>._method wrapper function. _unwrap_partial can't unwrap this, as it doesn't handle this case; it could look for the `_partialmethod` attribute and follow that to find the `.func` attribute.

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

----------
components: Library (Lib)
messages: 353849
nosy: mjpieters
priority: normal
severity: normal
status: open
title: inspect.iscoroutinefunction / isgeneratorfunction / isasyncgenfunction can't handle  partialmethod objects
type: behavior
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38364>
_______________________________________


More information about the New-bugs-announce mailing list