From 3bfe027e90c36306f7395c9ca0c93b7ec90f04c0 Mon Sep 17 00:00:00 2001 From: KotlinIsland <65446343+kotlinisland@users.noreply.github.com> Date: Wed, 1 Oct 2025 12:19:40 +1000 Subject: [PATCH 1/2] fix `type.__or__` --- stdlib/@tests/test_cases/builtins/check_type.py | 8 ++++++++ stdlib/builtins.pyi | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 stdlib/@tests/test_cases/builtins/check_type.py diff --git a/stdlib/@tests/test_cases/builtins/check_type.py b/stdlib/@tests/test_cases/builtins/check_type.py new file mode 100644 index 000000000000..acc83802cbf1 --- /dev/null +++ b/stdlib/@tests/test_cases/builtins/check_type.py @@ -0,0 +1,8 @@ +import sys +from typing_extensions import assert_type + +if sys.version_info >= (3, 10): + from types import UnionType + + # pyright has special-casing that causes this failure + assert_type(int | int, UnionType | type[int]) # pyright: ignore[reportAssertTypeFailure] diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 079e05fd0668..09e53625e056 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -226,8 +226,8 @@ class type: @classmethod def __prepare__(metacls, name: str, bases: tuple[type, ...], /, **kwds: Any) -> MutableMapping[str, object]: ... if sys.version_info >= (3, 10): - def __or__(self, value: Any, /) -> types.UnionType: ... - def __ror__(self, value: Any, /) -> types.UnionType: ... + def __or__(self, value: Any, /) -> types.UnionType | Self: ... # type: ignore[misc] + def __ror__(self, value: Any, /) -> types.UnionType | Self: ... # type: ignore[misc] if sys.version_info >= (3, 12): __type_params__: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] __annotations__: dict[str, AnnotationForm] From 55b5a1cdba054c50446cf6d650471ad9d5551d36 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 1 Oct 2025 10:51:55 +0100 Subject: [PATCH 2/2] add comment, remove ignore, remove test --- stdlib/@tests/test_cases/builtins/check_type.py | 8 -------- stdlib/builtins.pyi | 6 ++++-- 2 files changed, 4 insertions(+), 10 deletions(-) delete mode 100644 stdlib/@tests/test_cases/builtins/check_type.py diff --git a/stdlib/@tests/test_cases/builtins/check_type.py b/stdlib/@tests/test_cases/builtins/check_type.py deleted file mode 100644 index acc83802cbf1..000000000000 --- a/stdlib/@tests/test_cases/builtins/check_type.py +++ /dev/null @@ -1,8 +0,0 @@ -import sys -from typing_extensions import assert_type - -if sys.version_info >= (3, 10): - from types import UnionType - - # pyright has special-casing that causes this failure - assert_type(int | int, UnionType | type[int]) # pyright: ignore[reportAssertTypeFailure] diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 09e53625e056..2a75329f5b97 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -226,8 +226,10 @@ class type: @classmethod def __prepare__(metacls, name: str, bases: tuple[type, ...], /, **kwds: Any) -> MutableMapping[str, object]: ... if sys.version_info >= (3, 10): - def __or__(self, value: Any, /) -> types.UnionType | Self: ... # type: ignore[misc] - def __ror__(self, value: Any, /) -> types.UnionType | Self: ... # type: ignore[misc] + # `int | str` produces an instance of `UnionType`, but `int | int` produces an instance of `type`, + # and `abc.ABC | abc.ABC` produces an instance of `abc.ABCMeta`. + def __or__(self: _typeshed.Self, value: Any, /) -> types.UnionType | _typeshed.Self: ... + def __ror__(self: _typeshed.Self, value: Any, /) -> types.UnionType | _typeshed.Self: ... if sys.version_info >= (3, 12): __type_params__: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] __annotations__: dict[str, AnnotationForm]