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

Use "|" union syntax in type aliases and base classes too #6612

Closed
wants to merge 12 commits into from
4 changes: 2 additions & 2 deletions stdlib/_codecs.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import codecs
import sys
from typing import Any, Callable, Dict, Tuple, Union
from typing import Any, Callable, Dict, Tuple

# This type is not exposed; it is defined in unicodeobject.c
class _EncodingMap:
def size(self) -> int: ...

_MapT = Union[Dict[int, int], _EncodingMap]
_MapT = Dict[int, int] | _EncodingMap
_Handler = Callable[[Exception], Tuple[str, int]]

def register(__search_function: Callable[[str], Any]) -> None: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/_csv.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Iterable, Iterator, List, Protocol, Type, Union
from typing import Any, Iterable, Iterator, List, Protocol, Type

QUOTE_ALL: int
QUOTE_MINIMAL: int
Expand All @@ -18,7 +18,7 @@ class Dialect:
strict: int
def __init__(self) -> None: ...

_DialectLike = Union[str, Dialect, Type[Dialect]]
_DialectLike = str | Dialect | Type[Dialect]

class _reader(Iterator[List[str]]):
dialect: Dialect
Expand Down
4 changes: 2 additions & 2 deletions stdlib/_curses.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from typing import IO, Any, BinaryIO, NamedTuple, Union, overload
from typing import IO, Any, BinaryIO, NamedTuple, overload

_chtype = Union[str, bytes, int]
_chtype = str | bytes | int

# ACS codes are only initialized after initscr is called
ACS_BBSS: int
Expand Down
4 changes: 2 additions & 2 deletions stdlib/_dummy_threading.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import sys
from types import FrameType, TracebackType
from typing import Any, Callable, Iterable, Mapping, Optional, Type, TypeVar
from typing import Any, Callable, Iterable, Mapping, Type, TypeVar

# TODO recursive type
_TF = Callable[[FrameType, str, Any], Optional[Callable[..., Any]]]
_TF = Callable[[FrameType, str, Any], Callable[..., Any] | None]

_PF = Callable[[FrameType, str, Any], None]
_T = TypeVar("_T")
Expand Down
4 changes: 2 additions & 2 deletions stdlib/_socket.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from _typeshed import ReadableBuffer, WriteableBuffer
from collections.abc import Iterable
from typing import Any, SupportsInt, Tuple, Union, overload
from typing import Any, SupportsInt, Tuple, overload

if sys.version_info >= (3, 8):
from typing import SupportsIndex
Expand All @@ -15,7 +15,7 @@ _CMSGArg = Tuple[int, int, ReadableBuffer]

# Addresses can be either tuples of varying lengths (AF_INET, AF_INET6,
# AF_NETLINK, AF_TIPC) or strings (AF_UNIX).
_Address = Union[Tuple[Any, ...], str]
_Address = Tuple[Any, ...] | str
_RetAddress = Any
# TODO Most methods allow bytes as address objects

Expand Down
4 changes: 2 additions & 2 deletions stdlib/_thread.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from threading import Thread
from types import TracebackType
from typing import Any, Callable, NoReturn, Optional, Tuple, Type
from typing import Any, Callable, NoReturn, Tuple, Type
from typing_extensions import final

error = RuntimeError
Expand Down Expand Up @@ -32,7 +32,7 @@ TIMEOUT_MAX: float
if sys.version_info >= (3, 8):
def get_native_id() -> int: ... # only available on some platforms
@final
class _ExceptHookArgs(Tuple[Type[BaseException], Optional[BaseException], Optional[TracebackType], Optional[Thread]]):
class _ExceptHookArgs(Tuple[Type[BaseException], BaseException | None, TracebackType | None, Thread | None]):
@property
def exc_type(self) -> Type[BaseException]: ...
@property
Expand Down
4 changes: 2 additions & 2 deletions stdlib/aifc.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from _typeshed import Self
from types import TracebackType
from typing import IO, Any, NamedTuple, Tuple, Type, Union, overload
from typing import IO, Any, NamedTuple, Tuple, Type, overload
from typing_extensions import Literal

class Error(Exception): ...
Expand All @@ -14,7 +14,7 @@ class _aifc_params(NamedTuple):
comptype: bytes
compname: bytes

_File = Union[str, IO[bytes]]
_File = str | IO[bytes]
_Marker = Tuple[int, int, bytes]

