From b7d129f727bccb9976940a8db23f06a4c7dccc00 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 19 Mar 2022 08:23:00 -0700 Subject: [PATCH] PEP 604: Remove some more uses of Union/Optional (#7515) The following patterns still break mypy: 1. `type[]` at top level fails 2. `tuple[T1, T2]` at top level fails (but `tuple[T1, ...]` is fine) 3. `T1 | Callable[..., T2 | T3]` fails, but only <=3.9 This PR cleans up usage of `Union` and `Optional` outside these patterns. --- stdlib/asyncio/trsock.pyi | 4 ++-- stdlib/os/__init__.pyi | 21 ++++++++++----------- stdlib/traceback.pyi | 4 ++-- stdlib/wsgiref/handlers.pyi | 4 ++-- stdlib/xmlrpc/client.pyi | 6 ++++-- stubs/pyaudio/pyaudio.pyi | 4 ++-- stubs/requests/requests/sessions.pyi | 4 ++-- 7 files changed, 24 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..267a2b44e7db 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, ) @@ -832,16 +831,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: ... 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: ... 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/stdlib/xmlrpc/client.pyi b/stdlib/xmlrpc/client.pyi index 0cf8545fe2bb..a59be37f9e81 100644 --- a/stdlib/xmlrpc/client.pyi +++ b/stdlib/xmlrpc/client.pyi @@ -13,8 +13,10 @@ 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] +_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[tuple[str, dict[str, str]], str] def escape(s: str) -> str: ... # undocumented 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 07c2872c583d..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 = 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, Text | None]] | IO[Any] | None _Hook = Callable[[Response], Any] _Hooks = MutableMapping[Text, _Hook | list[_Hook]]