Skip to content

Commit 0ef9c3f

Browse files
authored
Enable flake8-pyi's Y037 (#9686)
1 parent c4c4bee commit 0ef9c3f

File tree

38 files changed

+119
-201
lines changed

38 files changed

+119
-201
lines changed

.flake8

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,15 @@
1818
# F403 import *' used; unable to detect undefined names
1919
# F405 defined from star imports
2020

21-
# Rules that we'd like to enable in the future:
22-
# Y037 Use PEP 604 syntax instead of `typing.Union` and `typing.Optional`.
23-
# Currently can't be enabled due to a few lingering bugs in mypy regarding
24-
# PEP 604 type aliases (see #4819).
25-
2621
[flake8]
2722
per-file-ignores =
2823
*.py: E203, E301, E302, E305, E501
29-
*.pyi: B, E301, E302, E305, E501, E701, E741, F401, F403, F405, F822, Y037
24+
*.pyi: B, E301, E302, E305, E501, E701, E741, F401, F403, F405, F822
3025
# Since typing.pyi defines "overload" this is not recognized by flake8 as typing.overload.
3126
# Unfortunately, flake8 does not allow to "noqa" just a specific error inside the file itself.
3227
# https://github.com/PyCQA/flake8/issues/1079
3328
# F811 redefinition of unused '...'
34-
stdlib/typing.pyi: B, E301, E302, E305, E501, E701, E741, F401, F403, F405, F811, F822, Y037
29+
stdlib/typing.pyi: B, E301, E302, E305, E501, E701, E741, F401, F403, F405, F811, F822
3530
# Generated protobuf files include docstrings
3631
*_pb2.pyi: B, E301, E302, E305, E501, E701, Y021, Y026
3732

stdlib/_csv.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from _typeshed import SupportsWrite
22
from collections.abc import Iterable, Iterator
3-
from typing import Any, Union
3+
from typing import Any
44
from typing_extensions import Literal, TypeAlias
55

66
__version__: str
@@ -27,7 +27,7 @@ class Dialect:
2727
strict: bool
2828
def __init__(self) -> None: ...
2929

30-
_DialectLike: TypeAlias = Union[str, Dialect, type[Dialect]]
30+
_DialectLike: TypeAlias = str | Dialect | type[Dialect]
3131

3232
class _reader(Iterator[list[str]]):
3333
@property

stdlib/_decimal.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import sys
33
from _typeshed import Self
44
from collections.abc import Container, Sequence
55
from types import TracebackType
6-
from typing import Any, ClassVar, NamedTuple, Union, overload
6+
from typing import Any, ClassVar, NamedTuple, overload
77
from typing_extensions import Literal, TypeAlias
88

99
_Decimal: TypeAlias = Decimal | int
10-
_DecimalNew: TypeAlias = Union[Decimal, float, str, tuple[int, Sequence[int], int]]
10+
_DecimalNew: TypeAlias = Decimal | float | str | tuple[int, Sequence[int], int]
1111
_ComparableNum: TypeAlias = Decimal | float | numbers.Rational
1212

1313
__version__: str

stdlib/_typeshed/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ from collections.abc import Awaitable, Callable, Iterable, Set as AbstractSet
1111
from dataclasses import Field
1212
from os import PathLike
1313
from types import FrameType, TracebackType
14-
from typing import Any, AnyStr, ClassVar, Generic, Protocol, TypeVar, Union
14+
from typing import Any, AnyStr, ClassVar, Generic, Protocol, TypeVar
1515
from typing_extensions import Final, Literal, LiteralString, TypeAlias, final
1616

1717
_KT = TypeVar("_KT")
@@ -265,7 +265,7 @@ IndexableBuffer: TypeAlias = bytes | bytearray | memoryview | array.array[Any] |
265265
# def __buffer__(self, __flags: int) -> memoryview: ...
266266

267267
ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, TracebackType]
268-
OptExcInfo: TypeAlias = Union[ExcInfo, tuple[None, None, None]]
268+
OptExcInfo: TypeAlias = ExcInfo | tuple[None, None, None]
269269

270270
# stable
271271
if sys.version_info >= (3, 10):

stdlib/copyreg.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from collections.abc import Callable, Hashable
2-
from typing import Any, SupportsInt, TypeVar, Union
2+
from typing import Any, SupportsInt, TypeVar
33
from typing_extensions import TypeAlias
44

