From 24a65816b62d37965dc877b0e69ccc42c4767fe8 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sat, 21 May 2022 18:27:28 +0200 Subject: [PATCH 1/7] Turn typing_extensions into a package --- .flake8 | 2 +- .github/workflows/ci.yml | 4 ++-- README.md | 2 +- .../__init__.py} | 2 +- src/{typing_extensions.py => typing_extensions/__init__.py} | 0 5 files changed, 5 insertions(+), 5 deletions(-) rename src/{test_typing_extensions.py => test_typing_extensions/__init__.py} (99%) rename src/{typing_extensions.py => typing_extensions/__init__.py} (100%) diff --git a/.flake8 b/.flake8 index fc71a6e2..a1f7fffd 100644 --- a/.flake8 +++ b/.flake8 @@ -12,4 +12,4 @@ ignore = exclude = # tests have more relaxed formatting rules # and its own specific config in .flake8-tests - src/test_typing_extensions.py, + src/test_typing_extensions, diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 43986802..2f0d768e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,7 +36,7 @@ jobs: # Be wary of running `pip install` here, since it becomes easy for us to # accidentally pick up typing_extensions as installed by a dependency cd src - python -m unittest test_typing_extensions.py + python -m unittest test_typing_extensions linting: name: Lint @@ -61,4 +61,4 @@ jobs: run: flake8 - name: Lint tests - run: flake8 --config=.flake8-tests src/test_typing_extensions.py + run: flake8 --config=.flake8-tests src/test_typing_extensions diff --git a/README.md b/README.md index 55a23185..4eb5da73 100644 --- a/README.md +++ b/README.md @@ -141,4 +141,4 @@ These types are only guaranteed to work for static type checking. ## Running tests To run tests, navigate into the appropriate source directory and run -`test_typing_extensions.py`. +`test_typing_extensions/__init__.py`. diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions/__init__.py similarity index 99% rename from src/test_typing_extensions.py rename to src/test_typing_extensions/__init__.py index 7f14f3f9..c8e9b491 100644 --- a/src/test_typing_extensions.py +++ b/src/test_typing_extensions/__init__.py @@ -2883,7 +2883,7 @@ def test_typing_extensions_defers_when_possible(self): def test_typing_extensions_compiles_with_opt(self): file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), - 'typing_extensions.py') + '../typing_extensions/__init__.py') try: subprocess.check_output(f'{sys.executable} -OO {file_path}', stderr=subprocess.STDOUT, diff --git a/src/typing_extensions.py b/src/typing_extensions/__init__.py similarity index 100% rename from src/typing_extensions.py rename to src/typing_extensions/__init__.py From e4da1dc12f555fc525456530061d44ab6a04f982 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sat, 21 May 2022 19:01:00 +0200 Subject: [PATCH 2/7] Add typing_extensions.ext Closes: #6 --- CHANGELOG.md | 19 +++++ src/test_typing_extensions/__init__.py | 2 + src/test_typing_extensions/ext.py | 2 + src/typing_extensions/ext.py | 99 ++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 src/test_typing_extensions/ext.py create mode 100644 src/typing_extensions/ext.py diff --git a/CHANGELOG.md b/CHANGELOG.md index aa66e55c..e200e78b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,22 @@ +# UNRELEASED + +- Add the `typing\_extensions.ext` module, which contains useful protocols + and type aliases that are not scheduled for inclusion in `typing`. + - `SupportsAnext` + - `SupportsDivMod` + - `SupportsGetItem` + - `SupportsNext` + - `SupportsRDivMod` + - `SupportsTrunc` + - `SupportsKeys` + - `SupportsItems` + - `SupportsKeysAndGetItem` + - `SupportsRead` + - `SupportsReadline` + - `SupportsWrite` + - `StrPath` + - `BytesPath` + # Release 4.2.0 (April 17, 2022) - Re-export `typing.Unpack` and `typing.TypeVarTuple` on Python 3.11. diff --git a/src/test_typing_extensions/__init__.py b/src/test_typing_extensions/__init__.py index c8e9b491..8c5704af 100644 --- a/src/test_typing_extensions/__init__.py +++ b/src/test_typing_extensions/__init__.py @@ -28,6 +28,8 @@ from typing_extensions import assert_type, get_type_hints, get_origin, get_args from typing_extensions import clear_overloads, get_overloads, overload +from .ext import * + # Flags used to mark tests that only apply after a specific # version of the typing module. TYPING_3_8_0 = sys.version_info[:3] >= (3, 8, 0) diff --git a/src/test_typing_extensions/ext.py b/src/test_typing_extensions/ext.py new file mode 100644 index 00000000..b2fb7a89 --- /dev/null +++ b/src/test_typing_extensions/ext.py @@ -0,0 +1,2 @@ +# We just import the ext module to check for syntax or import problems. +from typing_extensions.ext import * diff --git a/src/typing_extensions/ext.py b/src/typing_extensions/ext.py new file mode 100644 index 00000000..7141c42c --- /dev/null +++ b/src/typing_extensions/ext.py @@ -0,0 +1,99 @@ +""" +Protocols and type aliases that are not scheduled for inclusion in typing. +""" + +from os import PathLike +from typing import AbstractSet, Awaitable, Container, Iterable, Tuple, TypeVar, Union +from typing_extensions import Protocol, TypeAlias + +_T_co = TypeVar("_T_co", covariant=True) +_T_contra = TypeVar("_T_contra", contravariant=True) +_KT = TypeVar("_KT") +_KT_co = TypeVar("_KT_co", covariant=True) +_KT_contra = TypeVar("_KT_contra", contravariant=True) +_VT_co = TypeVar("_VT_co", covariant=True) + +# +# Protocols for dunder methods +# + + +class SupportsAnext(Protocol[_T_co]): + def __anext__(self) -> Awaitable[_T_co]: + ... + + +class SupportsDivMod(Protocol[_T_contra, _T_co]): + def __divmod__(self, __other: _T_contra) -> _T_co: + ... + + +class SupportsGetItem(Container[_KT_contra], Protocol[_KT_contra, _VT_co]): + def __getitem__(self, __k: _KT_contra) -> _VT_co: + ... + + +class SupportsNext(Protocol[_T_co]): + def __next__(self) -> _T_co: + ... + + +class SupportsRDivMod(Protocol[_T_contra, _T_co]): + def __rdivmod__(self, __other: _T_contra) -> _T_co: + ... + + +class SupportsTrunc(Protocol): + def __trunc__(self) -> int: + ... + + +# +# Mapping-like protocols +# + + +class SupportsKeys(Protocol[_KT_co]): + def keys(self) -> Iterable[_KT_co]: + ... + + +class SupportsItems(Protocol[_KT_co, _VT_co]): + def items(self) -> AbstractSet[Tuple[_KT_co, _VT_co]]: + ... + + +class SupportsKeysAndGetItem(Protocol[_KT, _VT_co]): + def keys(self) -> Iterable[_KT]: + ... + + def __getitem__(self, __k: _KT) -> _VT_co: + ... + + +# +# I/O protocols +# + + +class SupportsRead(Protocol[_T_co]): + def read(self, __length: int = ...) -> _T_co: + ... + + +class SupportsReadline(Protocol[_T_co]): + def readline(self, __length: int = ...) -> _T_co: + ... + + +class SupportsWrite(Protocol[_T_contra]): + def write(self, __s: _T_contra) -> object: + ... + + +# +# Path aliases +# + +StrPath: TypeAlias = Union[str, PathLike[str]] +BytesPath: TypeAlias = Union[bytes, PathLike[bytes]] From d09c0683b6aee33115f1a7bf11f60d711a232c33 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sat, 21 May 2022 19:07:03 +0200 Subject: [PATCH 3/7] Skip .venv directories for flake8 --- .flake8 | 1 + 1 file changed, 1 insertion(+) diff --git a/.flake8 b/.flake8 index a1f7fffd..28537367 100644 --- a/.flake8 +++ b/.flake8 @@ -10,6 +10,7 @@ ignore = # consistency with mypy W504 exclude = + .venv*, # tests have more relaxed formatting rules # and its own specific config in .flake8-tests src/test_typing_extensions, From 4f0977832c24847bdc9c7ecf833cafb52637bc61 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sat, 21 May 2022 19:23:54 +0200 Subject: [PATCH 4/7] Python <= 3.8 fix --- src/typing_extensions/ext.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/typing_extensions/ext.py b/src/typing_extensions/ext.py index 7141c42c..014acc53 100644 --- a/src/typing_extensions/ext.py +++ b/src/typing_extensions/ext.py @@ -95,5 +95,5 @@ def write(self, __s: _T_contra) -> object: # Path aliases # -StrPath: TypeAlias = Union[str, PathLike[str]] -BytesPath: TypeAlias = Union[bytes, PathLike[bytes]] +StrPath: TypeAlias = Union[str, "PathLike[str]"] +BytesPath: TypeAlias = Union[bytes, "PathLike[bytes]"] From ffb590821e27647aa830d1d07441d5ac75701fea Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sat, 21 May 2022 19:27:55 +0200 Subject: [PATCH 5/7] flake8 fixes --- src/test_typing_extensions/__init__.py | 3 ++- src/test_typing_extensions/ext.py | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/test_typing_extensions/__init__.py b/src/test_typing_extensions/__init__.py index 8c5704af..c284463d 100644 --- a/src/test_typing_extensions/__init__.py +++ b/src/test_typing_extensions/__init__.py @@ -28,7 +28,8 @@ from typing_extensions import assert_type, get_type_hints, get_origin, get_args from typing_extensions import clear_overloads, get_overloads, overload -from .ext import * +# Importing so that the tests are run. +from .ext import dummy # noqa F401 # Flags used to mark tests that only apply after a specific # version of the typing module. diff --git a/src/test_typing_extensions/ext.py b/src/test_typing_extensions/ext.py index b2fb7a89..d6025d93 100644 --- a/src/test_typing_extensions/ext.py +++ b/src/test_typing_extensions/ext.py @@ -1,2 +1,5 @@ # We just import the ext module to check for syntax or import problems. -from typing_extensions.ext import * +import typing_extensions.ext # noqa F401 + +def dummy(): + pass From 23c784b66319047999671e47660849f13cf832cb Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Fri, 27 May 2022 11:19:34 +0200 Subject: [PATCH 6/7] SupportsGetItem: Don't derive from Container --- src/typing_extensions/ext.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/typing_extensions/ext.py b/src/typing_extensions/ext.py index 014acc53..7239cd2b 100644 --- a/src/typing_extensions/ext.py +++ b/src/typing_extensions/ext.py @@ -3,7 +3,7 @@ """ from os import PathLike -from typing import AbstractSet, Awaitable, Container, Iterable, Tuple, TypeVar, Union +from typing import AbstractSet, Awaitable, Iterable, Tuple, TypeVar, Union from typing_extensions import Protocol, TypeAlias _T_co = TypeVar("_T_co", covariant=True) @@ -28,7 +28,7 @@ def __divmod__(self, __other: _T_contra) -> _T_co: ... -class SupportsGetItem(Container[_KT_contra], Protocol[_KT_contra, _VT_co]): +class SupportsGetItem(Protocol[_KT_contra, _VT_co]): def __getitem__(self, __k: _KT_contra) -> _VT_co: ... From 3c22f2a89484ecc0152143b09419279f6d7fd3b1 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Fri, 27 May 2022 11:29:16 +0200 Subject: [PATCH 7/7] Rename .exc to .utils --- CHANGELOG.md | 2 +- src/test_typing_extensions/__init__.py | 2 +- src/test_typing_extensions/ext.py | 5 ----- src/test_typing_extensions/utils.py | 5 +++++ src/typing_extensions/{ext.py => utils.py} | 0 5 files changed, 7 insertions(+), 7 deletions(-) delete mode 100644 src/test_typing_extensions/ext.py create mode 100644 src/test_typing_extensions/utils.py rename src/typing_extensions/{ext.py => utils.py} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 06044785..1eeedaff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ - Add `typing_extensions.NamedTuple`, allowing for generic `NamedTuple`s on Python <3.11 (backport from python/cpython#92027, by Serhiy Storchaka). Patch by Alex Waygood (@AlexWaygood). -- Add the `typing\_extensions.ext` module, which contains useful protocols +- Add the `typing\_extensions.utils` module, which contains useful protocols and type aliases that are not scheduled for inclusion in `typing`. - `SupportsAnext` - `SupportsDivMod` diff --git a/src/test_typing_extensions/__init__.py b/src/test_typing_extensions/__init__.py index 280df1b9..ab3e4165 100644 --- a/src/test_typing_extensions/__init__.py +++ b/src/test_typing_extensions/__init__.py @@ -31,7 +31,7 @@ from typing_extensions import NamedTuple # Importing so that the tests are run. -from .ext import dummy # noqa F401 +from .utils import dummy # noqa F401 # Flags used to mark tests that only apply after a specific # version of the typing module. diff --git a/src/test_typing_extensions/ext.py b/src/test_typing_extensions/ext.py deleted file mode 100644 index d6025d93..00000000 --- a/src/test_typing_extensions/ext.py +++ /dev/null @@ -1,5 +0,0 @@ -# We just import the ext module to check for syntax or import problems. -import typing_extensions.ext # noqa F401 - -def dummy(): - pass diff --git a/src/test_typing_extensions/utils.py b/src/test_typing_extensions/utils.py new file mode 100644 index 00000000..2dad338e --- /dev/null +++ b/src/test_typing_extensions/utils.py @@ -0,0 +1,5 @@ +# We just import the utils module to check for syntax or import problems. +import typing_extensions.utils # noqa F401 + +def dummy(): + pass diff --git a/src/typing_extensions/ext.py b/src/typing_extensions/utils.py similarity index 100% rename from src/typing_extensions/ext.py rename to src/typing_extensions/utils.py