From 40223373db4dff864e5454906e860b062f714868 Mon Sep 17 00:00:00 2001 From: layday Date: Tue, 27 Sep 2022 22:31:33 +0300 Subject: [PATCH] Update `importlib.resources` types (#8658) * Replace `Str[OrBytes]Path` with `str` A filesystem path is not valid as an abstract resource. `TraversableResources` cannot only accept strings if `ResourceReader` accepts both bytes and strings. `importlib.resources` does not work with bytes in any case and `ResourceReader` is typed as taking a `typing.Text` object in `importlib.resources`' source code. * Update `joinpath` signature from Python 3.11 --- stdlib/importlib/abc.pyi | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/stdlib/importlib/abc.pyi b/stdlib/importlib/abc.pyi index d3eb761ba..708037305 100644 --- a/stdlib/importlib/abc.pyi +++ b/stdlib/importlib/abc.pyi @@ -1,14 +1,6 @@ import sys import types -from _typeshed import ( - OpenBinaryMode, - OpenBinaryModeReading, - OpenBinaryModeUpdating, - OpenBinaryModeWriting, - OpenTextMode, - StrOrBytesPath, - StrPath, -) +from _typeshed import OpenBinaryMode, OpenBinaryModeReading, OpenBinaryModeUpdating, OpenBinaryModeWriting, OpenTextMode from abc import ABCMeta, abstractmethod from collections.abc import Iterator, Mapping, Sequence from importlib.machinery import ModuleSpec @@ -93,9 +85,9 @@ class FileLoader(ResourceLoader, ExecutionLoader, metaclass=ABCMeta): class ResourceReader(metaclass=ABCMeta): @abstractmethod - def open_resource(self, resource: StrOrBytesPath) -> IO[bytes]: ... + def open_resource(self, resource: str) -> IO[bytes]: ... @abstractmethod - def resource_path(self, resource: StrOrBytesPath) -> str: ... + def resource_path(self, resource: str) -> str: ... if sys.version_info >= (3, 10): @abstractmethod def is_resource(self, path: str) -> bool: ... @@ -115,8 +107,12 @@ if sys.version_info >= (3, 9): def is_file(self) -> bool: ... @abstractmethod def iterdir(self) -> Iterator[Traversable]: ... - @abstractmethod - def joinpath(self, child: StrPath) -> Traversable: ... + if sys.version_info >= (3, 11): + @abstractmethod + def joinpath(self, *descendants: str) -> Traversable: ... + else: + @abstractmethod + def joinpath(self, child: str) -> Traversable: ... # The .open method comes from pathlib.pyi and should be kept in sync. @overload @abstractmethod @@ -180,7 +176,7 @@ if sys.version_info >= (3, 9): @property def name(self) -> str: ... @abstractmethod - def __truediv__(self, child: StrPath) -> Traversable: ... + def __truediv__(self, child: str) -> Traversable: ... @abstractmethod def read_bytes(self) -> bytes: ... @abstractmethod @@ -189,7 +185,7 @@ if sys.version_info >= (3, 9): class TraversableResources(ResourceReader): @abstractmethod def files(self) -> Traversable: ... - def open_resource(self, resource: StrPath) -> BufferedReader: ... # type: ignore[override] + def open_resource(self, resource: str) -> BufferedReader: ... # type: ignore[override] def resource_path(self, resource: Any) -> NoReturn: ... - def is_resource(self, path: StrPath) -> bool: ... + def is_resource(self, path: str) -> bool: ... def contents(self) -> Iterator[str]: ...