fix UnionType.__or__ and add UnionType.__getitem__ (#14687)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
KotlinIsland
2025-09-24 22:01:33 +10:00
committed by GitHub
parent 2cc9c1667d
commit cef41544d0
2 changed files with 24 additions and 3 deletions
+13 -1
View File
@@ -3,9 +3,11 @@ from __future__ import annotations
import sys
import types
from collections import UserDict
from typing import Union
from typing import Any, Literal, TypeVar, Union
from typing_extensions import assert_type
_T = TypeVar("_T")
# test `types.SimpleNamespace`
# Valid:
@@ -58,3 +60,13 @@ class DCAtest:
@foo.deleter
def foo(self) -> None:
self._value = None
if sys.version_info > (3, 10):
union_type = int | list[_T]
# ideally this would be `_SpecialForm` (Union)
assert_type(union_type | Literal[1], types.UnionType | Any)
# Both mypy and pyright special-case this operation,
# but in different ways, so we just check that no error is emitted:
_ = union_type[int]