From 7c606500c873a030f9ca85b694341b18b933687e Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Tue, 22 Mar 2022 09:50:22 +0100 Subject: [PATCH 1/4] Use explicit type aliases in _typeshed --- stdlib/_typeshed/__init__.pyi | 34 +++++++++++++++++----------------- stdlib/_typeshed/dbapi.pyi | 5 +++-- stdlib/_typeshed/wsgi.pyi | 5 +++-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index ee3aa766d569..228961879981 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -8,7 +8,7 @@ import mmap import sys from os import PathLike from typing import AbstractSet, Any, Awaitable, Container, Generic, Iterable, Protocol, TypeVar -from typing_extensions import Final, Literal, final +from typing_extensions import Final, Literal, TypeAlias, final _KT = TypeVar("_KT") _KT_co = TypeVar("_KT_co", covariant=True) @@ -51,7 +51,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]): @@ -88,9 +88,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+", @@ -126,10 +126,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", @@ -155,16 +155,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]): @@ -182,13 +182,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): diff --git a/stdlib/_typeshed/dbapi.pyi b/stdlib/_typeshed/dbapi.pyi index eee4fc03874e..022e95996bb3 100644 --- a/stdlib/_typeshed/dbapi.pyi +++ b/stdlib/_typeshed/dbapi.pyi @@ -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: ... diff --git a/stdlib/_typeshed/wsgi.pyi b/stdlib/_typeshed/wsgi.pyi index 031d1472b6c5..9f036d8f2d33 100644 --- a/stdlib/_typeshed/wsgi.pyi +++ b/stdlib/_typeshed/wsgi.pyi @@ -4,6 +4,7 @@ from sys import _OptExcInfo from typing import Any, Callable, Iterable, Protocol +from typing_extensions import TypeAlias # stable class StartResponse(Protocol): @@ -11,8 +12,8 @@ class StartResponse(Protocol): 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): From 36e34a62b022ebe95d2b285818548e902113c38c Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Tue, 22 Mar 2022 13:26:50 +0100 Subject: [PATCH 2/4] Disable mypy cache --- tests/mypy_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/mypy_test.py b/tests/mypy_test.py index aeaf82c941af..3de3a569dba2 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -197,6 +197,7 @@ def get_mypy_flags(args, major: int, minor: int, temp_name: str, *, custom_types flags = [ "--python-version", "%d.%d" % (major, minor), + "--no-incremental", "--config-file", temp_name, "--no-site-packages", From e77228a1a828d13dac770918406db92a8df6eda1 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Tue, 22 Mar 2022 13:44:24 +0100 Subject: [PATCH 3/4] Revert "Disable mypy cache" This reverts commit 36e34a62b022ebe95d2b285818548e902113c38c. --- tests/mypy_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/mypy_test.py b/tests/mypy_test.py index 3de3a569dba2..aeaf82c941af 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -197,7 +197,6 @@ def get_mypy_flags(args, major: int, minor: int, temp_name: str, *, custom_types flags = [ "--python-version", "%d.%d" % (major, minor), - "--no-incremental", "--config-file", temp_name, "--no-site-packages", From fb8fb69c4e472ee3926b3ac33509f16ba34dd829 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 30 Mar 2022 10:41:49 +0200 Subject: [PATCH 4/4] Upgrade pytype --- requirements-tests.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-tests.txt b/requirements-tests.txt index 28f2f198dc19..c61b4f68da6b 100644 --- a/requirements-tests.txt +++ b/requirements-tests.txt @@ -1,5 +1,5 @@ mypy==0.941 -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