From c4b37291d185bad462d164e1e4735dd41b455e98 Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Thu, 1 Apr 2021 23:48:15 +0300 Subject: [PATCH 1/9] Change compileall rx variable to accept a protocol --- stdlib/compileall.pyi | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/stdlib/compileall.pyi b/stdlib/compileall.pyi index bcc2a8e8275d..83303c478d0a 100644 --- a/stdlib/compileall.pyi +++ b/stdlib/compileall.pyi @@ -1,17 +1,23 @@ import sys from _typeshed import AnyPath -from typing import Any, Optional, Pattern +from typing import Any, AnyStr, Optional, Pattern, TypeVar +from typing_extensions import Protocol if sys.version_info >= (3, 7): from py_compile import PycInvalidationMode +_AnyStr_contra = TypeVar("_AnyStr_contra", str, bytes, contravariant=True) + +class _SupportsSearch(Protocol[_AnyStr_contra]): + def search(self, string: _AnyStr_contra) -> Any: ... + if sys.version_info >= (3, 9): def compile_dir( dir: AnyPath, maxlevels: Optional[int] = ..., ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[Pattern[Any]] = ..., + rx: Optional[_SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -27,7 +33,7 @@ if sys.version_info >= (3, 9): fullname: AnyPath, ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[Pattern[Any]] = ..., + rx: Optional[_SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -45,7 +51,7 @@ elif sys.version_info >= (3, 7): maxlevels: int = ..., ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[Pattern[Any]] = ..., + rx: Optional[_SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -56,7 +62,7 @@ elif sys.version_info >= (3, 7): fullname: AnyPath, ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[Pattern[Any]] = ..., + rx: Optional[_SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -64,13 +70,12 @@ elif sys.version_info >= (3, 7): ) -> int: ... else: - # rx can be any object with a 'search' method; once we have Protocols we can change the type def compile_dir( dir: AnyPath, maxlevels: int = ..., ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[Pattern[Any]] = ..., + rx: Optional[_SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -80,7 +85,7 @@ else: fullname: AnyPath, ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[Pattern[Any]] = ..., + rx: Optional[_SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., From a164ab4caa4459f63d796effbf994ce29e55851d Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Fri, 2 Apr 2021 00:02:41 +0300 Subject: [PATCH 2/9] Removing unused import --- stdlib/compileall.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/compileall.pyi b/stdlib/compileall.pyi index 83303c478d0a..3faba27ee7e4 100644 --- a/stdlib/compileall.pyi +++ b/stdlib/compileall.pyi @@ -1,6 +1,6 @@ import sys from _typeshed import AnyPath -from typing import Any, AnyStr, Optional, Pattern, TypeVar +from typing import Any, AnyStr, Optional, TypeVar from typing_extensions import Protocol if sys.version_info >= (3, 7): From 3ce864ba826bb655e7a3845992fad95cb9a758ed Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Fri, 2 Apr 2021 00:18:18 +0300 Subject: [PATCH 3/9] Trying to use typeshed typevar --- stdlib/compileall.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/compileall.pyi b/stdlib/compileall.pyi index 3faba27ee7e4..ffd570e90595 100644 --- a/stdlib/compileall.pyi +++ b/stdlib/compileall.pyi @@ -1,15 +1,15 @@ import sys -from _typeshed import AnyPath +from _typeshed import AnyPath, _T_contra from typing import Any, AnyStr, Optional, TypeVar from typing_extensions import Protocol if sys.version_info >= (3, 7): from py_compile import PycInvalidationMode -_AnyStr_contra = TypeVar("_AnyStr_contra", str, bytes, contravariant=True) +#_AnyStr_contra = TypeVar("_AnyStr_contra", str, bytes, contravariant=True) -class _SupportsSearch(Protocol[_AnyStr_contra]): - def search(self, string: _AnyStr_contra) -> Any: ... +class _SupportsSearch(Protocol[_T_contra]): + def search(self, string: _T_contra) -> Any: ... if sys.version_info >= (3, 9): def compile_dir( From 71be86d7edb25ccde26d4cb106aa37abd0277a1d Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Fri, 2 Apr 2021 00:23:50 +0300 Subject: [PATCH 4/9] Trying to extract SupportsSearch out of file --- stdlib/_typeshed/__init__.pyi | 3 +++ stdlib/compileall.pyi | 21 ++++++++------------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 5be4f1ee12e9..11aee06ad1b2 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -57,6 +57,9 @@ class SupportsItemAccess(SupportsGetItem[_KT_contra, _VT], Protocol[_KT_contra, def __setitem__(self, __k: _KT_contra, __v: _VT) -> None: ... def __delitem__(self, __v: _KT_contra) -> None: ... +class SupportsSearch(Protocol[_T_contra]): + def search(self, string: _T_contra) -> Any: ... + # StrPath and AnyPath can be used in places where a # path can be used instead of a string, starting with Python 3.6. if sys.version_info >= (3, 6): diff --git a/stdlib/compileall.pyi b/stdlib/compileall.pyi index ffd570e90595..327f2438d230 100644 --- a/stdlib/compileall.pyi +++ b/stdlib/compileall.pyi @@ -1,23 +1,18 @@ import sys -from _typeshed import AnyPath, _T_contra -from typing import Any, AnyStr, Optional, TypeVar +from _typeshed import AnyPath, SupportsSearch +from typing import Any, AnyStr, Optional from typing_extensions import Protocol if sys.version_info >= (3, 7): from py_compile import PycInvalidationMode -#_AnyStr_contra = TypeVar("_AnyStr_contra", str, bytes, contravariant=True) - -class _SupportsSearch(Protocol[_T_contra]): - def search(self, string: _T_contra) -> Any: ... - if sys.version_info >= (3, 9): def compile_dir( dir: AnyPath, maxlevels: Optional[int] = ..., ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[_SupportsSearch[AnyStr]] = ..., + rx: Optional[SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -33,7 +28,7 @@ if sys.version_info >= (3, 9): fullname: AnyPath, ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[_SupportsSearch[AnyStr]] = ..., + rx: Optional[SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -51,7 +46,7 @@ elif sys.version_info >= (3, 7): maxlevels: int = ..., ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[_SupportsSearch[AnyStr]] = ..., + rx: Optional[SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -62,7 +57,7 @@ elif sys.version_info >= (3, 7): fullname: AnyPath, ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[_SupportsSearch[AnyStr]] = ..., + rx: Optional[SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -75,7 +70,7 @@ else: maxlevels: int = ..., ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[_SupportsSearch[AnyStr]] = ..., + rx: Optional[SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -85,7 +80,7 @@ else: fullname: AnyPath, ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[_SupportsSearch[AnyStr]] = ..., + rx: Optional[SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., From b86eef9d0ded391a11f4e144e968388482f6dbb7 Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Fri, 2 Apr 2021 00:37:50 +0300 Subject: [PATCH 5/9] Remove unused import --- stdlib/compileall.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/compileall.pyi b/stdlib/compileall.pyi index 327f2438d230..00a0b0a336ce 100644 --- a/stdlib/compileall.pyi +++ b/stdlib/compileall.pyi @@ -1,6 +1,6 @@ import sys from _typeshed import AnyPath, SupportsSearch -from typing import Any, AnyStr, Optional +from typing import AnyStr, Optional from typing_extensions import Protocol if sys.version_info >= (3, 7): From 1b0748b655fe4861c70af20383211666ce8f349b Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Fri, 2 Apr 2021 02:41:26 +0300 Subject: [PATCH 6/9] Remove another unused import --- stdlib/compileall.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/compileall.pyi b/stdlib/compileall.pyi index 00a0b0a336ce..333d1d417132 100644 --- a/stdlib/compileall.pyi +++ b/stdlib/compileall.pyi @@ -1,7 +1,6 @@ import sys from _typeshed import AnyPath, SupportsSearch from typing import AnyStr, Optional -from typing_extensions import Protocol if sys.version_info >= (3, 7): from py_compile import PycInvalidationMode From b38f5f4320043cad9c8561d1d46ba49b2711befd Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Fri, 2 Apr 2021 17:07:44 +0300 Subject: [PATCH 7/9] Move SupportsSearch back to compileall with Protocol imported from typing --- stdlib/_typeshed/__init__.pyi | 3 --- stdlib/compileall.pyi | 19 +++++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 11aee06ad1b2..5be4f1ee12e9 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -57,9 +57,6 @@ class SupportsItemAccess(SupportsGetItem[_KT_contra, _VT], Protocol[_KT_contra, def __setitem__(self, __k: _KT_contra, __v: _VT) -> None: ... def __delitem__(self, __v: _KT_contra) -> None: ... -class SupportsSearch(Protocol[_T_contra]): - def search(self, string: _T_contra) -> Any: ... - # StrPath and AnyPath can be used in places where a # path can be used instead of a string, starting with Python 3.6. if sys.version_info >= (3, 6): diff --git a/stdlib/compileall.pyi b/stdlib/compileall.pyi index 333d1d417132..630c094feb03 100644 --- a/stdlib/compileall.pyi +++ b/stdlib/compileall.pyi @@ -1,17 +1,20 @@ import sys -from _typeshed import AnyPath, SupportsSearch -from typing import AnyStr, Optional +from _typeshed import AnyPath, _T_contra +from typing import Any, AnyStr, Generic, Optional, Protocol if sys.version_info >= (3, 7): from py_compile import PycInvalidationMode +class _SupportsSearch(Protocol[_T_contra]): + def search(self, string: _T_contra) -> Any: ... + if sys.version_info >= (3, 9): def compile_dir( dir: AnyPath, maxlevels: Optional[int] = ..., ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[SupportsSearch[AnyStr]] = ..., + rx: Optional[_SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -27,7 +30,7 @@ if sys.version_info >= (3, 9): fullname: AnyPath, ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[SupportsSearch[AnyStr]] = ..., + rx: Optional[_SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -45,7 +48,7 @@ elif sys.version_info >= (3, 7): maxlevels: int = ..., ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[SupportsSearch[AnyStr]] = ..., + rx: Optional[_SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -56,7 +59,7 @@ elif sys.version_info >= (3, 7): fullname: AnyPath, ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[SupportsSearch[AnyStr]] = ..., + rx: Optional[_SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -69,7 +72,7 @@ else: maxlevels: int = ..., ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[SupportsSearch[AnyStr]] = ..., + rx: Optional[_SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -79,7 +82,7 @@ else: fullname: AnyPath, ddir: Optional[AnyPath] = ..., force: bool = ..., - rx: Optional[SupportsSearch[AnyStr]] = ..., + rx: Optional[_SupportsSearch[AnyStr]] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., From 04f2320159e058683e586fef75bb7291e8aca924 Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Fri, 2 Apr 2021 17:14:42 +0300 Subject: [PATCH 8/9] Remove unused import --- stdlib/compileall.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/compileall.pyi b/stdlib/compileall.pyi index 630c094feb03..12a989107fec 100644 --- a/stdlib/compileall.pyi +++ b/stdlib/compileall.pyi @@ -1,6 +1,6 @@ import sys from _typeshed import AnyPath, _T_contra -from typing import Any, AnyStr, Generic, Optional, Protocol +from typing import Any, AnyStr, Optional, Protocol if sys.version_info >= (3, 7): from py_compile import PycInvalidationMode From c57e3c49edab9067df0c042610aaab3f83067b4e Mon Sep 17 00:00:00 2001 From: Tal Hayon Date: Sat, 10 Apr 2021 06:45:37 +0300 Subject: [PATCH 9/9] Change AnyPath to StrPath --- stdlib/compileall.pyi | 56 +++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/stdlib/compileall.pyi b/stdlib/compileall.pyi index 12a989107fec..529568275c8c 100644 --- a/stdlib/compileall.pyi +++ b/stdlib/compileall.pyi @@ -1,54 +1,54 @@ import sys -from _typeshed import AnyPath, _T_contra -from typing import Any, AnyStr, Optional, Protocol +from _typeshed import StrPath +from typing import Any, Optional, Protocol if sys.version_info >= (3, 7): from py_compile import PycInvalidationMode -class _SupportsSearch(Protocol[_T_contra]): - def search(self, string: _T_contra) -> Any: ... +class _SupportsSearch(Protocol): + def search(self, string: str) -> Any: ... if sys.version_info >= (3, 9): def compile_dir( - dir: AnyPath, + dir: StrPath, maxlevels: Optional[int] = ..., - ddir: Optional[AnyPath] = ..., + ddir: Optional[StrPath] = ..., force: bool = ..., - rx: Optional[_SupportsSearch[AnyStr]] = ..., + rx: Optional[_SupportsSearch] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., workers: int = ..., invalidation_mode: Optional[PycInvalidationMode] = ..., *, - stripdir: Optional[str] = ..., # TODO: change to Optional[AnyPath] once https://bugs.python.org/issue40447 is resolved - prependdir: Optional[AnyPath] = ..., - limit_sl_dest: Optional[AnyPath] = ..., + stripdir: Optional[str] = ..., # TODO: change to Optional[StrPath] once https://bugs.python.org/issue40447 is resolved + prependdir: Optional[StrPath] = ..., + limit_sl_dest: Optional[StrPath] = ..., hardlink_dupes: bool = ..., ) -> int: ... def compile_file( - fullname: AnyPath, - ddir: Optional[AnyPath] = ..., + fullname: StrPath, + ddir: Optional[StrPath] = ..., force: bool = ..., - rx: Optional[_SupportsSearch[AnyStr]] = ..., + rx: Optional[_SupportsSearch] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., invalidation_mode: Optional[PycInvalidationMode] = ..., *, - stripdir: Optional[str] = ..., # TODO: change to Optional[AnyPath] once https://bugs.python.org/issue40447 is resolved - prependdir: Optional[AnyPath] = ..., - limit_sl_dest: Optional[AnyPath] = ..., + stripdir: Optional[str] = ..., # TODO: change to Optional[StrPath] once https://bugs.python.org/issue40447 is resolved + prependdir: Optional[StrPath] = ..., + limit_sl_dest: Optional[StrPath] = ..., hardlink_dupes: bool = ..., ) -> int: ... elif sys.version_info >= (3, 7): def compile_dir( - dir: AnyPath, + dir: StrPath, maxlevels: int = ..., - ddir: Optional[AnyPath] = ..., + ddir: Optional[StrPath] = ..., force: bool = ..., - rx: Optional[_SupportsSearch[AnyStr]] = ..., + rx: Optional[_SupportsSearch] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -56,10 +56,10 @@ elif sys.version_info >= (3, 7): invalidation_mode: Optional[PycInvalidationMode] = ..., ) -> int: ... def compile_file( - fullname: AnyPath, - ddir: Optional[AnyPath] = ..., + fullname: StrPath, + ddir: Optional[StrPath] = ..., force: bool = ..., - rx: Optional[_SupportsSearch[AnyStr]] = ..., + rx: Optional[_SupportsSearch] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., @@ -68,21 +68,21 @@ elif sys.version_info >= (3, 7): else: def compile_dir( - dir: AnyPath, + dir: StrPath, maxlevels: int = ..., - ddir: Optional[AnyPath] = ..., + ddir: Optional[StrPath] = ..., force: bool = ..., - rx: Optional[_SupportsSearch[AnyStr]] = ..., + rx: Optional[_SupportsSearch] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ..., workers: int = ..., ) -> int: ... def compile_file( - fullname: AnyPath, - ddir: Optional[AnyPath] = ..., + fullname: StrPath, + ddir: Optional[StrPath] = ..., force: bool = ..., - rx: Optional[_SupportsSearch[AnyStr]] = ..., + rx: Optional[_SupportsSearch] = ..., quiet: int = ..., legacy: bool = ..., optimize: int = ...,