class Aifc_read:
Expand Down
4 changes: 2 additions & 2 deletions stdlib/array.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import sys
from typing import Any, BinaryIO, Generic, Iterable, MutableSequence, TypeVar, Union, overload
from typing import Any, BinaryIO, Generic, Iterable, MutableSequence, TypeVar, overload
from typing_extensions import Literal, SupportsIndex

_IntTypeCode = Literal["b", "B", "h", "H", "i", "I", "l", "L", "q", "Q"]
_FloatTypeCode = Literal["f", "d"]
_UnicodeTypeCode = Literal["u"]
_TypeCode = Union[_IntTypeCode, _FloatTypeCode, _UnicodeTypeCode]
_TypeCode = _IntTypeCode | _FloatTypeCode | _UnicodeTypeCode

_T = TypeVar("_T", int, float, str)

Expand Down
4 changes: 2 additions & 2 deletions stdlib/asyncio/base_events.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ from asyncio.tasks import Task
from asyncio.transports import BaseTransport
from collections.abc import Iterable
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
from typing import IO, Any, Awaitable, Callable, Dict, Generator, Sequence, Tuple, TypeVar, Union, overload
from typing import IO, Any, Awaitable, Callable, Dict, Generator, Sequence, Tuple, TypeVar, overload
from typing_extensions import Literal

if sys.version_info >= (3, 7):
Expand All @@ -19,7 +19,7 @@ _T = TypeVar("_T")
_Context = Dict[str, Any]
_ExceptionHandler = Callable[[AbstractEventLoop, _Context], Any]
_ProtocolFactory = Callable[[], BaseProtocol]
_SSLContext = Union[bool, None, ssl.SSLContext]
_SSLContext = bool | None | ssl.SSLContext
_TransProtPair = Tuple[BaseTransport, BaseProtocol]

class Server(AbstractServer):
Expand Down
4 changes: 2 additions & 2 deletions stdlib/asyncio/base_subprocess.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import subprocess
from collections import deque
from typing import IO, Any, Callable, Optional, Sequence, Tuple, Union
from typing import IO, Any, Callable, Sequence, Tuple

from . import events, futures, protocols, transports

_File = Optional[Union[int, IO[Any]]]
_File = int | IO[Any] | None

class BaseSubprocessTransport(transports.SubprocessTransport):

Expand Down
4 changes: 2 additions & 2 deletions stdlib/asyncio/events.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sys
from _typeshed import FileDescriptorLike, Self
from abc import ABCMeta, abstractmethod
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
from typing import IO, Any, Awaitable, Callable, Dict, Generator, Sequence, Tuple, TypeVar, Union, overload
from typing import IO, Any, Awaitable, Callable, Dict, Generator, Sequence, Tuple, TypeVar, overload
from typing_extensions import Literal

from .base_events import Server
Expand All @@ -20,7 +20,7 @@ _T = TypeVar("_T")
_Context = Dict[str, Any]
_ExceptionHandler = Callable[[AbstractEventLoop, _Context], Any]
_ProtocolFactory = Callable[[], BaseProtocol]
_SSLContext = Union[bool, None, ssl.SSLContext]
_SSLContext = bool | None | ssl.SSLContext
_TransProtPair = Tuple[BaseTransport, BaseProtocol]

class Handle:
Expand Down
4 changes: 2 additions & 2 deletions stdlib/asyncio/format_helpers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import functools
import sys
import traceback
from types import FrameType, FunctionType
from typing import Any, Iterable, Union, overload
from typing import Any, Iterable, overload

class _HasWrapper:
__wrapper__: _HasWrapper | FunctionType

_FuncType = Union[FunctionType, _HasWrapper, functools.partial[Any], functools.partialmethod[Any]]
_FuncType = FunctionType | _HasWrapper | functools.partial[Any] | functools.partialmethod[Any]

if sys.version_info >= (3, 7):
@overload
Expand Down
4 changes: 2 additions & 2 deletions stdlib/asyncio/streams.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import sys
from _typeshed import StrPath
from typing import Any, AsyncIterator, Awaitable, Callable, Iterable, Optional
from typing import Any, AsyncIterator, Awaitable, Callable, Iterable

from . import events, protocols, transports
from .base_events import Server

_ClientConnectedCallback = Callable[[StreamReader, StreamWriter], Optional[Awaitable[None]]]
_ClientConnectedCallback = Callable[[StreamReader, StreamWriter], Awaitable[None] | None]

if sys.version_info < (3, 8):
class IncompleteReadError(EOFError):
Expand Down
4 changes: 2 additions & 2 deletions stdlib/asyncio/subprocess.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import subprocess
import sys
from _typeshed import StrOrBytesPath
from asyncio import events, protocols, streams, transports
from typing import IO, Any, Callable, Union
from typing import IO, Any, Callable
from typing_extensions import Literal

