|
14 | 14 | from ._abc import Loader |
15 | 15 | import abc |
16 | 16 | import warnings |
| 17 | +from typing import BinaryIO, Iterable, Text |
17 | 18 | from typing import Protocol, runtime_checkable |
18 | 19 |
|
19 | 20 |
|
@@ -297,49 +298,45 @@ def set_data(self, path, data): |
297 | 298 |
|
298 | 299 |
|
299 | 300 | class ResourceReader(metaclass=abc.ABCMeta): |
300 | | - |
301 | | - """Abstract base class to provide resource-reading support. |
302 | | -
|
303 | | - Loaders that support resource reading are expected to implement |
304 | | - the ``get_resource_reader(fullname)`` method and have it either return None |
305 | | - or an object compatible with this ABC. |
306 | | - """ |
| 301 | + """Abstract base class for loaders to provide resource reading support.""" |
307 | 302 |
|
308 | 303 | @abc.abstractmethod |
309 | | - def open_resource(self, resource): |
| 304 | + def open_resource(self, resource: Text) -> BinaryIO: |
310 | 305 | """Return an opened, file-like object for binary reading. |
311 | 306 |
|
312 | | - The 'resource' argument is expected to represent only a file name |
313 | | - and thus not contain any subdirectory components. |
314 | | -
|
| 307 | + The 'resource' argument is expected to represent only a file name. |
315 | 308 | If the resource cannot be found, FileNotFoundError is raised. |
316 | 309 | """ |
| 310 | + # This deliberately raises FileNotFoundError instead of |
| 311 | + # NotImplementedError so that if this method is accidentally called, |
| 312 | + # it'll still do the right thing. |
317 | 313 | raise FileNotFoundError |
318 | 314 |
|
319 | 315 | @abc.abstractmethod |
320 | | - def resource_path(self, resource): |
| 316 | + def resource_path(self, resource: Text) -> Text: |
321 | 317 | """Return the file system path to the specified resource. |
322 | 318 |
|
323 | | - The 'resource' argument is expected to represent only a file name |
324 | | - and thus not contain any subdirectory components. |
325 | | -
|
| 319 | + The 'resource' argument is expected to represent only a file name. |
326 | 320 | If the resource does not exist on the file system, raise |
327 | 321 | FileNotFoundError. |
328 | 322 | """ |
| 323 | + # This deliberately raises FileNotFoundError instead of |
| 324 | + # NotImplementedError so that if this method is accidentally called, |
| 325 | + # it'll still do the right thing. |
329 | 326 | raise FileNotFoundError |
330 | 327 |
|
331 | 328 | @abc.abstractmethod |
332 | | - def is_resource(self, name): |
333 | | - """Return True if the named 'name' is consider a resource.""" |
| 329 | + def is_resource(self, path: Text) -> bool: |
| 330 | + """Return True if the named 'path' is a resource. |
| 331 | +
|
| 332 | + Files are resources, directories are not. |
| 333 | + """ |
334 | 334 | raise FileNotFoundError |
335 | 335 |
|
336 | 336 | @abc.abstractmethod |
337 | | - def contents(self): |
338 | | - """Return an iterable of strings over the contents of the package.""" |
339 | | - return [] |
340 | | - |
341 | | - |
342 | | -_register(ResourceReader, machinery.SourceFileLoader) |
| 337 | + def contents(self) -> Iterable[str]: |
| 338 | + """Return an iterable of entries in `package`.""" |
| 339 | + raise FileNotFoundError |
343 | 340 |
|
344 | 341 |
|
345 | 342 | @runtime_checkable |
@@ -402,8 +399,7 @@ def open(self, mode='r', *args, **kwargs): |
402 | 399 | """ |
403 | 400 |
|
404 | 401 | @abc.abstractproperty |
405 | | - def name(self): |
406 | | - # type: () -> str |
| 402 | + def name(self) -> str: |
407 | 403 | """ |
408 | 404 | The base name of this object without any parent references. |
409 | 405 | """ |
|
0 commit comments