-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
inspect.iscoroutinefunction(inspect) returns True #120200
Copy link
Copy link
Closed
Labels
3.12only security fixesonly security fixes3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Metadata
Metadata
Assignees
Labels
3.12only security fixesonly security fixes3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Bug report
Bug description:
I would expect
inspect.iscoroutinefunctionto only return True for coroutine functions and those functions decorated withinspect.markcoroutinefunction, but it also returns True for theinspectmodule object itself.This is a regression - in 3.11 it returned False.
The issue seems to be that
iscoroutinefunction(obj)checks ifobj._is_coroutine_marker is inspect._is_coroutine_marker, and this check passes for obj being theinspectmodule.I tested 3.12.3 but I can see that the implementation of
iscoroutinefunctionhasn't changed between 3.12.3 and the main branch (GitHub link, in particularinspect.pyline 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_markeras the global variable_is_coroutine_marker, perhaps by renaming the global to_is_coroutine_marker_objector something.CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs
inspect.iscoroutinefunction(inspect) is Truecorner case #120214inspect.iscoroutinefunction(inspect) is Truecorner case (GH-120214) #120237inspect.iscoroutinefunction(inspect) is Truecorner case (GH-120214) #120239