Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable flake8-pyi's Y037 #9686

Merged
merged 4 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@
# F403 import *' used; unable to detect undefined names
# F405 defined from star imports

# Rules that we'd like to enable in the future:
# Y037 Use PEP 604 syntax instead of `typing.Union` and `typing.Optional`.
# Currently can't be enabled due to a few lingering bugs in mypy regarding
# PEP 604 type aliases (see #4819).

[flake8]
per-file-ignores =
*.py: E203, E301, E302, E305, E501
*.pyi: B, E301, E302, E305, E501, E701, E741, F401, F403, F405, F822, Y037
*.pyi: B, E301, E302, E305, E501, E701, E741, F401, F403, F405, F822
# Since typing.pyi defines "overload" this is not recognized by flake8 as typing.overload.
# Unfortunately, flake8 does not allow to "noqa" just a specific error inside the file itself.
# https://github.com/PyCQA/flake8/issues/1079
# F811 redefinition of unused '...'
stdlib/typing.pyi: B, E301, E302, E305, E501, E701, E741, F401, F403, F405, F811, F822, Y037
stdlib/typing.pyi: B, E301, E302, E305, E501, E701, E741, F401, F403, F405, F811, F822
# Generated protobuf files include docstrings
*_pb2.pyi: B, E301, E302, E305, E501, E701, Y021, Y026

Expand Down
4 changes: 2 additions & 2 deletions stdlib/_csv.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from _typeshed import SupportsWrite
from collections.abc import Iterable, Iterator
from typing import Any, Union
from typing import Any
from typing_extensions import Literal, TypeAlias

__version__: str
Expand All @@ -27,7 +27,7 @@ class Dialect:
strict: bool
def __init__(self) -> None: ...

_DialectLike: TypeAlias = Union[str, Dialect, type[Dialect]]
_DialectLike: TypeAlias = str | Dialect | type[Dialect]

class _reader(Iterator[list[str]]):
@property
Expand Down
4 changes: 2 additions & 2 deletions stdlib/_decimal.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import sys
from _typeshed import Self
from collections.abc import Container, Sequence
from types import TracebackType
from typing import Any, ClassVar, NamedTuple, Union, overload
from typing import Any, ClassVar, NamedTuple, overload
from typing_extensions import Literal, TypeAlias

_Decimal: TypeAlias = Decimal | int
_DecimalNew: TypeAlias = Union[Decimal, float, str, tuple[int, Sequence[int], int]]
_DecimalNew: TypeAlias = Decimal | float | str | tuple[int, Sequence[int], int]
_ComparableNum: TypeAlias = Decimal | float | numbers.Rational

__version__: str
Expand Down
4 changes: 2 additions & 2 deletions stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from collections.abc import Awaitable, Callable, Iterable, Set as AbstractSet
from dataclasses import Field
from os import PathLike
from types import FrameType, TracebackType
from typing import Any, AnyStr, ClassVar, Generic, Protocol, TypeVar, Union
from typing import Any, AnyStr, ClassVar, Generic, Protocol, TypeVar
from typing_extensions import Final, Literal, LiteralString, TypeAlias, final

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

ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, TracebackType]
OptExcInfo: TypeAlias = Union[ExcInfo, tuple[None, None, None]]
OptExcInfo: TypeAlias = ExcInfo | tuple[None, None, None]

# stable
if sys.version_info >= (3, 10):
Expand Down
4 changes: 2 additions & 2 deletions stdlib/copyreg.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from collections.abc import Callable, Hashable
from typing import Any, SupportsInt, TypeVar, Union
from typing import Any, SupportsInt, TypeVar
from typing_extensions import TypeAlias

_T = TypeVar("_T")
_Reduce: TypeAlias = Union[tuple[Callable[..., _T], tuple[Any, ...]], tuple[Callable[..., _T], tuple[Any, ...], Any | None]]
_Reduce: TypeAlias = tuple[Callable[..., _T], tuple[Any, ...]] | tuple[Callable[..., _T], tuple[Any, ...], Any | None]

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

