Skip to content

Commit 5b1281d

Browse files
committed
Remove restriction that a 'package' cannot be a module. Allows resolution of resources from adjacent modules, even those not found in a package. Fixes #203.
1 parent 94d59b6 commit 5b1281d

File tree

3 files changed

+3
-27
lines changed

3 files changed

+3
-27
lines changed

importlib_resources/_common.py

+1-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def files(package: Package) -> Traversable:
1818
"""
1919
Get a Traversable resource from a package
2020
"""
21-
return from_package(get_package(package))
21+
return from_package(resolve(package))
2222

2323

2424
def get_resource_reader(package: types.ModuleType) -> Optional[ResourceReader]:
@@ -47,17 +47,6 @@ def _(cand: str):
4747
return importlib.import_module(cand)
4848

4949

50-
def get_package(package: Package) -> types.ModuleType:
51-
"""Take a package name or module object and return the module.
52-
53-
Raise an exception if the resolved module is not a package.
54-
"""
55-
resolved = resolve(package)
56-
if wrap_spec(resolved).submodule_search_locations is None:
57-
raise TypeError(f'{package!r} is not a package')
58-
return resolved
59-
60-
6150
def from_package(package):
6251
"""
6352
Return a Traversable object for the given package.

importlib_resources/tests/test_files.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ def test_module_resources(self):
6464
_path.build(spec, self.site_dir)
6565
import mod
6666

67-
# currently a failure occurs; ref #203
68-
with self.assertRaisesRegex(TypeError, '.*mod.* is not a package'):
69-
actual = resources.files(mod).joinpath('res.txt').read_text()
70-
assert actual == spec['res.txt']
67+
actual = resources.files(mod).joinpath('res.txt').read_text()
68+
assert actual == spec['res.txt']
7169

7270

7371
if __name__ == '__main__':

importlib_resources/tests/util.py

-11
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,6 @@ def test_importing_module_as_side_effect(self):
102102
del sys.modules[data01.__name__]
103103
self.execute(data01.__name__, 'utf-8.file')
104104

105-
def test_non_package_by_name(self):
106-
# The anchor package cannot be a module.
107-
with self.assertRaises(TypeError):
108-
self.execute(__name__, 'utf-8.file')
109-
110-
def test_non_package_by_package(self):
111-
# The anchor package cannot be a module.
112-
with self.assertRaises(TypeError):
113-
module = sys.modules['importlib_resources.tests.util']
114-
self.execute(module, 'utf-8.file')
115-
116105
def test_missing_path(self):
117106
# Attempting to open or read or request the path for a
118107
# non-existent path should succeed if open_resource

0 commit comments

Comments
 (0)