Skip to content

Commit de976a0

Browse files
committed
Inline type defintions
1 parent 3110294 commit de976a0

File tree

3 files changed

+33
-44
lines changed

3 files changed

+33
-44
lines changed

importlib_resources/_common.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414
Package = Union[types.ModuleType, str]
1515

1616

17-
def files(package):
18-
# type: (Package) -> Traversable
17+
def files(package: Package) -> Traversable:
1918
"""
2019
Get a Traversable resource from a package
2120
"""
2221
return from_package(get_package(package))
2322

2423

25-
def get_resource_reader(package):
26-
# type: (types.ModuleType) -> Optional[ResourceReader]
24+
def get_resource_reader(package: types.ModuleType) -> Optional[ResourceReader]:
2725
"""
2826
Return the package's loader if it's a ResourceReader.
2927
"""
@@ -39,13 +37,11 @@ def get_resource_reader(package):
3937
return reader(spec.name) # type: ignore
4038

4139

42-
def resolve(cand):
43-
# type: (Package) -> types.ModuleType
40+
def resolve(cand: Package) -> types.ModuleType:
4441
return cand if isinstance(cand, types.ModuleType) else importlib.import_module(cand)
4542

4643

47-
def get_package(package):
48-
# type: (Package) -> types.ModuleType
44+
def get_package(package: Package) -> types.ModuleType:
4945
"""Take a package name or module object and return the module.
5046
5147
Raise an exception if the resolved module is not a package.

importlib_resources/_legacy.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ def wrapper(*args, **kwargs):
2727
return wrapper
2828

2929

30-
def normalize_path(path):
31-
# type: (Any) -> str
30+
def normalize_path(path: Any) -> str:
3231
"""Normalize a path by ensuring it is a string.
3332
3433
If the resulting string contains path separators, an exception is raised.

importlib_resources/simple.py

+28-34
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,26 @@ class SimpleReader(abc.ABC):
1717
"""
1818

1919
@abc.abstractproperty
20-
def package(self):
21-
# type: () -> str
20+
def package(self) -> str:
2221
"""
2322
The name of the package for which this reader loads resources.
2423
"""
2524

2625
@abc.abstractmethod
27-
def children(self):
28-
# type: () -> List['SimpleReader']
26+
def children(self) -> List['SimpleReader']:
2927
"""
3028
Obtain an iterable of SimpleReader for available
3129
child containers (e.g. directories).
3230
"""
3331

3432
@abc.abstractmethod
35-
def resources(self):
36-
# type: () -> List[str]
33+
def resources(self) -> List[str]:
3734
"""
3835
Obtain available named resources for this virtual package.
3936
"""
4037

4138
@abc.abstractmethod
42-
def open_binary(self, resource):
43-
# type: (str) -> BinaryIO
39+
def open_binary(self, resource: str) -> BinaryIO:
4440
"""
4541
Obtain a File-like for a named resource.
4642
"""
@@ -50,13 +46,35 @@ def name(self):
5046
return self.package.split('.')[-1]
5147

5248

49+
class ResourceContainer(Traversable):
50+
"""
51+
Traversable container for a package's resources via its reader.
52+
"""
53+
54+
def __init__(self, reader: SimpleReader):
55+
self.reader = reader
56+
57+
def is_dir(self):
58+
return True
59+
60+
def is_file(self):
61+
return False
62+
63+
def iterdir(self):
64+
files = (ResourceHandle(self, name) for name in self.reader.resources)
65+
dirs = map(ResourceContainer, self.reader.children())
66+
return itertools.chain(files, dirs)
67+
68+
def open(self, *args, **kwargs):
69+
raise IsADirectoryError()
70+
71+
5372
class ResourceHandle(Traversable):
5473
"""
5574
Handle to a named resource in a ResourceReader.
5675
"""
5776

58-
def __init__(self, parent, name):
59-
# type: (ResourceContainer, str) -> None
77+
def __init__(self, parent: ResourceContainer, name: str):
6078
self.parent = parent
6179
self.name = name # type: ignore
6280

@@ -76,30 +94,6 @@ def joinpath(self, name):
7694
raise RuntimeError("Cannot traverse into a resource")
7795

7896

79-
class ResourceContainer(Traversable):
80-
"""
81-
Traversable container for a package's resources via its reader.
82-
"""
83-
84-
def __init__(self, reader):
85-
# type: (SimpleReader) -> None
86-
self.reader = reader
87-
88-
def is_dir(self):
89-
return True
90-
91-
def is_file(self):
92-
return False
93-
94-
def iterdir(self):
95-
files = (ResourceHandle(self, name) for name in self.reader.resources)
96-
dirs = map(ResourceContainer, self.reader.children())
97-
return itertools.chain(files, dirs)
98-
99-
def open(self, *args, **kwargs):
100-
raise IsADirectoryError()
101-
102-
10397
class TraversableReader(TraversableResources, SimpleReader):
10498
"""
10599
A TraversableResources based on SimpleReader. Resource providers

0 commit comments

Comments
 (0)