Bug report
Bug description:
Before #105511, typing.Union is implemented in Python, and custom attributes can be assigned to a Union variable.
ta=Union[int, str]
ta.__some_attribute__= (int, str)
However, 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.
$ ipythonPython3.14.0a6+experimentalfree-threadingbuild (heads/main:1755157207c, Apr62025, 02:21:10) [Clang17.0.0 (clang-1700.0.13.3)]
Type'copyright', 'credits'or'license'formoreinformationIPython9.0.2--AnenhancedInteractivePython. Type'?'forhelp.
Tip: Use`object?`toseethehelpon`object`, `object??`toviewit'ssourceIn [1]: importoptreeIn [2]: optree.PyTree[int]
/Users/PanXuehai/Projects/optree/env/lib/python3.14t/site-packages/IPython/core/interactiveshell.py:3111: SyntaxWarning: 'return'ina'finally'blockreturnresult
╭────────────────────────────────────────────────────────────────────── Traceback (mostrecentcalllast) ───────────────────────────────────────────────────────────────────────╮
│ in<module>:1 │
│ │
│ /Users/PanXuehai/Projects/cpython/.pydev/lib/python3.14t/typing.py:398ininner │
│ │
│ 395 │ │ @functools.wraps(func) │
│ 396 │ │ definner(*args, **kwds): │
│ 397 │ │ │ try: │
│ ❱ 398 │ │ │ │ return_caches[func](*args, **kwds) │
│ 399 │ │ │ exceptTypeError: │
│ 400 │ │ │ │ pass# All real errors (not unhashable args) are raised below. │
│ 401 │ │ │ returnfunc(*args, **kwds) │
│ │
│ /Users/PanXuehai/Projects/optree/optree/typing.py:254in__class_getitem__ │
│ │
│ 251 │ │ │ Deque[recurse_ref], # type: ignore[valid-type] │
│ 252 │ │ │ CustomTreeNode[recurse_ref], # type: ignore[valid-type] │
│ 253 │ │ ] │
│ ❱ 254 │ │ pytree_alias.__pytree_args__=item# type: ignore[attr-defined] │
│ 255 │ │ │
│ 256 │ │ # pylint: disable-next=no-member │
│ 257 │ │ original_copy_with=pytree_alias.copy_with# type: ignore[attr-defined] │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
AttributeError: 'typing.Union'objecthasnoattribute'__pytree_args__'andno__dict__forsettingnewattributesIn [3]: classFoo(type(int|str)):
...: pass
...: ╭────────────────────────────────────────────────────────────────────── Traceback (mostrecentcalllast) ───────────────────────────────────────────────────────────────────────╮
│ in<module>:1 │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
TypeError: type'typing.Union'isnotanacceptablebasetype
Repro:
./python -m pip install -v optree
./python -c 'import optree; optree.PyTree[int]'
Source: https://github.com/metaopt/optree/blob/v0.15.0/optree/typing.py#L246-L254
cc @JelleZijlstra
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs
Bug report
Bug description:
Before #105511,
typing.Unionis implemented in Python, and custom attributes can be assigned to aUnionvariable.However, after #105511,
typing.Unionis now implemented in C, and no attributes can be assigned to it. TheUnionTypedoes not allow subclassing; users cannot bypass this using subclassing.Repro:
./python -m pip install -v optree ./python -c 'import optree; optree.PyTree[int]'Source: https://github.com/metaopt/optree/blob/v0.15.0/optree/typing.py#L246-L254
cc @JelleZijlstra
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs
Unionattributes #132157