Title: Setting `__module__` doesn't remove `__firstlineno__` when the class has a custom metaclass · Issue #162572 · python/cpython · GitHub
Open Graph Title: Setting `__module__` doesn't remove `__firstlineno__` when the class has a custom metaclass · Issue #162572 · python/cpython
X Title: Setting `__module__` doesn't remove `__firstlineno__` when the class has a custom metaclass · Issue #162572 · python/cpython
Description: Bug report Bug description: The docs say "Setting the __module__ attribute removes the __firstlineno__ item from the type's dictionary" (type.__firstlineno__). For a class whose metaclass is written in Python (abc.ABC, enum.Enum, class M...
Open Graph Description: Bug report Bug description: The docs say "Setting the __module__ attribute removes the __firstlineno__ item from the type's dictionary" (type.__firstlineno__). For a class whose metaclass is writte...
X Description: Bug report Bug description: The docs say "Setting the __module__ attribute removes the __firstlineno__ item from the type's dictionary" (type.__firstlineno__). For a class whose metac...
Opengraph URL: https://github.com/python/cpython/issues/162572
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Setting `__module__` doesn't remove `__firstlineno__` when the class has a custom metaclass","articleBody":"# Bug report\n\n### Bug description:\n\nThe docs say \"Setting the `__module__` attribute removes the `__firstlineno__` item from the type's dictionary\" ([`type.__firstlineno__`](https://docs.python.org/dev/reference/datamodel.html#type.__firstlineno__)). For a class whose metaclass is written in Python (`abc.ABC`, `enum.Enum`, `class M(type): pass`) it stays, so `inspect.getsource` returns lines of the module named by the new `__module__`, the problem of gh-123339:\n\n```python\nimport abc, inspect\nclass P: pass\nclass A(abc.ABC): pass\nfor c in (P, A):\n c.__module__ = \"inspect\" # any other imported module\n print(c.__name__, \"__firstlineno__\" in c.__dict__, end=\" \")\n try: print(repr(inspect.getsource(c)[:40]))\n except (OSError, TypeError) as e: print(type(e).__name__, e)\n```\n\n```\nP False OSError source code not available\nA True 'This module encapsulates the interface p'\n```\n\nThe metaclass's own `__module__` string in its `__dict__` comes before the `__module__` descriptor of `type` in the lookup, so the assignment just stores into the class's `__dict__` and `type_set_module` (`Objects/typeobject.c`), which removes `__firstlineno__`, never runs. I found it while writing a Lean spec of Python.\n\n### CPython versions tested on:\n\n3.15, CPython main branch\n\n### Operating systems tested on:\n\nLinux\n","author":{"url":"https://github.com/Robertboy18","@type":"Person","name":"Robertboy18"},"datePublished":"2026-11-24T17:33:06Z","url":"https://github.com/python/cpython/issues/162572"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:8cba069f-fa1a-1d87-478a-7b2e5a773cf2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 88F4:2E4E6F:AE4902:E87FBD:6B06420C |
| html-safe-nonce | a02c3463576874d9b5d6268ca49df2c29a3c25cc56c9d90f9ff3d83e91b960a6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OEY0OjJFNEU2RjpBRTQ5MDI6RTg3RkJEOjZCMDY0MjBDIiwidmlzaXRvcl9pZCI6Ijc3MzYxMjkxMTE0OTE3NTY1NTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4e90f2ff67d0d14c2c1d6c773d197d40926c1ec39820fd7cb51759db7344b4be |
| hovercard-subject-tag | issue:6587696808 |
| 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/162572/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cc1513059c758c9b1c55724c6c6920cb9fe7c39881f3e0c2a2a0a874b862a35d/python/cpython/issues/162572 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cc1513059c758c9b1c55724c6c6920cb9fe7c39881f3e0c2a2a0a874b862a35d/python/cpython/issues/162572 |
| og:image:alt | Bug report Bug description: The docs say "Setting the __module__ attribute removes the __firstlineno__ item from the type's dictionary" (type.__firstlineno__). For a class whose metaclass is writte... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Robertboy18 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1bff4f6bfaa96dc9c42cacf315424c24ebb548b06142ced5e3aaccb2a42dd652 |
| 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 | ac613d3698d3d8565651455086e1f74da07bf277 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width