Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
Closed
Labels
3.14bugs and security fixesbugs and security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
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'isnotanacceptablebasetypeRepro:
./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
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
Linked PRs
Metadata
Metadata
Assignees
Labels
3.14bugs and security fixesbugs and security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error