From f46cf7b8f662975d683292490eeb69011a67df7a Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 27 Mar 2018 21:30:18 -0700 Subject: [PATCH] add stub for importlib.resources Part of #1965 --- stdlib/3.7/importlib/resources.pyi | 22 ++++++++++++++++++++++ stdlib/3/importlib/abc.pyi | 16 +++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 stdlib/3.7/importlib/resources.pyi diff --git a/stdlib/3.7/importlib/resources.pyi b/stdlib/3.7/importlib/resources.pyi new file mode 100644 index 000000000000..57dc7b9f24c6 --- /dev/null +++ b/stdlib/3.7/importlib/resources.pyi @@ -0,0 +1,22 @@ +import os + +from pathlib import Path +from types import ModuleType +from typing import ContextManager, Iterator, Union, BinaryIO, TextIO + +Package = Union[str, ModuleType] +Resource = Union[str, os.PathLike] + +def open_binary(package: Package, resource: Resource) -> BinaryIO: ... +def open_text(package: Package, + resource: Resource, + encoding: str = ..., + errors: str = ...) -> TextIO: ... +def read_binary(package: Package, resource: Resource) -> bytes: ... +def read_text(package: Package, + resource: Resource, + encoding: str = ..., + errors: str = ...) -> str: ... +def path(package: Package, resource: Resource) -> ContextManager[Path]: ... +def is_resource(package: Package, name: str) -> bool: ... +def contents(package: Package) -> Iterator[str]: ... diff --git a/stdlib/3/importlib/abc.pyi b/stdlib/3/importlib/abc.pyi index 7c49eded9aaf..0e13fa2c1bec 100644 --- a/stdlib/3/importlib/abc.pyi +++ b/stdlib/3/importlib/abc.pyi @@ -1,7 +1,8 @@ from abc import ABCMeta, abstractmethod +import os import sys import types -from typing import Any, Mapping, Optional, Sequence, Tuple, Union +from typing import Any, IO, Iterator, Mapping, Optional, Sequence, Tuple, Union # Loader is exported from this module, but for circular import reasons # exists in its own stub file (with ModuleSpec and ModuleType). @@ -87,3 +88,16 @@ if sys.version_info >= (3, 3): def __init__(self, fullname: str, path: _Path) -> None: ... def get_data(self, path: _Path) -> bytes: ... def get_filename(self, fullname: str) -> _Path: ... + +if sys.version_info >= (3, 7): + _PathLike = Union[bytes, str, os.PathLike[Any]] + + class ResourceReader(metaclass=ABCMeta): + @abstractmethod + def open_resource(self, resource: _PathLike) -> IO[bytes]: ... + @abstractmethod + def resource_path(self, resource: _PathLike) -> str: ... + @abstractmethod + def is_resource(self, name: str) -> bool: ... + @abstractmethod + def contents(self) -> Iterator[str]: ...