Skip to content

Commit b7d129f

Browse files
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.
1 parent 1acc8f3 commit b7d129f

File tree

7 files changed

+24
-23
lines changed

7 files changed

+24
-23
lines changed

stdlib/asyncio/trsock.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import socket
22
import sys
33
from builtins import type as Type # alias to avoid name clashes with property named "type"
44
from types import TracebackType
5-
from typing import Any, BinaryIO, Iterable, NoReturn, Union, overload
5+
from typing import Any, BinaryIO, Iterable, NoReturn, overload
66

77
# These are based in socket, maybe move them out into _typeshed.pyi or such
8-
_Address = Union[tuple[Any, ...], str]
8+
_Address = tuple[Any, ...] | str
99
_RetAddress = Any
1010
_WriteBuffer = bytearray | memoryview
1111
_CMSG = tuple[int, int, bytes]

stdlib/os/__init__.pyi

+10-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ from typing import (
3232
Protocol,
3333
Sequence,
3434
TypeVar,
35-
Union,
3635
overload,
3736
runtime_checkable,
3837
)
@@ -832,16 +831,16 @@ def execlpe(file: StrOrBytesPath, __arg0: StrOrBytesPath, *args: Any) -> NoRetur
832831
# Not separating out PathLike[str] and PathLike[bytes] here because it doesn't make much difference
833832
# in practice, and doing so would explode the number of combinations in this already long union.
834833
# All these combinations are necessary due to list being invariant.
835-
_ExecVArgs = Union[
836-
tuple[StrOrBytesPath, ...],
837-
list[bytes],
838-
list[str],
839-
list[PathLike[Any]],
840-
list[bytes | str],
841-
list[bytes | PathLike[Any]],
842-
list[str | PathLike[Any]],
843-
list[bytes | str | PathLike[Any]],
844-
]
834+
_ExecVArgs = (
835+
tuple[StrOrBytesPath, ...]
836+
| list[bytes]
837+
| list[str]
838+
| list[PathLike[Any]]
839+
| list[bytes | str]
840+
| list[bytes | PathLike[Any]]
841+
| list[str | PathLike[Any]]
842+
| list[bytes | str | PathLike[Any]]
843+
)
845844
_ExecEnv = Mapping[bytes, bytes | str] | Mapping[str, bytes | str]
846845

847846
def execv(__path: StrOrBytesPath, __argv: _ExecVArgs) -> NoReturn: ...

stdlib/traceback.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
from _typeshed import Self, SupportsWrite
33
from types import FrameType, TracebackType
4-
from typing import IO, Any, Generator, Iterable, Iterator, Mapping, Optional, overload
4+
from typing import IO, Any, Generator, Iterable, Iterator, Mapping, overload
55
from typing_extensions import Literal
66

77
__all__ = [
@@ -26,7 +26,7 @@ __all__ = [
2626
"walk_tb",
2727
]
2828

29-
_PT = tuple[str, int, str, Optional[str]]
29+
_PT = tuple[str, int, str, str | None]
3030

3131
def print_tb(tb: TracebackType | None, limit: int | None = ..., file: IO[str] | None = ...) -> None: ...
3232

stdlib/wsgiref/handlers.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from abc import abstractmethod
22
from types import TracebackType
3-
from typing import IO, Callable, MutableMapping, Optional
3+
from typing import IO, Callable, MutableMapping
44

55
from .headers import Headers
66
from .types import ErrorStream, InputStream, StartResponse, WSGIApplication, WSGIEnvironment
77
from .util import FileWrapper
88

99
__all__ = ["BaseHandler", "SimpleHandler", "BaseCGIHandler", "CGIHandler", "IISCGIHandler", "read_environ"]
1010

11-
_exc_info = tuple[Optional[type[BaseException]], Optional[BaseException], Optional[TracebackType]]
11+
_exc_info = tuple[type[BaseException] | None, BaseException | None, TracebackType | None]
1212

1313
def format_date_time(timestamp: float | None) -> str: ... # undocumented
1414
def read_environ() -> dict[str, str]: ...

stdlib/xmlrpc/client.pyi

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ class _SupportsTimeTuple(Protocol):
1313
def timetuple(self) -> time.struct_time: ...
1414

1515
_DateTimeComparable = DateTime | datetime | str | _SupportsTimeTuple
16-
_Marshallable = Union[None, bool, int, float, str, bytes, tuple[Any, ...], list[Any], dict[Any, Any], datetime, DateTime, Binary]
17-
_XMLDate = Union[int, datetime, tuple[int, ...], time.struct_time]
16+
_Marshallable = (
17+
bool | int | float | str | bytes | None | tuple[Any, ...] | list[Any] | dict[Any, Any] | datetime | DateTime | Binary
18+
)
19+
_XMLDate = int | datetime | tuple[int, ...] | time.struct_time
1820
_HostType = Union[tuple[str, dict[str, str]], str]
1921

2022
def escape(s: str) -> str: ... # undocumented

stubs/pyaudio/pyaudio.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Callable, Mapping, Optional, Sequence
1+
from typing import Callable, Mapping, Sequence
22
from typing_extensions import Final
33

44
paFloat32: Final[int]
@@ -70,7 +70,7 @@ paMacCoreStreamInfo: PaMacCoreStreamInfo
7070
_ChannelMap = Sequence[int]
7171
_PaHostApiInfo = Mapping[str, str | int]
7272
_PaDeviceInfo = Mapping[str, str | int | float]
73-
_StreamCallback = Callable[[Optional[bytes], int, Mapping[str, float], int], tuple[Optional[bytes], int]]
73+
_StreamCallback = Callable[[bytes | None, int, Mapping[str, float], int], tuple[bytes | None, int]]
7474

7575
def get_format_from_width(width: int, unsigned: bool = ...) -> int: ...
7676
def get_portaudio_version() -> int: ...

stubs/requests/requests/sessions.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from _typeshed import Self, SupportsItems
2-
from typing import IO, Any, Callable, Iterable, Mapping, MutableMapping, Optional, Text, TypeVar, Union
2+
from typing import IO, Any, Callable, Iterable, Mapping, MutableMapping, Text, TypeVar, Union
33

44
from urllib3 import _collections
55

@@ -47,7 +47,7 @@ class SessionRedirectMixin:
4747
def rebuild_proxies(self, prepared_request, proxies): ...
4848
def should_strip_auth(self, old_url, new_url): ...
4949

50-
_Data = Union[None, Text, bytes, Mapping[str, Any], Mapping[Text, Any], Iterable[tuple[Text, Optional[Text]]], IO[Any]]
50+
_Data = Text | bytes | Mapping[str, Any] | Mapping[Text, Any] | Iterable[tuple[Text, Text | None]] | IO[Any] | None
5151

5252
_Hook = Callable[[Response], Any]
5353
_Hooks = MutableMapping[Text, _Hook | list[_Hook]]

0 commit comments

Comments
 (0)