if sys.version_info >= (3, 8):
_ExecArg = StrOrBytesPath
else:
_ExecArg = Union[str, bytes]
_ExecArg = str | bytes

PIPE: int
STDOUT: int
Expand Down
6 changes: 3 additions & 3 deletions stdlib/asyncio/trsock.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import socket
import sys
from types import TracebackType
from typing import Any, BinaryIO, Iterable, NoReturn, Tuple, Type, Union, overload
from typing import Any, BinaryIO, Iterable, NoReturn, Tuple, Type, overload

if sys.version_info >= (3, 8):
# 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 = Union[bytearray, memoryview]
_WriteBuffer = bytearray | memoryview
_CMSG = Tuple[int, int, bytes]
class TransportSocket:
def __init__(self, sock: socket.socket) -> None: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/binhex.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import IO, Any, Tuple, Union
from typing import IO, Any, Tuple

class Error(Exception): ...

Expand All @@ -13,7 +13,7 @@ class FInfo:
Flags: int

_FileInfoTuple = Tuple[str, FInfo, int, int]
_FileHandleUnion = Union[str, IO[bytes]]
_FileHandleUnion = str | IO[bytes]

def getfileinfo(name: str) -> _FileInfoTuple: ...

Expand Down
2 changes: 1 addition & 1 deletion stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ class _SupportsPow3NoneOnly(Protocol[_E, _T_co]):
class _SupportsPow3(Protocol[_E, _M, _T_co]):
def __pow__(self, __other: _E, __modulo: _M) -> _T_co: ...

_SupportsSomeKindOfPow = Union[_SupportsPow2[Any, Any], _SupportsPow3NoneOnly[Any, Any], _SupportsPow3[Any, Any, Any]]
_SupportsSomeKindOfPow = _SupportsPow2[Any, Any] | _SupportsPow3NoneOnly[Any, Any] | _SupportsPow3[Any, Any, Any]

if sys.version_info >= (3, 8):
@overload
Expand Down
4 changes: 2 additions & 2 deletions stdlib/calendar.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import datetime
import sys
from time import struct_time
from typing import Any, Iterable, Optional, Sequence, Tuple
from typing import Any, Iterable, Sequence, Tuple

_LocaleType = Tuple[Optional[str], Optional[str]]
_LocaleType = Tuple[str | None, str | None]

class IllegalMonthError(ValueError):
def __init__(self, month: int) -> None: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/cgitb.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from _typeshed import StrOrBytesPath
from types import FrameType, TracebackType
from typing import IO, Any, Callable, Optional, Tuple, Type
from typing import IO, Any, Callable, Tuple, Type

_ExcInfo = Tuple[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]]
_ExcInfo = Tuple[Type[BaseException] | None, BaseException | None, TracebackType | None]

def reset() -> str: ... # undocumented
def small(text: str) -> str: ... # undocumented
Expand Down
6 changes: 3 additions & 3 deletions stdlib/cmath.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from typing import SupportsComplex, SupportsFloat, Union
from typing import SupportsComplex, SupportsFloat

if sys.version_info >= (3, 8):
from typing import SupportsIndex
Expand All @@ -13,9 +13,9 @@ nanj: complex
tau: float

if sys.version_info >= (3, 8):
_C = Union[SupportsFloat, SupportsComplex, SupportsIndex, complex]
_C = SupportsFloat | SupportsComplex | SupportsIndex | complex
else:
_C = Union[SupportsFloat, SupportsComplex, complex]
_C = SupportsFloat | SupportsComplex | complex

def acos(__z: _C) -> complex: ...
def acosh(__z: _C) -> complex: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/configparser.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from _typeshed import StrOrBytesPath, StrPath, SupportsWrite
from collections.abc import Callable, ItemsView, Iterable, Iterator, Mapping, MutableMapping, Sequence
from typing import Any, ClassVar, Dict, Optional, Pattern, Type, TypeVar, overload
from typing import Any, ClassVar, Dict, Pattern, Type, TypeVar, overload
from typing_extensions import Literal

# Internal type aliases
Expand Down Expand Up @@ -180,7 +180,7 @@ class SectionProxy(MutableMapping[str, str]):
# SectionProxy can have arbitrary attributes when custom converters are used
def __getattr__(self, key: str) -> Callable[..., Any]: ...

