-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
Description
Bug report
Bug description:
I would expect inspect.iscoroutinefunction to only return True for coroutine functions and those functions decorated with inspect.markcoroutinefunction, but it also returns True for the inspect module object itself.
Python 3.12.3 (main, Apr 23 2024, 09:16:07) [GCC 13.2.1 20240417] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> inspect.iscoroutinefunction(inspect)
True
This is a regression - in 3.11 it returned False.
The issue seems to be that iscoroutinefunction(obj) checks if obj._is_coroutine_marker is inspect._is_coroutine_marker, and this check passes for obj being the inspect module.
I tested 3.12.3 but I can see that the implementation of iscoroutinefunction hasn't changed between 3.12.3 and the main branch (GitHub link, in particular inspect.py line 412), so I believe the issue is still present in tip of main.
The bug was triggered in our proprietary codebase in a test file that defines a number of tests as coroutine functions and then uses checks = {k: v for k, v in globals().items() if inspect.iscoroutinefunction(v)} to iterate over all tests. This broke when updating from 3.11 to 3.12.
I think inspect.iscoroutinefunction(inspect) returning True is quite surprising behavior so I would propose changing inspect.py so it doesn't use the same attribute name _is_coroutine_marker as the global variable _is_coroutine_marker, perhaps by renaming the global to _is_coroutine_marker_object or something.
CPython versions tested on:
3.12
Operating systems tested on:
Linux