Title: surprising new behavior in inspect/annotationlib get_annotations as of 3.14.0b1, not in 3.14.0a7. intended change? · Issue #133684 · python/cpython · GitHub
Open Graph Title: surprising new behavior in inspect/annotationlib get_annotations as of 3.14.0b1, not in 3.14.0a7. intended change? · Issue #133684 · python/cpython
X Title: surprising new behavior in inspect/annotationlib get_annotations as of 3.14.0b1, not in 3.14.0a7. intended change? · Issue #133684 · python/cpython
Description: Bug report Bug description: 3.14.0b1 seems to be interpreting the annotations of a metaclass into a new format that is also interfering with the correct annotations of subclasses of that class. The subclass reports the annotations of the...
Open Graph Description: Bug report Bug description: 3.14.0b1 seems to be interpreting the annotations of a metaclass into a new format that is also interfering with the correct annotations of subclasses of that class. The...
X Description: Bug report Bug description: 3.14.0b1 seems to be interpreting the annotations of a metaclass into a new format that is also interfering with the correct annotations of subclasses of that class. The...
Opengraph URL: https://github.com/python/cpython/issues/133684
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"surprising new behavior in inspect/annotationlib get_annotations as of 3.14.0b1, not in 3.14.0a7. intended change?","articleBody":"# Bug report\n\n### Bug description:\n\n3.14.0b1 seems to be interpreting the annotations of a metaclass into a new format that is also interfering with the correct annotations of subclasses of that class. The subclass reports the annotations of the parent class, if it does not itself have any annotations. this only occurs if superclass is from a custom metaclass. if the subclass does have annotations, then the superclass annotations *are not* reported. this behavior is inconsistent with previous python versions including 3.14.0a7 and is also inconsistent with itself. see script below\n\n\n\n```python\nfrom __future__ import annotations\n\n\nimport inspect\n\n# a metaclass with an annotation\nclass MyMetaClass(type):\n metadata: dict[str, str]\n\n\n# dynamic class based on metaclass\nMyNewClass = MyMetaClass(\"MyNewClass\", (object,), {\"metadata\": {}})\n\n# this seems new, but maybe intentional. annotations are shown for the\n# dynamic class that come from the metaclass. OK\n# 3.13.3: {}\n# 3.14.0a7: {}\n# 3.14.0b1: {'metadata': 'dict[str, str]'}\nprint(inspect.get_annotations(MyNewClass))\n\n\n# declared class based on metaclass. Add another anno to it.\nclass MyOtherNewClass(metaclass=MyMetaClass):\n metadata: dict[str, str] = {}\n someint: int\n\n# both pythons look good and this agrees with older pythons too.\n# 3.13.3: {'metadata': 'dict[str, str]', 'someint': 'int'}\n# 3.14.0a7: {'metadata': 'dict[str, str]', 'someint': 'int'}\n# 3.14.0b1: {'metadata': 'dict[str, str]', 'someint': 'int'}\nprint(inspect.get_annotations(MyOtherNewClass))\n\n# a control, a normal class without special metaclass.\nclass YetAnotherClass:\n metadata: dict[str, str] = {}\n someint: int\n\n\n\n# here's where it goes wrong. If we make new classes from these\n# bases that have *no* annotations, the *superclass annotations leak into the subclass*\n\nclass MySubClass(MyNewClass):\n pass\n\n\n# 3.13.3: {}\n# 3.14.0a7: {}\n# 3.14.0b1: {'metadata': 'dict[str, str]', 'someint': 'int'} # this has to be wrong\nprint(inspect.get_annotations(MySubClass))\n\n# if we add *any* annotations to MySubClass, the above annos *disappear*\nclass MySubClass(MyNewClass):\n foobar: float\n\n# these look all correct. but 3.14.0b1's seems to be inconsistent with itself\n# 3.13.3: {'foobar': 'float'}\n# 3.14.0a7: {'foobar': 'float'}\n# 3.14.0b1: {'foobar': 'float'} # wait what? What happened to metadata/someint from above?\nprint(inspect.get_annotations(MySubClass))\n\n\n\nclass MyOtherSubClass(MyOtherNewClass):\n pass\n\n# similar behaviors for declared class\n# 3.13.3: {}\n# 3.14.0a7: {}\n# 3.14.0b1: {'metadata': 'dict[str, str]', 'someint': 'int'}\nprint(inspect.get_annotations(MyOtherSubClass))\n\n\n# behavior does not seem to occur at all without a metaclass.\n# YetAnotherClass has annotations but we do not see them from subclasses\n# of that class\nclass MyYetAnotherSubClass(YetAnotherClass):\n pass\n\n# 3.13.3: {}\n# 3.14.0a7: {}\n# 3.14.0b1: {}\nprint(inspect.get_annotations(MyYetAnotherSubClass))\n\n```\n\n\noutputs:\n\n```\n$ python test4.py # python 3.13.3\n{}\n{'metadata': 'dict[str, str]', 'someint': 'int'}\n{}\n{'foobar': 'float'}\n{}\n{}\n\n$ ~/.venv314a7/bin/python test4.py # python 3.14.0a7\n{}\n{'metadata': 'dict[str, str]', 'someint': 'int'}\n{}\n{'foobar': 'float'}\n{}\n{}\n\n$ ~/.venv314/bin/python test4.py # python 3.14.0b1\n{'metadata': 'dict[str, str]'}\n{'metadata': 'dict[str, str]', 'someint': 'int'}\n{'metadata': 'dict[str, str]'}\n{'foobar': 'float'}\n{'metadata': 'dict[str, str]', 'someint': 'int'}\n{}\n\n\n```\n\n### CPython versions tested on:\n\n3.14\n\n### Operating systems tested on:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-133795\n* gh-134656\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/zzzeek","@type":"Person","name":"zzzeek"},"datePublished":"2025-05-08T17:08:12Z","url":"https://github.com/python/cpython/issues/133684"}
| 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:9adccf75-5e31-6574-5d4f-0f3b0dd69ae4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CFDA:59847:8EBE2C:BEFAD4:6B0042B0 |
| html-safe-nonce | 08786d5e23cbafdfc01871f4172376e5cfdce37b132d04a78267bed64fe648a5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRkRBOjU5ODQ3OjhFQkUyQzpCRUZBRDQ6NkIwMDQyQjAiLCJ2aXNpdG9yX2lkIjoiNjIwNDcxNjY1NDc2MTM2MjA5NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ca4f3f8a63aaa98286dd9cf465a95c54c8ba2ffa5c8dba2138203dbeb0d87ca7 |
| hovercard-subject-tag | issue:3049611703 |
| 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/133684/issue_layout |
| twitter:image | https://opengraph.githubassets.com/48946d90ff248d23585a2ecc12b0fe09fe730c967689ceab3ccd647213cb4a40/python/cpython/issues/133684 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/48946d90ff248d23585a2ecc12b0fe09fe730c967689ceab3ccd647213cb4a40/python/cpython/issues/133684 |
| og:image:alt | Bug report Bug description: 3.14.0b1 seems to be interpreting the annotations of a metaclass into a new format that is also interfering with the correct annotations of subclasses of that class. The... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zzzeek |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width