From cb43535541db6f2105c89c2886bf52c8f7fed162 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Wed, 9 Dec 2020 13:47:45 +0100 Subject: [PATCH] Add importlib.resources.files for Python 3.9 (#4807) Co-authored-by: hauntsaninja <> Co-authored-by: Sebastian Rittau --- stdlib/3/importlib/abc.pyi | 25 +++++++++++++++++++++++++ stdlib/3/importlib/resources.pyi | 4 ++++ tests/stubtest_whitelists/py39.txt | 1 + 3 files changed, 30 insertions(+) diff --git a/stdlib/3/importlib/abc.pyi b/stdlib/3/importlib/abc.pyi index 185b8a629..507d3a596 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). @@ -70,3 +71,27 @@ 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): + @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: Literal["r", "rb"] = ..., *args: Any, **kwargs: Any) -> IO: ... + @property + @abstractmethod + def name(self) -> str: ... diff --git a/stdlib/3/importlib/resources.pyi b/stdlib/3/importlib/resources.pyi index be8d49ec3..65beccba8 100644 --- a/stdlib/3/importlib/resources.pyi +++ b/stdlib/3/importlib/resources.pyi @@ -16,3 +16,7 @@ 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 importlib.abc import Traversable + def files(package: Package) -> Traversable: ... diff --git a/tests/stubtest_whitelists/py39.txt b/tests/stubtest_whitelists/py39.txt index 527c69f35..0c97dcd81 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