Title: `inspect.getsource` (`findsource`) not working as expected when duplicate class is used · Issue #106727 · python/cpython · GitHub
Open Graph Title: `inspect.getsource` (`findsource`) not working as expected when duplicate class is used · Issue #106727 · python/cpython
X Title: `inspect.getsource` (`findsource`) not working as expected when duplicate class is used · Issue #106727 · python/cpython
Description: Bug report Consider the following example: import inspect class Model: a = 1 class Model: a = 2 print(Model.a) #> 2 print(inspect.getsource(Model)) #> class Model: #> a = 1 I think we should expect the last class to be used (it works as ...
Open Graph Description: Bug report Consider the following example: import inspect class Model: a = 1 class Model: a = 2 print(Model.a) #> 2 print(inspect.getsource(Model)) #> class Model: #> a = 1 I think we should expect...
X Description: Bug report Consider the following example: import inspect class Model: a = 1 class Model: a = 2 print(Model.a) #> 2 print(inspect.getsource(Model)) #> class Model: #> a = 1 I think we shou...
Opengraph URL: https://github.com/python/cpython/issues/106727
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`inspect.getsource` (`findsource`) not working as expected when duplicate class is used","articleBody":"\u003c!--\r\n If you're new to Python and you're not sure whether what you're experiencing is a bug, the CPython issue tracker is not\r\n the right place to seek help. Consider the following options instead:\r\n\r\n - reading the Python tutorial: https://docs.python.org/3/tutorial/\r\n - posting in the \"Users\" category on discuss.python.org: https://discuss.python.org/c/users/7\r\n - emailing the Python-list mailing list: https://mail.python.org/mailman/listinfo/python-list\r\n - searching our issue tracker (https://github.com/python/cpython/issues) to see if\r\n your problem has already been reported\r\n--\u003e\r\n\r\n# Bug report\r\n\r\nConsider the following example:\r\n\r\n```python\r\nimport inspect\r\n\r\nclass Model:\r\n a = 1\r\n\r\nclass Model:\r\n a = 2\r\n\r\nprint(Model.a)\r\n#\u003e 2\r\nprint(inspect.getsource(Model))\r\n#\u003e class Model:\r\n#\u003e a = 1\r\n```\r\n\r\nI think we should expect the last class to be used (it works as expected if we use functions instead of classes).\r\n\r\nThis issue was raised in an external project:\r\n- https://github.com/octoml/synr/issues/13\r\n\r\nAnd I'm facing this issue while working on:\r\n- https://github.com/pydantic/pydantic/pull/6563\r\n\r\nLet me know if this should be fixed, I'll take a deeper look at what was done in https://github.com/python/cpython/pull/10307 and see if I can make a PR\r\n\r\n# Your environment\r\n\r\n\u003c!-- Include as many relevant details as possible about the environment you experienced the bug in --\u003e\r\n\r\n- CPython versions tested on: 3.11.4\r\n- Operating system and architecture: Ubuntu\r\n\r\n\u003c!--\r\nYou can freely edit this text. Remove any lines you believe are unnecessary.\r\n--\u003e\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-106815\n* gh-106968\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/Viicos","@type":"Person","name":"Viicos"},"datePublished":"2023-07-13T17:50:55.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/106727/cpython/issues/106727"}
| 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:9092495b-442e-ded3-af39-701d50f913e7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE3C:3A06E8:5962F6:7EC9D1:6A50FDBF |
| html-safe-nonce | ced503861b230643338addaeea220abce5f61572596e115dfab067ab3be40c79 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTNDOjNBMDZFODo1OTYyRjY6N0VDOUQxOjZBNTBGREJGIiwidmlzaXRvcl9pZCI6IjgzMjQ5OTAxOTQ1NzYwNjM5MzUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 92c50ad40f439d4ef1197d8114556f4637b22455a661e44c612715d9e566a8ea |
| hovercard-subject-tag | issue:1803501087 |
| 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/106727/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4dc0b3d114831201ad6471f214b16d73e825af8164fbcdb141b59b1e19d341a8/python/cpython/issues/106727 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4dc0b3d114831201ad6471f214b16d73e825af8164fbcdb141b59b1e19d341a8/python/cpython/issues/106727 |
| og:image:alt | Bug report Consider the following example: import inspect class Model: a = 1 class Model: a = 2 print(Model.a) #> 2 print(inspect.getsource(Model)) #> class Model: #> a = 1 I think we should expect... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Viicos |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5266e58c17a510c403505cc811606465e90a881d2007ee7df1c4800d5c659838 |
| 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 | 8d836581d020676cc0ef4a83cd2fe17e4af02c3d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width