Skip to content
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

inspect.iscoroutinefunction(inspect) returns True #120200

Closed
Mortal opened this issue Jun 7, 2024 · 1 comment
Closed

inspect.iscoroutinefunction(inspect) returns True #120200

Mortal opened this issue Jun 7, 2024 · 1 comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@Mortal
Copy link
Contributor

Mortal commented Jun 7, 2024

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

Linked PRs

@Mortal Mortal added the type-bug An unexpected behavior, bug, or error label Jun 7, 2024
@Eclips4 Eclips4 added 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir labels Jun 7, 2024
sobolevn added a commit to sobolevn/cpython that referenced this issue Jun 7, 2024
sobolevn added a commit to sobolevn/cpython that referenced this issue Jun 7, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 7, 2024
…orner case (pythonGH-120214)

(cherry picked from commit 10fb1b8)

Co-authored-by: Nikita Sobolev <[email protected]>
sobolevn added a commit to sobolevn/cpython that referenced this issue Jun 7, 2024
…True` corner case (pythonGH-120214)

(cherry picked from commit 10fb1b8)

Co-authored-by: Nikita Sobolev <[email protected]>
sobolevn added a commit that referenced this issue Jun 7, 2024
…corner case (GH-120214) (#120237)

gh-120200: Fix `inspect.iscoroutinefunction(inspect) is True` corner case (GH-120214)
(cherry picked from commit 10fb1b8)

Co-authored-by: Nikita Sobolev <[email protected]>
sobolevn added a commit that referenced this issue Jun 7, 2024
…corner case (GH-120214) (#120239)

* [3.12] gh-120200: Fix `inspect.iscoroutinefunction(inspect) is True` corner case (GH-120214)
(cherry picked from commit 10fb1b8)

Co-authored-by: Nikita Sobolev <[email protected]>
@sobolevn
Copy link
Member

sobolevn commented Jun 7, 2024

Thanks everyone!

@sobolevn sobolevn closed this as completed Jun 7, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants