Title: `typing.Union` does not support attribute assignment post gh-105511 · Issue #132139 · python/cpython · GitHub
Open Graph Title: `typing.Union` does not support attribute assignment post gh-105511 · Issue #132139 · python/cpython
X Title: `typing.Union` does not support attribute assignment post gh-105511 · Issue #132139 · python/cpython
Description: Bug report Bug description: Before #105511, typing.Union is implemented in Python, and custom attributes can be assigned to a Union variable. #105511 ta = Union[int, str] ta.__some_attribute__ = (int, str) However, after #105511, typing....
Open Graph Description: Bug report Bug description: Before #105511, typing.Union is implemented in Python, and custom attributes can be assigned to a Union variable. #105511 ta = Union[int, str] ta.__some_attribute__ = (i...
X Description: Bug report Bug description: Before #105511, typing.Union is implemented in Python, and custom attributes can be assigned to a Union variable. #105511 ta = Union[int, str] ta.__some_attribute__ = (i...
Opengraph URL: https://github.com/python/cpython/issues/132139
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`typing.Union` does not support attribute assignment post gh-105511","articleBody":"# Bug report\n\n### Bug description:\n\nBefore #105511, `typing.Union` is implemented in Python, and custom attributes can be assigned to a `Union` variable.\n\n- #105511\n\n```python\nta = Union[int, str]\nta.__some_attribute__ = (int, str)\n```\n\nHowever, after #105511, `typing.Union` is now implemented in C, and no attributes can be assigned to it. The `UnionType` does not allow subclassing; users cannot bypass this using subclassing.\n\n```python\n$ ipython\nPython 3.14.0a6+ experimental free-threading build (heads/main:1755157207c, Apr 6 2025, 02:21:10) [Clang 17.0.0 (clang-1700.0.13.3)]\nType 'copyright', 'credits' or 'license' for more information\nIPython 9.0.2 -- An enhanced Interactive Python. Type '?' for help.\nTip: Use `object?` to see the help on `object`, `object??` to view it's source\n\nIn [1]: import optree\n\nIn [2]: optree.PyTree[int]\n/Users/PanXuehai/Projects/optree/env/lib/python3.14t/site-packages/IPython/core/interactiveshell.py:3111: SyntaxWarning: 'return' in a 'finally' block\n return result\n╭────────────────────────────────────────────────────────────────────── Traceback (most recent call last) ───────────────────────────────────────────────────────────────────────╮\n│ in \u003cmodule\u003e:1 │\n│ │\n│ /Users/PanXuehai/Projects/cpython/.pydev/lib/python3.14t/typing.py:398 in inner │\n│ │\n│ 395 │ │ @functools.wraps(func) │\n│ 396 │ │ def inner(*args, **kwds): │\n│ 397 │ │ │ try: │\n│ ❱ 398 │ │ │ │ return _caches[func](*args, **kwds) │\n│ 399 │ │ │ except TypeError: │\n│ 400 │ │ │ │ pass # All real errors (not unhashable args) are raised below. │\n│ 401 │ │ │ return func(*args, **kwds) │\n│ │\n│ /Users/PanXuehai/Projects/optree/optree/typing.py:254 in __class_getitem__ │\n│ │\n│ 251 │ │ │ Deque[recurse_ref], # type: ignore[valid-type] │\n│ 252 │ │ │ CustomTreeNode[recurse_ref], # type: ignore[valid-type] │\n│ 253 │ │ ] │\n│ ❱ 254 │ │ pytree_alias.__pytree_args__ = item # type: ignore[attr-defined] │\n│ 255 │ │ │\n│ 256 │ │ # pylint: disable-next=no-member │\n│ 257 │ │ original_copy_with = pytree_alias.copy_with # type: ignore[attr-defined] │\n╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\nAttributeError: 'typing.Union' object has no attribute '__pytree_args__' and no __dict__ for setting new attributes\n\nIn [3]: class Foo(type(int | str)):\n ...: pass\n ...: \n╭────────────────────────────────────────────────────────────────────── Traceback (most recent call last) ───────────────────────────────────────────────────────────────────────╮\n│ in \u003cmodule\u003e:1 │\n╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\nTypeError: type 'typing.Union' is not an acceptable base type\n```\n\nRepro:\n\n```bash\n./python -m pip install -v optree\n./python -c 'import optree; optree.PyTree[int]'\n```\n\nSource: https://github.com/metaopt/optree/blob/v0.15.0/optree/typing.py#L246-L254\n\ncc @JelleZijlstra\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-132146\n* gh-132157\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/XuehaiPan","@type":"Person","name":"XuehaiPan"},"datePublished":"2025-04-05T18:44:48.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":18},"url":"https://github.com/132139/cpython/issues/132139"}
| 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:e0f34b98-ea1e-3587-8d4d-0a7ba9eb15e7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8ACC:C8A64:EC811C:1461C62:696A9462 |
| html-safe-nonce | 4b60f542dc544c3bd3268c5a58bf1d52ff70137775cca35660c347c90eb27469 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QUNDOkM4QTY0OkVDODExQzoxNDYxQzYyOjY5NkE5NDYyIiwidmlzaXRvcl9pZCI6Ijg4MzU1NDA4Mjk3MTE1MzcyNTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9ae5412886c9ee030d3a0d2145db1ce6e438405a90c44b67f69fc05ab8a2cd1c |
| hovercard-subject-tag | issue:2974429277 |
| 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/132139/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d2e0da7764967ff0b3cda8f11f65e91af5ce1310477e0e79c395771e26b91cee/python/cpython/issues/132139 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d2e0da7764967ff0b3cda8f11f65e91af5ce1310477e0e79c395771e26b91cee/python/cpython/issues/132139 |
| og:image:alt | Bug report Bug description: Before #105511, typing.Union is implemented in Python, and custom attributes can be assigned to a Union variable. #105511 ta = Union[int, str] ta.__some_attribute__ = (i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | XuehaiPan |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4dd496afc954da9c207b2d9fbe86e3074619f565754aa1d9274aec30d9e5b8d7 |
| 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 | 31496a13b80a2f6ad77d6c617ee2255a3545b539 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width