[New-bugs-announce] [issue33261] inspect.isgeneratorfunction fails on hand-created methods

Jeroen Demeyer report at bugs.python.org
Wed Apr 11 04:59:53 EDT 2018

New submission from Jeroen Demeyer <J.Demeyer at UGent.be>:

The inspect functions isgeneratorfunction, iscoroutinefunction, isasyncgenfunction can fail on methods that do not have a __code__ attribute:

>>> from types import MethodType
>>> class Callable:
...     def __call__(self, *args):
...         return args
>>> m = MethodType(Callable(), 42)
>>> m()
>>> import inspect
>>> inspect.iscoroutinefunction(m)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.6/inspect.py", line 186, in iscoroutinefunction
    object.__code__.co_flags & CO_COROUTINE)
AttributeError: 'Callable' object has no attribute '__code__'

This was discovered while working on PEP 575, but it is really an independent issue that should be fixed anyway.

components: Library (Lib)
messages: 315187
nosy: jdemeyer
priority: normal
severity: normal
status: open
title: inspect.isgeneratorfunction fails on hand-created methods
type: behavior
versions: Python 3.6, Python 3.7, Python 3.8

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list