From 20c9ee24d75e119b6de6ae74a7fb9e11b7bb8c55 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Tue, 8 Dec 2020 10:21:48 +0100 Subject: [PATCH 01/14] Add importlib.resources.files for Python 3.9 --- stdlib/3/importlib/resources.pyi | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/stdlib/3/importlib/resources.pyi b/stdlib/3/importlib/resources.pyi index be8d49ec3193..d91e7c5243b4 100644 --- a/stdlib/3/importlib/resources.pyi +++ b/stdlib/3/importlib/resources.pyi @@ -16,3 +16,8 @@ if sys.version_info >= (3, 7): def path(package: Package, resource: Resource) -> ContextManager[Path]: ... def is_resource(package: Package, name: str) -> bool: ... def contents(package: Package) -> Iterator[str]: ... + +if sys.version_info >= (3, 9): + from .abc import Traversable + + def files(package: Package) -> Traversable: ... From a2567d3eada0a69972b9b56560f6a6877a2784f6 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Tue, 8 Dec 2020 10:23:52 +0100 Subject: [PATCH 02/14] Update resources.pyi --- stdlib/3/importlib/resources.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/3/importlib/resources.pyi b/stdlib/3/importlib/resources.pyi index d91e7c5243b4..f823b2952a79 100644 --- a/stdlib/3/importlib/resources.pyi +++ b/stdlib/3/importlib/resources.pyi @@ -19,5 +19,5 @@ if sys.version_info >= (3, 7): if sys.version_info >= (3, 9): from .abc import Traversable - + def files(package: Package) -> Traversable: ... From 15725854d7641a24ff3fee82801e7fb9a3711947 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Tue, 8 Dec 2020 10:37:58 +0100 Subject: [PATCH 03/14] Update resources.pyi --- stdlib/3/importlib/resources.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/3/importlib/resources.pyi b/stdlib/3/importlib/resources.pyi index f823b2952a79..ab630d0ca800 100644 --- a/stdlib/3/importlib/resources.pyi +++ b/stdlib/3/importlib/resources.pyi @@ -18,6 +18,6 @@ if sys.version_info >= (3, 7): def contents(package: Package) -> Iterator[str]: ... if sys.version_info >= (3, 9): - from .abc import Traversable + from ..abc import Traversable def files(package: Package) -> Traversable: ... From ce6b65c5b106d15d36f166364914b04b944d6260 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Tue, 8 Dec 2020 10:39:50 +0100 Subject: [PATCH 04/14] Update resources.pyi --- stdlib/3/importlib/resources.pyi | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/3/importlib/resources.pyi b/stdlib/3/importlib/resources.pyi index ab630d0ca800..42b2e1edaa86 100644 --- a/stdlib/3/importlib/resources.pyi +++ b/stdlib/3/importlib/resources.pyi @@ -19,5 +19,4 @@ if sys.version_info >= (3, 7): if sys.version_info >= (3, 9): from ..abc import Traversable - def files(package: Package) -> Traversable: ... From d003cb77fd39d45258beef6b426b436e7ec9908f Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Tue, 8 Dec 2020 10:51:13 +0100 Subject: [PATCH 05/14] Update resources.pyi --- stdlib/3/importlib/resources.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/3/importlib/resources.pyi b/stdlib/3/importlib/resources.pyi index 42b2e1edaa86..d4b1ea1a1740 100644 --- a/stdlib/3/importlib/resources.pyi +++ b/stdlib/3/importlib/resources.pyi @@ -18,5 +18,5 @@ if sys.version_info >= (3, 7): def contents(package: Package) -> Iterator[str]: ... if sys.version_info >= (3, 9): - from ..abc import Traversable + from ..abc import Traversable as Traversable def files(package: Package) -> Traversable: ... From f56462439b1cccbc2082c87fec4b972c1e3ef366 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Tue, 8 Dec 2020 10:54:07 +0100 Subject: [PATCH 06/14] Update resources.pyi --- stdlib/3/importlib/resources.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/3/importlib/resources.pyi b/stdlib/3/importlib/resources.pyi index d4b1ea1a1740..65beccba8cfd 100644 --- a/stdlib/3/importlib/resources.pyi +++ b/stdlib/3/importlib/resources.pyi @@ -18,5 +18,5 @@ if sys.version_info >= (3, 7): def contents(package: Package) -> Iterator[str]: ... if sys.version_info >= (3, 9): - from ..abc import Traversable as Traversable + from importlib.abc import Traversable def files(package: Package) -> Traversable: ... From 3e87cb2b4b597540e7e90b7062df8d10b52c6dca Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Tue, 8 Dec 2020 17:29:58 +0100 Subject: [PATCH 07/14] Try to add Traversable to importlib.abc --- stdlib/3/importlib/abc.pyi | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/stdlib/3/importlib/abc.pyi b/stdlib/3/importlib/abc.pyi index 185b8a629bb1..47094dc11c28 100644 --- a/stdlib/3/importlib/abc.pyi +++ b/stdlib/3/importlib/abc.pyi @@ -70,3 +70,34 @@ if sys.version_info >= (3, 7): def is_resource(self, name: str) -> bool: ... @abstractmethod def contents(self) -> Iterator[str]: ... + +if sys.version_info >= (3, 9): + from typing import Protocol, runtime_checkable + @runtime_checkable + class Traversable(Protocol): + @abc.abstractmethod + def iterdir(self) -> Iterator[Traversable]: ... + + @abc.abstractmethod + def read_bytes(self) -> bytes: ... + + @abc.abstractmethod + def read_text(self, encoding: Optional[str] = None) -> str: ... + + @abc.abstractmethod + def is_dir(self) -> bool: ... + + @abc.abstractmethod + def is_file(self) -> bool: ... + + @abc.abstractmethod + def joinpath(self, child: Traversable) -> Traversable: ... + + @abc.abstractmethod + def __truediv__(self, child: Traversable) -> Traversable: ... + + @abc.abstractmethod + def open(self, mode: str = 'r', *args, **kwargs) -> IO: ... + + @abc.abstractproperty + def name(self) -> str: ... From dc66c34f233e281620ca3b8a5267cada76b176c6 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Wed, 9 Dec 2020 06:42:39 +0100 Subject: [PATCH 08/14] Update abc.pyi --- stdlib/3/importlib/abc.pyi | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/stdlib/3/importlib/abc.pyi b/stdlib/3/importlib/abc.pyi index 47094dc11c28..6a3842894a4e 100644 --- a/stdlib/3/importlib/abc.pyi +++ b/stdlib/3/importlib/abc.pyi @@ -75,28 +75,28 @@ if sys.version_info >= (3, 9): from typing import Protocol, runtime_checkable @runtime_checkable class Traversable(Protocol): - @abc.abstractmethod + @abstractmethod def iterdir(self) -> Iterator[Traversable]: ... - @abc.abstractmethod + @abstractmethod def read_bytes(self) -> bytes: ... - @abc.abstractmethod + @abstractmethod def read_text(self, encoding: Optional[str] = None) -> str: ... - @abc.abstractmethod + @abstractmethod def is_dir(self) -> bool: ... - @abc.abstractmethod + @abstractmethod def is_file(self) -> bool: ... - @abc.abstractmethod + @abstractmethod def joinpath(self, child: Traversable) -> Traversable: ... - @abc.abstractmethod + @abstractmethod def __truediv__(self, child: Traversable) -> Traversable: ... - @abc.abstractmethod + @abstractmethod def open(self, mode: str = 'r', *args, **kwargs) -> IO: ... @abc.abstractproperty From bdd95268ea45d44c4b8d3cb4308f768c00fe5ba2 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Wed, 9 Dec 2020 06:46:40 +0100 Subject: [PATCH 09/14] Update abc.pyi --- stdlib/3/importlib/abc.pyi | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/stdlib/3/importlib/abc.pyi b/stdlib/3/importlib/abc.pyi index 6a3842894a4e..50564b147b3f 100644 --- a/stdlib/3/importlib/abc.pyi +++ b/stdlib/3/importlib/abc.pyi @@ -1,7 +1,7 @@ import os import sys import types -from abc import ABCMeta, abstractmethod +from abc import ABCMeta, abstractmethod, abstractproperty from typing import IO, Any, Iterator, Mapping, Optional, Sequence, Tuple, Union # Loader is exported from this module, but for circular import reasons @@ -70,7 +70,7 @@ if sys.version_info >= (3, 7): def is_resource(self, name: str) -> bool: ... @abstractmethod def contents(self) -> Iterator[str]: ... - + if sys.version_info >= (3, 9): from typing import Protocol, runtime_checkable @runtime_checkable @@ -82,7 +82,7 @@ if sys.version_info >= (3, 9): def read_bytes(self) -> bytes: ... @abstractmethod - def read_text(self, encoding: Optional[str] = None) -> str: ... + def read_text(self, encoding: Optional[str] = ...) -> str: ... @abstractmethod def is_dir(self) -> bool: ... @@ -97,7 +97,7 @@ if sys.version_info >= (3, 9): def __truediv__(self, child: Traversable) -> Traversable: ... @abstractmethod - def open(self, mode: str = 'r', *args, **kwargs) -> IO: ... + def open(self, mode: str = ..., *args, **kwargs) -> IO: ... - @abc.abstractproperty + @abstractproperty def name(self) -> str: ... From b67178ad0f98582ef19cdcdef47dc58c972445db Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Wed, 9 Dec 2020 06:55:20 +0100 Subject: [PATCH 10/14] Artisanal, hand-blacking --- stdlib/3/importlib/abc.pyi | 8 -------- 1 file changed, 8 deletions(-) diff --git a/stdlib/3/importlib/abc.pyi b/stdlib/3/importlib/abc.pyi index 50564b147b3f..8e99a284459e 100644 --- a/stdlib/3/importlib/abc.pyi +++ b/stdlib/3/importlib/abc.pyi @@ -77,27 +77,19 @@ if sys.version_info >= (3, 9): class Traversable(Protocol): @abstractmethod def iterdir(self) -> Iterator[Traversable]: ... - @abstractmethod def read_bytes(self) -> bytes: ... - @abstractmethod def read_text(self, encoding: Optional[str] = ...) -> str: ... - @abstractmethod def is_dir(self) -> bool: ... - @abstractmethod def is_file(self) -> bool: ... - @abstractmethod def joinpath(self, child: Traversable) -> Traversable: ... - @abstractmethod def __truediv__(self, child: Traversable) -> Traversable: ... - @abstractmethod def open(self, mode: str = ..., *args, **kwargs) -> IO: ... - @abstractproperty def name(self) -> str: ... From 7fc6c8157ee8b0ed1087163f303746ae6d4df784 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Tue, 8 Dec 2020 22:33:14 -0800 Subject: [PATCH 11/14] Use property + abstractmethod to appease pytype abstractproperty has been deprecated for forever too --- stdlib/3/importlib/abc.pyi | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stdlib/3/importlib/abc.pyi b/stdlib/3/importlib/abc.pyi index 8e99a284459e..72ae9e47a652 100644 --- a/stdlib/3/importlib/abc.pyi +++ b/stdlib/3/importlib/abc.pyi @@ -1,7 +1,7 @@ import os import sys import types -from abc import ABCMeta, abstractmethod, abstractproperty +from abc import ABCMeta, abstractmethod from typing import IO, Any, Iterator, Mapping, Optional, Sequence, Tuple, Union # Loader is exported from this module, but for circular import reasons @@ -91,5 +91,6 @@ if sys.version_info >= (3, 9): def __truediv__(self, child: Traversable) -> Traversable: ... @abstractmethod def open(self, mode: str = ..., *args, **kwargs) -> IO: ... - @abstractproperty + @property + @abstractmethod def name(self) -> str: ... From 3bcd7b76a37c4c8883734e92cf74a8afdd8275e9 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Tue, 8 Dec 2020 22:36:43 -0800 Subject: [PATCH 12/14] allowlist a stubtest complaint --- tests/stubtest_whitelists/py39.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/stubtest_whitelists/py39.txt b/tests/stubtest_whitelists/py39.txt index 527c69f3502c..0c97dcd81f57 100644 --- a/tests/stubtest_whitelists/py39.txt +++ b/tests/stubtest_whitelists/py39.txt @@ -51,6 +51,7 @@ hmac.new # Stub is a white lie; see comments in the stub http.client.HTTPSConnection.__init__ http.cookiejar.DefaultCookiePolicy.__init__ http.server.SimpleHTTPRequestHandler.__init__ +importlib.abc.Traversable.__init__ # Inherits __init__ from typing.Protocol importlib.metadata.DistributionFinder.Context.pattern importlib.metadata.EntryPointBase ipaddress.IPv4Interface.hostmask From 66e7d2474dc1364a193e13a538c4ec9141488ca2 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Wed, 9 Dec 2020 11:59:59 +0100 Subject: [PATCH 13/14] Update abc.pyi --- stdlib/3/importlib/abc.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/3/importlib/abc.pyi b/stdlib/3/importlib/abc.pyi index 72ae9e47a652..ae700dd6ca0e 100644 --- a/stdlib/3/importlib/abc.pyi +++ b/stdlib/3/importlib/abc.pyi @@ -72,7 +72,7 @@ if sys.version_info >= (3, 7): def contents(self) -> Iterator[str]: ... if sys.version_info >= (3, 9): - from typing import Protocol, runtime_checkable + from typing import Literal, Protocol, runtime_checkable @runtime_checkable class Traversable(Protocol): @abstractmethod @@ -90,7 +90,7 @@ if sys.version_info >= (3, 9): @abstractmethod def __truediv__(self, child: Traversable) -> Traversable: ... @abstractmethod - def open(self, mode: str = ..., *args, **kwargs) -> IO: ... + def open(self, mode: Literal["r", "rb"] = ..., *args: Any, **kwargs: Any) -> IO: ... @property @abstractmethod def name(self) -> str: ... From 02dfcc69913bb0cb1bb689ae05c2180f4de6a161 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 9 Dec 2020 13:05:32 +0100 Subject: [PATCH 14/14] Work around a pytype crash --- stdlib/3/importlib/abc.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/3/importlib/abc.pyi b/stdlib/3/importlib/abc.pyi index ae700dd6ca0e..507d3a596aca 100644 --- a/stdlib/3/importlib/abc.pyi +++ b/stdlib/3/importlib/abc.pyi @@ -3,6 +3,7 @@ import sys import types from abc import ABCMeta, abstractmethod from typing import IO, Any, Iterator, Mapping, Optional, Sequence, Tuple, Union +from typing_extensions import Literal # Loader is exported from this module, but for circular import reasons # exists in its own stub file (with ModuleSpec and ModuleType). @@ -72,7 +73,7 @@ if sys.version_info >= (3, 7): def contents(self) -> Iterator[str]: ... if sys.version_info >= (3, 9): - from typing import Literal, Protocol, runtime_checkable + from typing import Protocol, runtime_checkable @runtime_checkable class Traversable(Protocol): @abstractmethod