Skip to content

Use explicit type aliases in _typeshed #7534

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

Merged
merged 5 commits into from
Mar 30, 2022
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
2 changes: 1 addition & 1 deletion requirements-tests.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mypy==0.942
pytype==2022.3.21; platform_system != "Windows" and python_version < "3.10"
pytype==2022.03.29; platform_system != "Windows" and python_version < "3.10"
# must match .pre-commit-config.yaml
black==22.1.0
flake8==4.0.1
Expand Down
32 changes: 16 additions & 16 deletions stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SupportsDunderGE(Protocol):

class SupportsAllComparisons(SupportsDunderLT, SupportsDunderGT, SupportsDunderLE, SupportsDunderGE, Protocol): ...

SupportsRichComparison = SupportsDunderLT | SupportsDunderGT
SupportsRichComparison: TypeAlias = SupportsDunderLT | SupportsDunderGT
SupportsRichComparisonT = TypeVar("SupportsRichComparisonT", bound=SupportsRichComparison) # noqa: Y001

class SupportsDivMod(Protocol[_T_contra, _T_co]):
Expand Down Expand Up @@ -95,9 +95,9 @@ class SupportsItemAccess(SupportsGetItem[_KT_contra, _VT], Protocol[_KT_contra,
def __delitem__(self, __v: _KT_contra) -> None: ...

# These aliases are simple strings in Python 2.
StrPath = str | PathLike[str] # stable
BytesPath = bytes | PathLike[bytes] # stable
StrOrBytesPath = str | bytes | PathLike[str] | PathLike[bytes] # stable
StrPath: TypeAlias = str | PathLike[str] # stable
BytesPath: TypeAlias = bytes | PathLike[bytes] # stable
StrOrBytesPath: TypeAlias = str | bytes | PathLike[str] | PathLike[bytes] # stable

OpenTextModeUpdating = Literal[
"r+",
Expand Down Expand Up @@ -133,10 +133,10 @@ OpenTextModeUpdating = Literal[
"t+x",
"+tx",
]
OpenTextModeWriting = Literal["w", "wt", "tw", "a", "at", "ta", "x", "xt", "tx"]
OpenTextModeReading = Literal["r", "rt", "tr", "U", "rU", "Ur", "rtU", "rUt", "Urt", "trU", "tUr", "Utr"]
OpenTextMode = OpenTextModeUpdating | OpenTextModeWriting | OpenTextModeReading
OpenBinaryModeUpdating = Literal[
OpenTextModeWriting: TypeAlias = Literal["w", "wt", "tw", "a", "at", "ta", "x", "xt", "tx"]
OpenTextModeReading: TypeAlias = Literal["r", "rt", "tr", "U", "rU", "Ur", "rtU", "rUt", "Urt", "trU", "tUr", "Utr"]
OpenTextMode: TypeAlias = OpenTextModeUpdating | OpenTextModeWriting | OpenTextModeReading
OpenBinaryModeUpdating: TypeAlias = Literal[
"rb+",
"r+b",
"+rb",
Expand All @@ -162,16 +162,16 @@ OpenBinaryModeUpdating = Literal[
"b+x",
"+bx",
]
OpenBinaryModeWriting = Literal["wb", "bw", "ab", "ba", "xb", "bx"]
OpenBinaryModeReading = Literal["rb", "br", "rbU", "rUb", "Urb", "brU", "bUr", "Ubr"]
OpenBinaryMode = OpenBinaryModeUpdating | OpenBinaryModeReading | OpenBinaryModeWriting
OpenBinaryModeWriting: TypeAlias = Literal["wb", "bw", "ab", "ba", "xb", "bx"]
OpenBinaryModeReading: TypeAlias = Literal["rb", "br", "rbU", "rUb", "Urb", "brU", "bUr", "Ubr"]
OpenBinaryMode: TypeAlias = OpenBinaryModeUpdating | OpenBinaryModeReading | OpenBinaryModeWriting

# stable
class HasFileno(Protocol):
def fileno(self) -> int: ...

FileDescriptor = int # stable
FileDescriptorLike = int | HasFileno # stable
FileDescriptor: TypeAlias = int # stable
FileDescriptorLike: TypeAlias = int | HasFileno # stable

# stable
class SupportsRead(Protocol[_T_co]):
Expand All @@ -189,13 +189,13 @@ class SupportsNoArgReadline(Protocol[_T_co]):
class SupportsWrite(Protocol[_T_contra]):
def write(self, __s: _T_contra) -> object: ...

ReadOnlyBuffer = bytes # stable
ReadOnlyBuffer: TypeAlias = bytes # stable
# Anything that implements the read-write buffer interface.
# The buffer interface is defined purely on the C level, so we cannot define a normal Protocol
# for it. Instead we have to list the most common stdlib buffer classes in a Union.
WriteableBuffer = bytearray | memoryview | array.array[Any] | mmap.mmap | ctypes._CData # stable
WriteableBuffer: TypeAlias = bytearray | memoryview | array.array[Any] | mmap.mmap | ctypes._CData # stable
# Same as _WriteableBuffer, but also includes read-only buffer types (like bytes).
ReadableBuffer = ReadOnlyBuffer | WriteableBuffer # stable
ReadableBuffer: TypeAlias = ReadOnlyBuffer | WriteableBuffer # stable

# stable
if sys.version_info >= (3, 10):
Expand Down
5 changes: 3 additions & 2 deletions stdlib/_typeshed/dbapi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

from collections.abc import Mapping, Sequence
from typing import Any, Protocol
from typing_extensions import TypeAlias

DBAPITypeCode = Any | None
DBAPITypeCode: TypeAlias = Any | None
# Strictly speaking, this should be a Sequence, but the type system does
# not support fixed-length sequences.
DBAPIColumnDescription = tuple[str, DBAPITypeCode, int | None, int | None, int | None, int | None, bool | None]
DBAPIColumnDescription: TypeAlias = tuple[str, DBAPITypeCode, int | None, int | None, int | None, int | None, bool | None]

class DBAPIConnection(Protocol):
def close(self) -> object: ...
Expand Down
5 changes: 3 additions & 2 deletions stdlib/_typeshed/wsgi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

from sys import _OptExcInfo
from typing import Any, Callable, Iterable, Protocol
from typing_extensions import TypeAlias

# stable
class StartResponse(Protocol):
def __call__(
self, status: str, headers: list[tuple[str, str]], exc_info: _OptExcInfo | None = ...
) -> Callable[[bytes], Any]: ...

WSGIEnvironment = dict[str, Any] # stable
WSGIApplication = Callable[[WSGIEnvironment, StartResponse], Iterable[bytes]] # stable
WSGIEnvironment: TypeAlias = dict[str, Any] # stable
WSGIApplication: TypeAlias = Callable[[WSGIEnvironment, StartResponse], Iterable[bytes]] # stable

# WSGI input streams per PEP 3333, stable
class InputStream(Protocol):
Expand Down