55
_T = TypeVar("_T")
6-
_Reduce: TypeAlias = Union[tuple[Callable[..., _T], tuple[Any, ...]], tuple[Callable[..., _T], tuple[Any, ...], Any | None]]
6+
_Reduce: TypeAlias = tuple[Callable[..., _T], tuple[Any, ...]] | tuple[Callable[..., _T], tuple[Any, ...], Any | None]
77

88
__all__ = ["pickle", "constructor", "add_extension", "remove_extension", "clear_extension_cache"]
99

stdlib/ctypes/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from _ctypes import RTLD_GLOBAL as RTLD_GLOBAL, RTLD_LOCAL as RTLD_LOCAL
33
from _typeshed import ReadableBuffer, Self, WriteableBuffer
44
from abc import abstractmethod
55
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
6-
from typing import Any, ClassVar, Generic, TypeVar, Union as _UnionT, overload
6+
from typing import Any, ClassVar, Generic, TypeVar, overload
77
from typing_extensions import TypeAlias
88

99
if sys.version_info >= (3, 9):
@@ -91,7 +91,7 @@ class _CanCastTo(_CData): ...
9191
class _PointerLike(_CanCastTo): ...
9292

9393
_ECT: TypeAlias = Callable[[type[_CData] | None, _FuncPointer, tuple[_CData, ...]], _CData]
94-
_PF: TypeAlias = _UnionT[tuple[int], tuple[int, str], tuple[int, str, Any]]
94+
_PF: TypeAlias = tuple[int] | tuple[int, str] | tuple[int, str, Any]
9595

9696
class _FuncPointer(_PointerLike, _CData):
9797
restype: type[_CData] | Callable[[int], Any] | None

stdlib/distutils/ccompiler.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from collections.abc import Callable
2-
from typing import Any, Union
2+
from typing import Any
33
from typing_extensions import TypeAlias
44

5-
_Macro: TypeAlias = Union[tuple[str], tuple[str, str | None]]
5+
_Macro: TypeAlias = tuple[str] | tuple[str, str | None]
66