class ConverterMapping(MutableMapping[str, Optional[_converter]]):
class ConverterMapping(MutableMapping[str, _converter | None]):
GETTERCRE: Pattern[Any]
def __init__(self, parser: RawConfigParser) -> None: ...
def __getitem__(self, key: str) -> _converter: ...
Expand Down
7 changes: 3 additions & 4 deletions stdlib/contextlib.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ from typing import (
ContextManager,
Generic,
Iterator,
Optional,
Protocol,
Type,
TypeVar,
Expand All @@ -26,11 +25,11 @@ if sys.version_info >= (3, 7):

_T = TypeVar("_T")
_T_co = TypeVar("_T_co", covariant=True)
_T_io = TypeVar("_T_io", bound=Optional[IO[str]])
_T_io = TypeVar("_T_io", bound=IO[str] | None)
_F = TypeVar("_F", bound=Callable[..., Any])
_P = ParamSpec("_P")

_ExitFunc = Callable[[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]], bool]
_ExitFunc = Callable[[Type[BaseException] | None, BaseException | None, TracebackType | None], bool]
_CM_EF = TypeVar("_CM_EF", AbstractContextManager[Any], _ExitFunc)

class _GeneratorContextManager(AbstractContextManager[_T_co]):
Expand Down Expand Up @@ -88,7 +87,7 @@ class ExitStack(AbstractContextManager[ExitStack]):
) -> bool: ...

if sys.version_info >= (3, 7):
_ExitCoroFunc = Callable[[Optional[Type[BaseException]], Optional[BaseException], Optional[TracebackType]], Awaitable[bool]]
_ExitCoroFunc = Callable[[Type[BaseException] | None, BaseException | None, TracebackType | None], Awaitable[bool]]
_CallbackCoroFunc = Callable[..., Awaitable[Any]]
_ACM_EF = TypeVar("_ACM_EF", AbstractAsyncContextManager[Any], _ExitCoroFunc)
class AsyncExitStack(AbstractAsyncContextManager[AsyncExitStack]):
Expand Down
9 changes: 4 additions & 5 deletions stdlib/ctypes/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ from typing import (
Iterable,
Iterator,
Mapping,
Optional,
Sequence,
Tuple,
Type,
Expand Down Expand Up @@ -98,7 +97,7 @@ class _CData(metaclass=_CDataMeta):
class _CanCastTo(_CData): ...
class _PointerLike(_CanCastTo): ...

_ECT = Callable[[Optional[Type[_CData]], _FuncPointer, Tuple[_CData, ...]], _CData]
_ECT = Callable[[Type[_CData] | None, _FuncPointer, Tuple[_CData, ...]], _CData]
_PF = _UnionT[Tuple[int], Tuple[int, str], Tuple[int, str, Any]]

class _FuncPointer(_PointerLike, _CData):
Expand Down Expand Up @@ -209,7 +208,7 @@ class c_byte(_SimpleCData[int]): ...
class c_char(_SimpleCData[bytes]):
def __init__(self, value: int | bytes = ...) -> None: ...

class c_char_p(_PointerLike, _SimpleCData[Optional[bytes]]):
class c_char_p(_PointerLike, _SimpleCData[bytes | None]):
def __init__(self, value: int | bytes | None = ...) -> None: ...

class c_double(_SimpleCData[float]): ...
Expand All @@ -234,10 +233,10 @@ class c_uint64(_SimpleCData[int]): ...
class c_ulong(_SimpleCData[int]): ...
class c_ulonglong(_SimpleCData[int]): ...
class c_ushort(_SimpleCData[int]): ...
class c_void_p(_PointerLike, _SimpleCData[Optional[int]]): ...
class c_void_p(_PointerLike, _SimpleCData[int | None]): ...
class c_wchar(_SimpleCData[str]): ...

class c_wchar_p(_PointerLike, _SimpleCData[Optional[str]]):
class c_wchar_p(_PointerLike, _SimpleCData[str | None]):
def __init__(self, value: int | str | None = ...) -> None: ...

class c_bool(_SimpleCData[bool]):
Expand Down
6 changes: 3 additions & 3 deletions stdlib/dbm/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from _typeshed import Self
from types import TracebackType
from typing import Iterator, MutableMapping, Type, Union
from typing import Iterator, MutableMapping, Type
from typing_extensions import Literal

_KeyType = Union[str, bytes]
_ValueType = Union[str, bytes]
_KeyType = str | bytes
_ValueType = str | bytes
_TFlags = Literal[
"r",
"w",
Expand Down
Loading