Expand Down
4 changes: 2 additions & 2 deletions stdlib/ctypes/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from _ctypes import RTLD_GLOBAL as RTLD_GLOBAL, RTLD_LOCAL as RTLD_LOCAL
from _typeshed import ReadableBuffer, Self, WriteableBuffer
from abc import abstractmethod
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
from typing import Any, ClassVar, Generic, TypeVar, Union as _UnionT, overload
from typing import Any, ClassVar, Generic, TypeVar, overload
from typing_extensions import TypeAlias

if sys.version_info >= (3, 9):
Expand Down Expand Up @@ -91,7 +91,7 @@ class _CanCastTo(_CData): ...
class _PointerLike(_CanCastTo): ...

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

class _FuncPointer(_PointerLike, _CData):
restype: type[_CData] | Callable[[int], Any] | None
Expand Down
4 changes: 2 additions & 2 deletions stdlib/distutils/ccompiler.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from collections.abc import Callable
from typing import Any, Union
from typing import Any
from typing_extensions import TypeAlias

_Macro: TypeAlias = Union[tuple[str], tuple[str, str | None]]
_Macro: TypeAlias = tuple[str] | tuple[str, str | None]

def gen_lib_options(
compiler: CCompiler, library_dirs: list[str], runtime_library_dirs: list[str], libraries: list[str]
Expand Down
6 changes: 3 additions & 3 deletions stdlib/email/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from collections.abc import Callable
from email.message import Message
from email.policy import Policy
from typing import IO, Union
from typing import IO
from typing_extensions import TypeAlias

# Definitions imported by multiple submodules in typeshed
_ParamType: TypeAlias = Union[str, tuple[str | None, str | None, str]] # noqa: Y047
_ParamsType: TypeAlias = Union[str, None, tuple[str, str | None, str]] # noqa: Y047
_ParamType: TypeAlias = str | tuple[str | None, str | None, str] # noqa: Y047
_ParamsType: TypeAlias = str | None | tuple[str, str | None, str] # noqa: Y047

def message_from_string(s: str, _class: Callable[[], Message] = ..., *, policy: Policy = ...) -> Message: ...
def message_from_bytes(s: bytes | bytearray, _class: Callable[[], Message] = ..., *, policy: Policy = ...) -> Message: ...
Expand Down
8 changes: 4 additions & 4 deletions stdlib/inspect.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ from types import (
TracebackType,
WrapperDescriptorType,
)
from typing import Any, ClassVar, NamedTuple, Protocol, TypeVar, Union, overload
from typing import Any, ClassVar, NamedTuple, Protocol, TypeVar, overload
from typing_extensions import Literal, ParamSpec, TypeAlias, TypeGuard

if sys.version_info >= (3, 11):
Expand Down Expand Up @@ -264,9 +264,9 @@ def isdatadescriptor(object: object) -> TypeGuard[_SupportsSet[Any, Any] | _Supp
#
# Retrieving source code
#
_SourceObjectType: TypeAlias = Union[
ModuleType, type[Any], MethodType, FunctionType, TracebackType, FrameType, CodeType, Callable[..., Any]
]
_SourceObjectType: TypeAlias = (
ModuleType | type[Any] | MethodType | FunctionType | TracebackType | FrameType | CodeType | Callable[..., Any]
)

def findsource(object: _SourceObjectType) -> tuple[list[str], int]: ...
def getabsfile(object: _SourceObjectType, _filename: str | None = None) -> str: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/logging/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ from re import Pattern
from string import Template
from time import struct_time
from types import FrameType, TracebackType
from typing import Any, ClassVar, Generic, TextIO, TypeVar, Union, overload
from typing import Any, ClassVar, Generic, TextIO, TypeVar, overload
from typing_extensions import Literal, TypeAlias

if sys.version_info >= (3, 11):
Expand Down Expand Up @@ -61,7 +61,7 @@ __all__ = [
if sys.version_info >= (3, 11):
__all__ += ["getLevelNamesMapping"]

_SysExcInfoType: TypeAlias = Union[tuple[type[BaseException], BaseException, TracebackType | None], tuple[None, None, None]]
_SysExcInfoType: TypeAlias = tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None]
_ExcInfoType: TypeAlias = None | bool | _SysExcInfoType | BaseException
_ArgsType: TypeAlias = tuple[object, ...] | Mapping[str, object]
_FilterType: TypeAlias = Filter | Callable[[LogRecord], bool]
Expand Down
38 changes: 19 additions & 19 deletions stdlib/marshal.pyi
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import builtins
import types
from _typeshed import ReadableBuffer, SupportsRead, SupportsWrite
from typing import Any, Union
from typing import Any
from typing_extensions import TypeAlias

version: int

_Marshallable: TypeAlias = Union[
_Marshallable: TypeAlias = (
# handled in w_object() in marshal.c
None,
type[StopIteration],
builtins.ellipsis,
bool,
None
| type[StopIteration]
| builtins.ellipsis
| bool
# handled in w_complex_object() in marshal.c
int,
float,
complex,
bytes,
str,
tuple[_Marshallable, ...],
list[Any],
dict[Any, Any],
set[Any],
frozenset[_Marshallable],
types.CodeType,
ReadableBuffer,
]
| int
| float
| complex
| bytes
| str
| tuple[_Marshallable, ...]
| list[Any]
| dict[Any, Any]
| set[Any]
| frozenset[_Marshallable]
| types.CodeType
| ReadableBuffer
)

def dump(__value: _Marshallable, __file: SupportsWrite[bytes], __version: int = 4) -> None: ...
def load(__file: SupportsRead[bytes]) -> Any: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/multiprocessing/connection.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import sys
import types
from _typeshed import ReadableBuffer, Self
from collections.abc import Iterable
from typing import Any, Union
from typing import Any
from typing_extensions import SupportsIndex, TypeAlias

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

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

class _ConnectionBase:
def __init__(self, handle: SupportsIndex, readable: bool = True, writable: bool = True) -> None: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/multiprocessing/dummy/connection.pyi
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from _typeshed import Self
from queue import Queue
from types import TracebackType
from typing import Any, Union
from typing import Any
from typing_extensions import TypeAlias

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

families: list[None]

_Address: TypeAlias = Union[str, tuple[str, int]]
_Address: TypeAlias = str | tuple[str, int]

class Connection:
_in: Any
Expand Down
16 changes: 8 additions & 8 deletions stdlib/pickle.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from _typeshed import ReadableBuffer, SupportsWrite
from collections.abc import Callable, Iterable, Iterator, Mapping
from typing import Any, ClassVar, Protocol, SupportsBytes, Union
from typing import Any, ClassVar, Protocol, SupportsBytes
from typing_extensions import SupportsIndex, TypeAlias, final

__all__ = [
Expand Down Expand Up @@ -142,13 +142,13 @@ class PickleError(Exception): ...
class PicklingError(PickleError): ...
class UnpicklingError(PickleError): ...

_ReducedType: TypeAlias = Union[
str,
tuple[Callable[..., Any], tuple[Any, ...]],
tuple[Callable[..., Any], tuple[Any, ...], Any],
tuple[Callable[..., Any], tuple[Any, ...], Any, Iterator[Any] | None],
tuple[Callable[..., Any], tuple[Any, ...], Any, Iterator[Any] | None, Iterator[Any] | None],
]
_ReducedType: TypeAlias = (
str
| tuple[Callable[..., Any], tuple[Any, ...]]
| tuple[Callable[..., Any], tuple[Any, ...], Any]
| tuple[Callable[..., Any], tuple[Any, ...], Any, Iterator[Any] | None]
| tuple[Callable[..., Any], tuple[Any, ...], Any, Iterator[Any] | None, Iterator[Any] | None]
)

class Pickler:
fast: bool
Expand Down
4 changes: 2 additions & 2 deletions stdlib/signal.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ from _typeshed import structseq
from collections.abc import Callable, Iterable
from enum import IntEnum
from types import FrameType
from typing import Any, Union
from typing import Any
from typing_extensions import Final, Never, TypeAlias, final

NSIG: int
Expand Down Expand Up @@ -62,7 +62,7 @@ SIG_DFL: Handlers
SIG_IGN: Handlers

_SIGNUM: TypeAlias = int | Signals
_HANDLER: TypeAlias = Union[Callable[[int, FrameType | None], Any], int, Handlers, None]
_HANDLER: TypeAlias = Callable[[int, FrameType | None], Any] | int | Handlers | None

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

Expand Down
4 changes: 2 additions & 2 deletions stdlib/socketserver.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from _socket import _Address, _RetAddress
from _typeshed import ReadableBuffer, Self
from collections.abc import Callable
from socket import socket as _socket
from typing import Any, BinaryIO, ClassVar, Union
from typing import Any, BinaryIO, ClassVar
from typing_extensions import TypeAlias

__all__ = [
Expand All @@ -29,7 +29,7 @@ if sys.platform != "win32":
"UnixStreamServer",
]

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

Expand Down
4 changes: 2 additions & 2 deletions stdlib/ssl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import socket
import sys
from _typeshed import ReadableBuffer, Self, StrOrBytesPath, WriteableBuffer
from collections.abc import Callable, Iterable
from typing import Any, NamedTuple, Union, overload
from typing import Any, NamedTuple, overload
from typing_extensions import Literal, TypeAlias, TypedDict, final

_PCTRTT: TypeAlias = tuple[tuple[str, str], ...]
_PCTRTTT: TypeAlias = tuple[_PCTRTT, ...]
_PeerCertRetDictType: TypeAlias = dict[str, str | _PCTRTTT | _PCTRTT]
_PeerCertRetType: TypeAlias = _PeerCertRetDictType | bytes | None
_EnumRetType: TypeAlias = list[tuple[bytes, str, set[str] | bool]]
_PasswordType: TypeAlias = Union[Callable[[], str | bytes | bytearray], str, bytes, bytearray]
_PasswordType: TypeAlias = Callable[[], str | bytes | bytearray] | str | bytes | bytearray

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

Expand Down
6 changes: 3 additions & 3 deletions stdlib/tkinter/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ from enum import Enum
from tkinter.constants import *
from tkinter.font import _FontDescription
from types import TracebackType
from typing import Any, Generic, NamedTuple, Protocol, TypeVar, Union, overload
from typing import Any, Generic, NamedTuple, Protocol, TypeVar, overload
from typing_extensions import Literal, TypeAlias, TypedDict

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

if sys.version_info >= (3, 11):
class _VersionInfoType(NamedTuple):
Expand Down
16 changes: 8 additions & 8 deletions stdlib/tkinter/ttk.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import tkinter
from _typeshed import Incomplete
from collections.abc import Callable
from tkinter.font import _FontDescription
from typing import Any, Union, overload
from typing import Any, overload
from typing_extensions import Literal, TypeAlias, TypedDict

__all__ = [
Expand Down Expand Up @@ -38,13 +38,13 @@ __all__ = [
def tclobjs_to_py(adict: dict[Any, Any]) -> dict[Any, Any]: ...
def setup_master(master: Incomplete | None = None): ...

_Padding: TypeAlias = Union[
tkinter._ScreenUnits,
tuple[tkinter._ScreenUnits],
tuple[tkinter._ScreenUnits, tkinter._ScreenUnits],
tuple[tkinter._ScreenUnits, tkinter._ScreenUnits, tkinter._ScreenUnits],
tuple[tkinter._ScreenUnits, tkinter._ScreenUnits, tkinter._ScreenUnits, tkinter._ScreenUnits],
]
_Padding: TypeAlias = (
tkinter._ScreenUnits
| tuple[tkinter._ScreenUnits]
| tuple[tkinter._ScreenUnits, tkinter._ScreenUnits]
| tuple[tkinter._ScreenUnits, tkinter._ScreenUnits, tkinter._ScreenUnits]
| tuple[tkinter._ScreenUnits, tkinter._ScreenUnits, tkinter._ScreenUnits, tkinter._ScreenUnits]
)

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