77
def gen_lib_options(
88
compiler: CCompiler, library_dirs: list[str], runtime_library_dirs: list[str], libraries: list[str]

stdlib/email/__init__.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from collections.abc import Callable
22
from email.message import Message
33
from email.policy import Policy
4-
from typing import IO, Union
4+
from typing import IO
55
from typing_extensions import TypeAlias
66

77
# Definitions imported by multiple submodules in typeshed
8-
_ParamType: TypeAlias = Union[str, tuple[str | None, str | None, str]] # noqa: Y047
9-
_ParamsType: TypeAlias = Union[str, None, tuple[str, str | None, str]] # noqa: Y047
8+
_ParamType: TypeAlias = str | tuple[str | None, str | None, str] # noqa: Y047
9+
_ParamsType: TypeAlias = str | None | tuple[str, str | None, str] # noqa: Y047
1010

1111
def message_from_string(s: str, _class: Callable[[], Message] = ..., *, policy: Policy = ...) -> Message: ...
1212
def message_from_bytes(s: bytes | bytearray, _class: Callable[[], Message] = ..., *, policy: Policy = ...) -> Message: ...

stdlib/inspect.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ from types import (
2525
TracebackType,
2626
WrapperDescriptorType,
2727
)
28-
from typing import Any, ClassVar, NamedTuple, Protocol, TypeVar, Union, overload
28+
from typing import Any, ClassVar, NamedTuple, Protocol, TypeVar, overload
2929
from typing_extensions import Literal, ParamSpec, TypeAlias, TypeGuard
3030

3131
if sys.version_info >= (3, 11):
@@ -264,9 +264,9 @@ def isdatadescriptor(object: object) -> TypeGuard[_SupportsSet[Any, Any] | _Supp
264264
#
265265
# Retrieving source code
266266
#
267-
_SourceObjectType: TypeAlias = Union[
268-
ModuleType, type[Any], MethodType, FunctionType, TracebackType, FrameType, CodeType, Callable[..., Any]
269-
]
267+
_SourceObjectType: TypeAlias = (
268+
ModuleType | type[Any] | MethodType | FunctionType | TracebackType | FrameType | CodeType | Callable[..., Any]
269+
)
270270

271271
def findsource(object: _SourceObjectType) -> tuple[list[str], int]: ...
272272
def getabsfile(object: _SourceObjectType, _filename: str | None = None) -> str: ...

stdlib/logging/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ from re import Pattern
77
from string import Template
88
from time import struct_time
99
from types import FrameType, TracebackType
10-
from typing import Any, ClassVar, Generic, TextIO, TypeVar, Union, overload
10+
from typing import Any, ClassVar, Generic, TextIO, TypeVar, overload
1111
from typing_extensions import Literal, TypeAlias
1212

1313
if sys.version_info >= (3, 11):
@@ -61,7 +61,7 @@ __all__ = [
6161
if sys.version_info >= (3, 11):
6262
__all__ += ["getLevelNamesMapping"]
6363

64-
_SysExcInfoType: TypeAlias = Union[tuple[type[BaseException], BaseException, TracebackType | None], tuple[None, None, None]]
64+
_SysExcInfoType: TypeAlias = tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None]
6565
_ExcInfoType: TypeAlias = None | bool | _SysExcInfoType | BaseException
6666
_ArgsType: TypeAlias = tuple[object, ...] | Mapping[str, object]
6767
_FilterType: TypeAlias = Filter | Callable[[LogRecord], bool]

stdlib/marshal.pyi

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
import builtins
22
import types
33
from _typeshed import ReadableBuffer, SupportsRead, SupportsWrite
4-
from typing import Any, Union
4+
from typing import Any
55
from typing_extensions import TypeAlias
66

77
version: int
88

9-
_Marshallable: TypeAlias = Union[
9+
_Marshallable: TypeAlias = (
1010
# handled in w_object() in marshal.c
11-
None,
12-
type[StopIteration],
13-
builtins.ellipsis,
14-
bool,
11+
None
12+
| type[StopIteration]
13+
| builtins.ellipsis
14+
| bool
1515
# handled in w_complex_object() in marshal.c
16-
int,
17-
float,
18-
complex,
19-
bytes,
20-
str,
21-
tuple[_Marshallable, ...],
22-
list[Any],
23-
dict[Any, Any],
24-
set[Any],
25-
frozenset[_Marshallable],
26-
types.CodeType,
27-
ReadableBuffer,
28-
]
16+
| int
17+
| float
18+
| complex
19+
| bytes
20+
| str
21+
| tuple[_Marshallable, ...]
22+
| list[Any]
23+
| dict[Any, Any]
24+
| set[Any]
25+
| frozenset[_Marshallable]
26+
| types.CodeType
27+
| ReadableBuffer
28+
)
2929

3030
def dump(__value: _Marshallable, __file: SupportsWrite[bytes], __version: int = 4) -> None: ...
3131
def load(__file: SupportsRead[bytes]) -> Any: ...

stdlib/multiprocessing/connection.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import sys
33
import types
44
from _typeshed import ReadableBuffer, Self
55
from collections.abc import Iterable
6-
from typing import Any, Union
6+
from typing import Any
77
from typing_extensions import SupportsIndex, TypeAlias
88

99
__all__ = ["Client", "Listener", "Pipe", "wait"]
1010

1111
# https://docs.python.org/3/library/multiprocessing.html#address-formats
12-
_Address: TypeAlias = Union[str, tuple[str, int]]
12+
_Address: TypeAlias = str | tuple[str, int]
1313

1414
class _ConnectionBase:
1515
def __init__(self, handle: SupportsIndex, readable: bool = True, writable: bool = True) -> None: ...

stdlib/multiprocessing/dummy/connection.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from _typeshed import Self
22
from queue import Queue
33
from types import TracebackType
4-
from typing import Any, Union
4+
from typing import Any
55
from typing_extensions import TypeAlias
66

77
__all__ = ["Client", "Listener", "Pipe"]
88

99
families: list[None]
1010

11-
_Address: TypeAlias = Union[str, tuple[str, int]]
11+
_Address: TypeAlias = str | tuple[str, int]
1212

1313
class Connection:
1414
_in: Any

stdlib/pickle.pyi

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
from _typeshed import ReadableBuffer, SupportsWrite
33
from collections.abc import Callable, Iterable, Iterator, Mapping
4-
from typing import Any, ClassVar, Protocol, SupportsBytes, Union
4+
from typing import Any, ClassVar, Protocol, SupportsBytes
55
from typing_extensions import SupportsIndex, TypeAlias, final
66

77
__all__ = [
@@ -142,13 +142,13 @@ class PickleError(Exception): ...
142142
class PicklingError(PickleError): ...
143143
class UnpicklingError(PickleError): ...
144144

145-
_ReducedType: TypeAlias = Union[
146-
str,
147-
tuple[Callable[..., Any], tuple[Any, ...]],
148-
tuple[Callable[..., Any], tuple[Any, ...], Any],
149-
tuple[Callable[..., Any], tuple[Any, ...], Any, Iterator[Any] | None],
150-
tuple[Callable[..., Any], tuple[Any, ...], Any, Iterator[Any] | None, Iterator[Any] | None],
151-
]
145+
_ReducedType: TypeAlias = (
146+
str
147+
| tuple[Callable[..., Any], tuple[Any, ...]]
148+
| tuple[Callable[..., Any], tuple[Any, ...], Any]
149+
| tuple[Callable[..., Any], tuple[Any, ...], Any, Iterator[Any] | None]
150+
| tuple[Callable[..., Any], tuple[Any, ...], Any, Iterator[Any] | None, Iterator[Any] | None]
151+
)
152152

153153
class Pickler:
154154
fast: bool

stdlib/signal.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from _typeshed import structseq
33
from collections.abc import Callable, Iterable
44
from enum import IntEnum
55
from types import FrameType
6-
from typing import Any, Union
6+
from typing import Any
77
from typing_extensions import Final, Never, TypeAlias, final
88

99
NSIG: int
@@ -62,7 +62,7 @@ SIG_DFL: Handlers
6262
SIG_IGN: Handlers
6363

6464
_SIGNUM: TypeAlias = int | Signals
65-
_HANDLER: TypeAlias = Union[Callable[[int, FrameType | None], Any], int, Handlers, None]
65+
_HANDLER: TypeAlias = Callable[[int, FrameType | None], Any] | int | Handlers | None
6666

6767
def default_int_handler(__signalnum: int, __frame: FrameType | None) -> Never: ...
6868

stdlib/socketserver.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ from _socket import _Address, _RetAddress
44
from _typeshed import ReadableBuffer, Self
55
from collections.abc import Callable
66
from socket import socket as _socket
7-
from typing import Any, BinaryIO, ClassVar, Union
7+
from typing import Any, BinaryIO, ClassVar
88
from typing_extensions import TypeAlias
99

1010
__all__ = [
@@ -29,7 +29,7 @@ if sys.platform != "win32":
2929
"UnixStreamServer",
3030
]
3131

32-
_RequestType: TypeAlias = Union[_socket, tuple[bytes, _socket]]
32+
_RequestType: TypeAlias = _socket | tuple[bytes, _socket]
3333
_AfUnixAddress: TypeAlias = str | ReadableBuffer # adddress acceptable for an AF_UNIX socket
3434
_AfInetAddress: TypeAlias = tuple[str | bytes | bytearray, int] # address acceptable for an AF_INET socket
3535

stdlib/ssl.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import socket
33
import sys
44
from _typeshed import ReadableBuffer, Self, StrOrBytesPath, WriteableBuffer
55
from collections.abc import Callable, Iterable
6-
from typing import Any, NamedTuple, Union, overload
6+
from typing import Any, NamedTuple, overload
77
from typing_extensions import Literal, TypeAlias, TypedDict, final
88

99
_PCTRTT: TypeAlias = tuple[tuple[str, str], ...]
1010
_PCTRTTT: TypeAlias = tuple[_PCTRTT, ...]
1111
_PeerCertRetDictType: TypeAlias = dict[str, str | _PCTRTTT | _PCTRTT]
1212
_PeerCertRetType: TypeAlias = _PeerCertRetDictType | bytes | None
1313
_EnumRetType: TypeAlias = list[tuple[bytes, str, set[str] | bool]]
14-
_PasswordType: TypeAlias = Union[Callable[[], str | bytes | bytearray], str, bytes, bytearray]
14+
_PasswordType: TypeAlias = Callable[[], str | bytes | bytearray] | str | bytes | bytearray
1515

1616
_SrvnmeCbType: TypeAlias = Callable[[SSLSocket | SSLObject, str | None, SSLSocket], int | None]
1717

stdlib/tkinter/__init__.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from enum import Enum
66
from tkinter.constants import *
77
from tkinter.font import _FontDescription
88
from types import TracebackType
9-
from typing import Any, Generic, NamedTuple, Protocol, TypeVar, Union, overload
9+
from typing import Any, Generic, NamedTuple, Protocol, TypeVar, overload
1010
from typing_extensions import Literal, TypeAlias, TypedDict
1111

1212
if sys.version_info >= (3, 9):
@@ -179,7 +179,7 @@ _CanvasItemId: TypeAlias = int
179179
_Color: TypeAlias = str # typically '#rrggbb', '#rgb' or color names.
180180
_Compound: TypeAlias = Literal["top", "left", "center", "right", "bottom", "none"] # -compound in manual page named 'options'
181181
# manual page: Tk_GetCursor
182-
_Cursor: TypeAlias = Union[str, tuple[str], tuple[str, str], tuple[str, str, str], tuple[str, str, str, str]]
182+
_Cursor: TypeAlias = str | tuple[str] | tuple[str, str] | tuple[str, str, str] | tuple[str, str, str, str]
183183
# example when it's sequence: entry['invalidcommand'] = [entry.register(print), '%P']
184184
_EntryValidateCommand: TypeAlias = str | list[str] | tuple[str, ...] | Callable[[], bool]
185185
_GridIndex: TypeAlias = int | str
@@ -188,7 +188,7 @@ _Relief: TypeAlias = Literal["raised", "sunken", "flat", "ridge", "solid", "groo
188188
_ScreenUnits: TypeAlias = str | float # Often the right type instead of int. Manual page: Tk_GetPixels
189189
# -xscrollcommand and -yscrollcommand in 'options' manual page
190190
_XYScrollCommand: TypeAlias = str | Callable[[float, float], object]
191-
_TakeFocusValue: TypeAlias = Union[int, Literal[""], Callable[[str], bool | None]] # -takefocus in manual page named 'options'
191+
_TakeFocusValue: TypeAlias = int | Literal[""] | Callable[[str], bool | None] # -takefocus in manual page named 'options'
192192

193193
if sys.version_info >= (3, 11):
194194
class _VersionInfoType(NamedTuple):

stdlib/tkinter/ttk.pyi

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import tkinter
44
from _typeshed import Incomplete
55
from collections.abc import Callable
66
from tkinter.font import _FontDescription
7-
from typing import Any, Union, overload
7+
from typing import Any, overload
88
from typing_extensions import Literal, TypeAlias, TypedDict
99

1010
__all__ = [
@@ -38,13 +38,13 @@ __all__ = [
3838
def tclobjs_to_py(adict: dict[Any, Any]) -> dict[Any, Any]: ...
3939
def setup_master(master: Incomplete | None = None): ...
4040

41-
_Padding: TypeAlias = Union[
42-
tkinter._ScreenUnits,
43-
tuple[tkinter._ScreenUnits],
44-
tuple[tkinter._ScreenUnits, tkinter._ScreenUnits],
45-
tuple[tkinter._ScreenUnits, tkinter._ScreenUnits, tkinter._ScreenUnits],
46-
tuple[tkinter._ScreenUnits, tkinter._ScreenUnits, tkinter._ScreenUnits, tkinter._ScreenUnits],
47-
]
41+
_Padding: TypeAlias = (
42+
tkinter._ScreenUnits
43+
| tuple[tkinter._ScreenUnits]
44+
| tuple[tkinter._ScreenUnits, tkinter._ScreenUnits]
45+
| tuple[tkinter._ScreenUnits, tkinter._ScreenUnits, tkinter._ScreenUnits]
46+
| tuple[tkinter._ScreenUnits, tkinter._ScreenUnits, tkinter._ScreenUnits, tkinter._ScreenUnits]
47+
)
4848

4949
# from ttk_widget (aka ttk::widget) manual page, differs from tkinter._Compound
5050
_TtkCompound: TypeAlias = Literal["text", "image", tkinter._Compound]

0 commit comments

Comments
 (0)