From 9e5c9feabed12c171c52d75e88dc072b6cffbc1e Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 19 Mar 2022 07:52:49 -0700 Subject: [PATCH 1/5] PEP 604: Remove some more uses of Union Mypy seems fine with these. Aliases that use type[] or variadic tuple[] still break mypy. --- stdlib/asyncio/trsock.pyi | 4 ++-- stdlib/os/__init__.pyi | 23 ++++++++++------------- stdlib/signal.pyi | 4 ++-- stdlib/ssl.pyi | 4 ++-- stdlib/xmlrpc/client.pyi | 8 +++++--- stubs/requests/requests/sessions.pyi | 2 +- 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/stdlib/asyncio/trsock.pyi b/stdlib/asyncio/trsock.pyi index 7973704d7635..3fe1e19d1311 100644 --- a/stdlib/asyncio/trsock.pyi +++ b/stdlib/asyncio/trsock.pyi @@ -2,10 +2,10 @@ import socket import sys from builtins import type as Type # alias to avoid name clashes with property named "type" from types import TracebackType -from typing import Any, BinaryIO, Iterable, NoReturn, Union, overload +from typing import Any, BinaryIO, Iterable, NoReturn, overload # These are based in socket, maybe move them out into _typeshed.pyi or such -_Address = Union[tuple[Any, ...], str] +_Address = tuple[Any, ...] | str _RetAddress = Any _WriteBuffer = bytearray | memoryview _CMSG = tuple[int, int, bytes] diff --git a/stdlib/os/__init__.pyi b/stdlib/os/__init__.pyi index c87ed7e4d4bc..497830c4460b 100644 --- a/stdlib/os/__init__.pyi +++ b/stdlib/os/__init__.pyi @@ -32,7 +32,6 @@ from typing import ( Protocol, Sequence, TypeVar, - Union, overload, runtime_checkable, ) @@ -388,7 +387,6 @@ _FdOrAnyPath = int | StrOrBytesPath class DirEntry(Generic[AnyStr]): # This is what the scandir iterator yields # The constructor is hidden - @property def name(self) -> AnyStr: ... @property @@ -832,16 +830,16 @@ def execlpe(file: StrOrBytesPath, __arg0: StrOrBytesPath, *args: Any) -> NoRetur # Not separating out PathLike[str] and PathLike[bytes] here because it doesn't make much difference # in practice, and doing so would explode the number of combinations in this already long union. # All these combinations are necessary due to list being invariant. -_ExecVArgs = Union[ - tuple[StrOrBytesPath, ...], - list[bytes], - list[str], - list[PathLike[Any]], - list[bytes | str], - list[bytes | PathLike[Any]], - list[str | PathLike[Any]], - list[bytes | str | PathLike[Any]], -] +_ExecVArgs = ( + tuple[StrOrBytesPath, ...] + | list[bytes] + | list[str] + | list[PathLike[Any]] + | list[bytes | str] + | list[bytes | PathLike[Any]] + | list[str | PathLike[Any]] + | list[bytes | str | PathLike[Any]] +) _ExecEnv = Mapping[bytes, bytes | str] | Mapping[str, bytes | str] def execv(__path: StrOrBytesPath, __argv: _ExecVArgs) -> NoReturn: ... @@ -1034,6 +1032,5 @@ if sys.version_info >= (3, 8): if sys.version_info >= (3, 9): def waitstatus_to_exitcode(status: int) -> int: ... - if sys.platform == "linux": def pidfd_open(pid: int, flags: int = ...) -> int: ... diff --git a/stdlib/signal.pyi b/stdlib/signal.pyi index 632539bf7df1..bac837bebfa6 100644 --- a/stdlib/signal.pyi +++ b/stdlib/signal.pyi @@ -2,7 +2,7 @@ import sys from _typeshed import structseq from enum import IntEnum from types import FrameType -from typing import Any, Callable, Iterable, Union +from typing import Any, Callable, Iterable from typing_extensions import final NSIG: int @@ -61,7 +61,7 @@ SIG_DFL: Handlers SIG_IGN: Handlers _SIGNUM = int | Signals -_HANDLER = Union[Callable[[int, FrameType | None], Any], int, Handlers, None] +_HANDLER = Callable[[int, FrameType | None], Any] | int | Handlers | None def default_int_handler(__signalnum: int, __frame: FrameType | None) -> None: ... diff --git a/stdlib/ssl.pyi b/stdlib/ssl.pyi index b7fe6914db0e..57768c1f5c2e 100644 --- a/stdlib/ssl.pyi +++ b/stdlib/ssl.pyi @@ -2,7 +2,7 @@ import enum import socket import sys from _typeshed import ReadableBuffer, Self, StrOrBytesPath, WriteableBuffer -from typing import Any, Callable, Iterable, NamedTuple, Union, overload +from typing import Any, Callable, Iterable, NamedTuple, overload from typing_extensions import Literal, TypedDict, final _PCTRTT = tuple[tuple[str, str], ...] @@ -10,7 +10,7 @@ _PCTRTTT = tuple[_PCTRTT, ...] _PeerCertRetDictType = dict[str, str | _PCTRTTT | _PCTRTT] _PeerCertRetType = _PeerCertRetDictType | bytes | None _EnumRetType = list[tuple[bytes, str, set[str] | bool]] -_PasswordType = Union[Callable[[], str | bytes], str, bytes] +_PasswordType = Callable[[], str | bytes] | str | bytes _SrvnmeCbType = Callable[[SSLSocket | SSLObject, str | None, SSLSocket], int | None] diff --git a/stdlib/xmlrpc/client.pyi b/stdlib/xmlrpc/client.pyi index 0cf8545fe2bb..f0844bb0b7c1 100644 --- a/stdlib/xmlrpc/client.pyi +++ b/stdlib/xmlrpc/client.pyi @@ -13,9 +13,11 @@ class _SupportsTimeTuple(Protocol): def timetuple(self) -> time.struct_time: ... _DateTimeComparable = DateTime | datetime | str | _SupportsTimeTuple -_Marshallable = Union[None, bool, int, float, str, bytes, tuple[Any, ...], list[Any], dict[Any, Any], datetime, DateTime, Binary] -_XMLDate = Union[int, datetime, tuple[int, ...], time.struct_time] -_HostType = Union[tuple[str, dict[str, str]], str] +_Marshallable = ( + bool | int | float | str | bytes | None | tuple[Any, ...] | list[Any] | dict[Any, Any] | datetime | DateTime | Binary +) +_XMLDate = int | datetime | tuple[int, ...] | time.struct_time +_HostType = Union[str, tuple[str, dict[str, str]]] def escape(s: str) -> str: ... # undocumented diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index 07c2872c583d..c026083b5492 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -47,7 +47,7 @@ class SessionRedirectMixin: def rebuild_proxies(self, prepared_request, proxies): ... def should_strip_auth(self, old_url, new_url): ... -_Data = Union[None, Text, bytes, Mapping[str, Any], Mapping[Text, Any], Iterable[tuple[Text, Optional[Text]]], IO[Any]] +_Data = Text | bytes | Mapping[str, Any] | Mapping[Text, Any] | Iterable[tuple[Text, Optional[Text]]] | IO[Any] | None _Hook = Callable[[Response], Any] _Hooks = MutableMapping[Text, _Hook | list[_Hook]] From d7212e0e18c772a82794ec831dc4fe55ed4a2dc7 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 19 Mar 2022 08:01:10 -0700 Subject: [PATCH 2/5] undo some more --- stdlib/os/__init__.pyi | 2 ++ stdlib/signal.pyi | 4 ++-- stdlib/ssl.pyi | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/stdlib/os/__init__.pyi b/stdlib/os/__init__.pyi index 497830c4460b..267a2b44e7db 100644 --- a/stdlib/os/__init__.pyi +++ b/stdlib/os/__init__.pyi @@ -387,6 +387,7 @@ _FdOrAnyPath = int | StrOrBytesPath class DirEntry(Generic[AnyStr]): # This is what the scandir iterator yields # The constructor is hidden + @property def name(self) -> AnyStr: ... @property @@ -1032,5 +1033,6 @@ if sys.version_info >= (3, 8): if sys.version_info >= (3, 9): def waitstatus_to_exitcode(status: int) -> int: ... + if sys.platform == "linux": def pidfd_open(pid: int, flags: int = ...) -> int: ... diff --git a/stdlib/signal.pyi b/stdlib/signal.pyi index bac837bebfa6..632539bf7df1 100644 --- a/stdlib/signal.pyi +++ b/stdlib/signal.pyi @@ -2,7 +2,7 @@ import sys from _typeshed import structseq from enum import IntEnum from types import FrameType -from typing import Any, Callable, Iterable +from typing import Any, Callable, Iterable, Union from typing_extensions import final NSIG: int @@ -61,7 +61,7 @@ SIG_DFL: Handlers SIG_IGN: Handlers _SIGNUM = int | Signals -_HANDLER = Callable[[int, FrameType | None], Any] | int | Handlers | None +_HANDLER = Union[Callable[[int, FrameType | None], Any], int, Handlers, None] def default_int_handler(__signalnum: int, __frame: FrameType | None) -> None: ... diff --git a/stdlib/ssl.pyi b/stdlib/ssl.pyi index 57768c1f5c2e..b7fe6914db0e 100644 --- a/stdlib/ssl.pyi +++ b/stdlib/ssl.pyi @@ -2,7 +2,7 @@ import enum import socket import sys from _typeshed import ReadableBuffer, Self, StrOrBytesPath, WriteableBuffer -from typing import Any, Callable, Iterable, NamedTuple, overload +from typing import Any, Callable, Iterable, NamedTuple, Union, overload from typing_extensions import Literal, TypedDict, final _PCTRTT = tuple[tuple[str, str], ...] @@ -10,7 +10,7 @@ _PCTRTTT = tuple[_PCTRTT, ...] _PeerCertRetDictType = dict[str, str | _PCTRTTT | _PCTRTT] _PeerCertRetType = _PeerCertRetDictType | bytes | None _EnumRetType = list[tuple[bytes, str, set[str] | bool]] -_PasswordType = Callable[[], str | bytes] | str | bytes +_PasswordType = Union[Callable[[], str | bytes], str, bytes] _SrvnmeCbType = Callable[[SSLSocket | SSLObject, str | None, SSLSocket], int | None] From 2ad73c851295c4b14baebe7bd55f4db213cc53a7 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 19 Mar 2022 08:05:59 -0700 Subject: [PATCH 3/5] Update stdlib/xmlrpc/client.pyi --- stdlib/xmlrpc/client.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/xmlrpc/client.pyi b/stdlib/xmlrpc/client.pyi index f0844bb0b7c1..a59be37f9e81 100644 --- a/stdlib/xmlrpc/client.pyi +++ b/stdlib/xmlrpc/client.pyi @@ -17,7 +17,7 @@ _Marshallable = ( bool | int | float | str | bytes | None | tuple[Any, ...] | list[Any] | dict[Any, Any] | datetime | DateTime | Binary ) _XMLDate = int | datetime | tuple[int, ...] | time.struct_time -_HostType = Union[str, tuple[str, dict[str, str]]] +_HostType = Union[tuple[str, dict[str, str]], str] def escape(s: str) -> str: ... # undocumented From e281d9c87f21fc47930643e49dd5ee04db64e083 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 19 Mar 2022 08:06:22 -0700 Subject: [PATCH 4/5] one more --- stdlib/traceback.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/traceback.pyi b/stdlib/traceback.pyi index 4df5c10077bf..859bfef64622 100644 --- a/stdlib/traceback.pyi +++ b/stdlib/traceback.pyi @@ -1,7 +1,7 @@ import sys from _typeshed import Self, SupportsWrite from types import FrameType, TracebackType -from typing import IO, Any, Generator, Iterable, Iterator, Mapping, Optional, overload +from typing import IO, Any, Generator, Iterable, Iterator, Mapping, overload from typing_extensions import Literal __all__ = [ @@ -26,7 +26,7 @@ __all__ = [ "walk_tb", ] -_PT = tuple[str, int, str, Optional[str]] +_PT = tuple[str, int, str, str | None] def print_tb(tb: TracebackType | None, limit: int | None = ..., file: IO[str] | None = ...) -> None: ... From 8f909d72dc1420980e48ac927df6d29a3e315e6a Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 19 Mar 2022 08:12:09 -0700 Subject: [PATCH 5/5] more Optional --- stdlib/wsgiref/handlers.pyi | 4 ++-- stubs/pyaudio/pyaudio.pyi | 4 ++-- stubs/requests/requests/sessions.pyi | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/stdlib/wsgiref/handlers.pyi b/stdlib/wsgiref/handlers.pyi index 731cb52ea9db..9e2153788cac 100644 --- a/stdlib/wsgiref/handlers.pyi +++ b/stdlib/wsgiref/handlers.pyi @@ -1,6 +1,6 @@ from abc import abstractmethod from types import TracebackType -from typing import IO, Callable, MutableMapping, Optional +from typing import IO, Callable, MutableMapping from .headers import Headers from .types import ErrorStream, InputStream, StartResponse, WSGIApplication, WSGIEnvironment @@ -8,7 +8,7 @@ from .util import FileWrapper __all__ = ["BaseHandler", "SimpleHandler", "BaseCGIHandler", "CGIHandler", "IISCGIHandler", "read_environ"] -_exc_info = tuple[Optional[type[BaseException]], Optional[BaseException], Optional[TracebackType]] +_exc_info = tuple[type[BaseException] | None, BaseException | None, TracebackType | None] def format_date_time(timestamp: float | None) -> str: ... # undocumented def read_environ() -> dict[str, str]: ... diff --git a/stubs/pyaudio/pyaudio.pyi b/stubs/pyaudio/pyaudio.pyi index a6994f9b5ae6..6e8d4f2bcccd 100644 --- a/stubs/pyaudio/pyaudio.pyi +++ b/stubs/pyaudio/pyaudio.pyi @@ -1,4 +1,4 @@ -from typing import Callable, Mapping, Optional, Sequence +from typing import Callable, Mapping, Sequence from typing_extensions import Final paFloat32: Final[int] @@ -70,7 +70,7 @@ paMacCoreStreamInfo: PaMacCoreStreamInfo _ChannelMap = Sequence[int] _PaHostApiInfo = Mapping[str, str | int] _PaDeviceInfo = Mapping[str, str | int | float] -_StreamCallback = Callable[[Optional[bytes], int, Mapping[str, float], int], tuple[Optional[bytes], int]] +_StreamCallback = Callable[[bytes | None, int, Mapping[str, float], int], tuple[bytes | None, int]] def get_format_from_width(width: int, unsigned: bool = ...) -> int: ... def get_portaudio_version() -> int: ... diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index c026083b5492..4140c07dbff7 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -1,5 +1,5 @@ from _typeshed import Self, SupportsItems -from typing import IO, Any, Callable, Iterable, Mapping, MutableMapping, Optional, Text, TypeVar, Union +from typing import IO, Any, Callable, Iterable, Mapping, MutableMapping, Text, TypeVar, Union from urllib3 import _collections @@ -47,7 +47,7 @@ class SessionRedirectMixin: def rebuild_proxies(self, prepared_request, proxies): ... def should_strip_auth(self, old_url, new_url): ... -_Data = Text | bytes | Mapping[str, Any] | Mapping[Text, Any] | Iterable[tuple[Text, Optional[Text]]] | IO[Any] | None +_Data = Text | bytes | Mapping[str, Any] | Mapping[Text, Any] | Iterable[tuple[Text, Text | None]] | IO[Any] | None _Hook = Callable[[Response], Any] _Hooks = MutableMapping[Text, _Hook | list[_Hook]]