Skip to content

Commit 5b07ba5

Browse files
committed
Merge pull request python#244 from henryiii/henryiii/types/fix1
fix(types): Adding some typing from typeshed
2 parents 0526c20 + 2c4a1bd commit 5b07ba5

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
v5.6.0
2+
======
3+
4+
* #244: Add type declarations in ABCs.
5+
16
v5.5.0
27
======
38

importlib_resources/_compat.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# flake8: noqa
22

33
import abc
4+
import os
45
import sys
56
import pathlib
67
from contextlib import suppress
8+
from typing import Union
9+
710

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

98101
return _adapters.SpecLoaderAdapter(package.__spec__, TraversableResourcesLoader)
102+
103+
104+
if sys.version_info >= (3, 9):
105+
StrPath = Union[str, os.PathLike[str]]
106+
else:
107+
# PathLike is only subscriptable at runtime in 3.9+
108+
StrPath = Union[str, "os.PathLike[str]"]

importlib_resources/abc.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import abc
2-
from typing import BinaryIO, Iterable, Text
2+
import io
3+
from typing import Any, BinaryIO, Iterable, Iterator, NoReturn, Text, Optional
34

4-
from ._compat import runtime_checkable, Protocol
5+
from ._compat import runtime_checkable, Protocol, StrPath
6+
7+
8+
__all__ = ["ResourceReader", "Traversable", "TraversableResources"]
59

610

711
class ResourceReader(metaclass=abc.ABCMeta):
@@ -54,19 +58,19 @@ class Traversable(Protocol):
5458
"""
5559

5660
@abc.abstractmethod
57-
def iterdir(self):
61+
def iterdir(self) -> Iterator["Traversable"]:
5862
"""
5963
Yield Traversable objects in self
6064
"""
6165

62-
def read_bytes(self):
66+
def read_bytes(self) -> bytes:
6367
"""
6468
Read contents of self as bytes
6569
"""
6670
with self.open('rb') as strm:
6771
return strm.read()
6872

69-
def read_text(self, encoding=None):
73+
def read_text(self, encoding: Optional[str] = None) -> str:
7074
"""
7175
Read contents of self as text
7276
"""
@@ -86,12 +90,12 @@ def is_file(self) -> bool:
8690
"""
8791

8892
@abc.abstractmethod
89-
def joinpath(self, child):
93+
def joinpath(self, child: StrPath) -> "Traversable":
9094
"""
9195
Return Traversable child in self
9296
"""
9397

94-
def __truediv__(self, child):
98+
def __truediv__(self, child: StrPath) -> "Traversable":
9599
"""
96100
Return Traversable child in self
97101
"""
@@ -121,17 +125,17 @@ class TraversableResources(ResourceReader):
121125
"""
122126

123127
@abc.abstractmethod
124-
def files(self):
128+
def files(self) -> "Traversable":
125129
"""Return a Traversable object for the loaded package."""
126130

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

130-
def resource_path(self, resource):
134+
def resource_path(self, resource: Any) -> NoReturn:
131135
raise FileNotFoundError(resource)
132136

133-
def is_resource(self, path):
137+
def is_resource(self, path: StrPath) -> bool:
134138
return self.files().joinpath(path).is_file()
135139

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy