Skip to content

Commit

Permalink
Merge pull request #244 from henryiii/henryiii/types/fix1
Browse files Browse the repository at this point in the history
fix(types): Adding some typing from typeshed
  • Loading branch information
jaraco committed Mar 26, 2022
2 parents 0526c20 + 2c4a1bd commit 5b07ba5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v5.6.0
======

* #244: Add type declarations in ABCs.

v5.5.0
======

Expand Down
10 changes: 10 additions & 0 deletions importlib_resources/_compat.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# flake8: noqa

import abc
import os
import sys
import pathlib
from contextlib import suppress
from typing import Union


if sys.version_info >= (3, 10):
from zipfile import Path as ZipPath # type: ignore
Expand Down Expand Up @@ -96,3 +99,10 @@ def wrap_spec(package):
from . import _adapters

return _adapters.SpecLoaderAdapter(package.__spec__, TraversableResourcesLoader)


if sys.version_info >= (3, 9):
StrPath = Union[str, os.PathLike[str]]
else:
# PathLike is only subscriptable at runtime in 3.9+
StrPath = Union[str, "os.PathLike[str]"]
28 changes: 16 additions & 12 deletions importlib_resources/abc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import abc
from typing import BinaryIO, Iterable, Text
import io
from typing import Any, BinaryIO, Iterable, Iterator, NoReturn, Text, Optional

from ._compat import runtime_checkable, Protocol
from ._compat import runtime_checkable, Protocol, StrPath


__all__ = ["ResourceReader", "Traversable", "TraversableResources"]


class ResourceReader(metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -54,19 +58,19 @@ class Traversable(Protocol):
"""

@abc.abstractmethod
def iterdir(self):
def iterdir(self) -> Iterator["Traversable"]:
"""
Yield Traversable objects in self
"""

def read_bytes(self):
def read_bytes(self) -> bytes:
"""
Read contents of self as bytes
"""
with self.open('rb') as strm:
return strm.read()

def read_text(self, encoding=None):
def read_text(self, encoding: Optional[str] = None) -> str:
"""
Read contents of self as text
"""
Expand All @@ -86,12 +90,12 @@ def is_file(self) -> bool:
"""

@abc.abstractmethod
def joinpath(self, child):
def joinpath(self, child: StrPath) -> "Traversable":
"""
Return Traversable child in self
"""

def __truediv__(self, child):
def __truediv__(self, child: StrPath) -> "Traversable":
"""
Return Traversable child in self
"""
Expand Down Expand Up @@ -121,17 +125,17 @@ class TraversableResources(ResourceReader):
"""

@abc.abstractmethod
def files(self):
def files(self) -> "Traversable":
"""Return a Traversable object for the loaded package."""

def open_resource(self, resource):
def open_resource(self, resource: StrPath) -> io.BufferedReader:
return self.files().joinpath(resource).open('rb')

def resource_path(self, resource):
def resource_path(self, resource: Any) -> NoReturn:
raise FileNotFoundError(resource)

def is_resource(self, path):
def is_resource(self, path: StrPath) -> bool:
return self.files().joinpath(path).is_file()

def contents(self):
def contents(self) -> Iterator[str]:
return (item.name for item in self.files().iterdir())

0 comments on commit 5b07ba5

Please sign in to comment.