1
1
import abc
2
- import io
3
2
import itertools
4
3
import pathlib
5
4
from typing import (
11
10
Optional ,
12
11
Protocol ,
13
12
Text ,
13
+ TextIO ,
14
+ Union ,
15
+ overload ,
14
16
runtime_checkable ,
15
17
)
16
18
19
+ from typing_extensions import Literal
20
+
17
21
from .compat .py38 import StrPath
18
22
19
23
__all__ = ["ResourceReader" , "Traversable" , "TraversableResources" ]
@@ -138,8 +142,16 @@ def __truediv__(self, child: StrPath) -> "Traversable":
138
142
"""
139
143
return self .joinpath (child )
140
144
145
+ @overload
146
+ def open (self , mode : Literal ['r' ] = 'r' , * args : Any , ** kwargs : Any ) -> TextIO : ...
147
+
148
+ @overload
149
+ def open (self , mode : Literal ['rb' ], * args : Any , ** kwargs : Any ) -> BinaryIO : ...
150
+
141
151
@abc .abstractmethod
142
- def open (self , mode = 'r' , * args , ** kwargs ):
152
+ def open (
153
+ self , mode : str = 'r' , * args : Any , ** kwargs : Any
154
+ ) -> Union [TextIO , BinaryIO ]:
143
155
"""
144
156
mode may be 'r' or 'rb' to open as text or binary. Return a handle
145
157
suitable for reading (same as pathlib.Path.open).
@@ -166,7 +178,7 @@ class TraversableResources(ResourceReader):
166
178
def files (self ) -> "Traversable" :
167
179
"""Return a Traversable object for the loaded package."""
168
180
169
- def open_resource (self , resource : StrPath ) -> io . BufferedReader :
181
+ def open_resource (self , resource : StrPath ) -> BinaryIO :
170
182
return self .files ().joinpath (resource ).open ('rb' )
171
183
172
184
def resource_path (self , resource : Any ) -> NoReturn :
0 commit comments