11import abc
2- import io
32import itertools
43import os
54import pathlib
6- from typing import Any, BinaryIO, Iterable, Iterator, NoReturn, Text, Optional
7- from typing import runtime_checkable, Protocol
8- from typing import Union
9-
5+ from typing import (
6+ Any,
7+ BinaryIO,
8+ Iterable,
9+ Iterator,
10+ Literal,
11+ NoReturn,
12+ Optional,
13+ Protocol,
14+ Text,
15+ TextIO,
16+ Union,
17+ overload,
18+ runtime_checkable,
19+ )
1020
1121StrPath = Union[str, os.PathLike[str]]
1222
@@ -82,11 +92,13 @@ def read_bytes(self) -> bytes:
8292 with self.open('rb') as strm:
8393 return strm.read()
8494
85- def read_text(self, encoding: Optional[str] = None) -> str:
95+ def read_text(
96+ self, encoding: Optional[str] = None, errors: Optional[str] = None
97+ ) -> str:
8698 """
8799 Read contents of self as text
88100 """
89- with self.open(encoding=encoding) as strm:
101+ with self.open(encoding=encoding, errors=errors ) as strm:
90102 return strm.read()
91103
92104 @abc.abstractmethod
@@ -132,8 +144,16 @@ def __truediv__(self, child: StrPath) -> "Traversable":
132144 """
133145 return self.joinpath(child)
134146
147+ @overload
148+ def open(self, mode: Literal['r'] = 'r', *args: Any, **kwargs: Any) -> TextIO: ...
149+
150+ @overload
151+ def open(self, mode: Literal['rb'], *args: Any, **kwargs: Any) -> BinaryIO: ...
152+
135153 @abc.abstractmethod
136- def open(self, mode='r', *args, **kwargs):
154+ def open(
155+ self, mode: str = 'r', *args: Any, **kwargs: Any
156+ ) -> Union[TextIO, BinaryIO]:
137157 """
138158 mode may be 'r' or 'rb' to open as text or binary. Return a handle
139159 suitable for reading (same as pathlib.Path.open).
@@ -160,7 +180,7 @@ class TraversableResources(ResourceReader):
160180 def files(self) -> "Traversable":
161181 """Return a Traversable object for the loaded package."""
162182
163- def open_resource(self, resource: StrPath) -> io.BufferedReader :
183+ def open_resource(self, resource: StrPath) -> BinaryIO :
164184 return self.files().joinpath(resource).open('rb')
165185
166186 def resource_path(self, resource: Any) -> NoReturn:
0 commit comments