Title: inspect.iscoroutinefunction(inspect) returns True · Issue #120200 · python/cpython · GitHub
Open Graph Title: inspect.iscoroutinefunction(inspect) returns True · Issue #120200 · python/cpython
X Title: inspect.iscoroutinefunction(inspect) returns True · Issue #120200 · python/cpython
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 i...
Open Graph 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 retu...
X 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 retu...
Opengraph URL: https://github.com/python/cpython/issues/120200
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"inspect.iscoroutinefunction(inspect) returns True","articleBody":"# Bug report\n\n### Bug description:\n\nI 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.\n\n```\nPython 3.12.3 (main, Apr 23 2024, 09:16:07) [GCC 13.2.1 20240417] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n\u003e\u003e\u003e import inspect\n\u003e\u003e\u003e inspect.iscoroutinefunction(inspect)\nTrue\n```\n\nThis is a regression - in 3.11 it returned False.\n\nThe 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.\n\nI 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](https://github.com/python/cpython/blob/47816f465e833a5257a82b759b1081e06381e528/Lib/inspect.py#L405-L429), in particular `inspect.py` line 412), so I believe the issue is still present in tip of main.\n\nThe 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.\n\nI 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.\n\n### CPython versions tested on:\n\n3.12\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-120214\n* gh-120237\n* gh-120239\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/Mortal","@type":"Person","name":"Mortal"},"datePublished":"2024-06-07T09:03:59.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/120200/cpython/issues/120200"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0c88f263-6ef3-153b-eaf9-67589cce1819 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D36C:8619D:23B41D:2F07AD:69697923 |
| html-safe-nonce | 6f9be18fd01cccaa122c1f672a6b149180279d0836e43f5f654f6ec3e3b5fea5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzZDOjg2MTlEOjIzQjQxRDoyRjA3QUQ6Njk2OTc5MjMiLCJ2aXNpdG9yX2lkIjoiODk3MzMwNzI5NTkxNDYyMTIxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 93a71b8a0365f290df8b36b4e0550c96350f22ee0c85532dd370ef3f3e82e046 |
| hovercard-subject-tag | issue:2339957988 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/120200/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7c6081359ebc201cbb26c9942894a0af03aa4a95ac0ffa7824b25272c46a4b52/python/cpython/issues/120200 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7c6081359ebc201cbb26c9942894a0af03aa4a95ac0ffa7824b25272c46a4b52/python/cpython/issues/120200 |
| og:image:alt | 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 retu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Mortal |
| hostname | github.com |
| expected-hostname | github.com |
| None | c6f193beb8ff08443adc07685d75302ab8aaf0a135f6e251c3ff3112c8deb881 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 212e3e3d3298bf5b313830edfd2399e869f7ea76 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width