Skip to content

Commit 8c60e06

Browse files
committed
add tests, update comments, remove type
1 parent aca7bec commit 8c60e06

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

stdlib/@tests/test_cases/check_types.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import sys
44
import types
55
from collections import UserDict
6-
from typing import Union
6+
from typing import Any, Literal, TypeVar, Union
77
from typing_extensions import assert_type
88

9+
_T = TypeVar("_T")
10+
911
# test `types.SimpleNamespace`
1012

1113
# Valid:
@@ -58,3 +60,12 @@ def foo(self, value: int) -> None:
5860
@foo.deleter
5961
def foo(self) -> None:
6062
self._value = None
63+
64+
65+
if sys.version_info > (3, 10):
66+
union_type = int | list[_T]
67+
68+
# ideally this would be `_SpecialForm` (Union)
69+
assert_type(union_type | Literal[1], types.UnionType | Any)
70+
# pyright special cases this operation
71+
assert_type(union_type[int], object) # pyright: ignore[reportAssertTypeFailure]

stdlib/types.pyi

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -717,12 +717,18 @@ if sys.version_info >= (3, 10):
717717
def __args__(self) -> tuple[Any, ...]: ...
718718
@property
719719
def __parameters__(self) -> tuple[Any, ...]: ...
720-
# the `Any` in the return typeis because of underspecified binop semantics, and `value: Any` is clobbering
721-
# `_SpecialForm.__ror__` which would result in a `_SpecialForm` (Union)
722-
def __or__(self, value: Any, /) -> UnionType | type | Any: ...
723-
def __ror__(self, value: Any, /) -> UnionType | type | Any: ...
720+
# `(int | str) | Literal["foo"]` returns a generic alias to an instance of `_SpecialForm` (`Union`).
721+
# Normally we'd express this using the return type of `_SpecialForm.__ror__`,
722+
# but because `UnionType.__or__` accepts `Any`, type checkers will use
723+
# the return type of `UnionType.__or__` to infer the result of this operation
724+
# rather than `_SpecialForm.__ror__`. To mitigate this, we use `| Any`
725+
# in the return type of `UnionType.__(r)or__`.
726+
def __or__(self, value: Any, /) -> UnionType | Any: ...
727+
def __ror__(self, value: Any, /) -> UnionType | Any: ...
724728
def __eq__(self, value: object, /) -> bool: ...
725729
def __hash__(self) -> int: ...
730+
# you can only subscript a `UnionType` instance if at least one of the elements
731+
# in the union is a generic alias instance that has a non-empty `__parameters__`
726732
def __getitem__(self, parameters: Any) -> object: ...
727733

728734
if sys.version_info >= (3, 13):

0 commit comments

Comments
 (0)