Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Lib/importlib/resources/_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@

from . import abc

TYPE_CHECKING = False
if TYPE_CHECKING:
from ..machinery import ModuleSpec
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python stdlib is not typed (that's what typeshed is for) so you'll likely be asked to revert these changes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please revert all type annotation changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks updated!



class SpecLoaderAdapter:
"""
Adapt a package spec to adapt the underlying loader.
"""

def __init__(self, spec, adapter=lambda spec: spec.loader):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was the default adapter removed? Isn't this a breaking change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the simplicity sake, because it seems not to be used anywhere.
This default value was defined since this mechanism was introduced in #24670 but it was not used even in that commit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it does not pertain to the issue that's addressed in this PR I'd recommend reverting this change anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, got it 👍

def __init__(self, spec: ModuleSpec, adapter):
self.spec = spec
self.loader = adapter(spec)

Expand Down Expand Up @@ -160,9 +164,9 @@ def files(self):
return CompatibilityFiles.SpecPath(self.spec, self._reader)


def wrap_spec(package):
def wrap_spec(spec: ModuleSpec):
"""
Construct a package spec with traversable compatibility
on the spec/loader/reader.
"""
return SpecLoaderAdapter(package.__spec__, TraversableResourcesLoader)
return SpecLoaderAdapter(spec, TraversableResourcesLoader)
5 changes: 4 additions & 1 deletion Lib/importlib/resources/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ def from_package(package: types.ModuleType):
# deferred for performance (python/cpython#109829)
from ._adapters import wrap_spec

spec = wrap_spec(package)
if package.__spec__ is None:
raise TypeError(f"Can't access resources on a module with no spec: {package}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder is the error can be a bit more specific. I think most Python users will not know what a "spec" is? (I don't :-) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I proposed a new message. wdyt?


spec = wrap_spec(package.__spec__)
reader = spec.loader.get_resource_reader(spec.name)
return reader.files()

Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_importlib/resources/test_resource.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import types

from . import util
from importlib import resources, import_module
Expand Down Expand Up @@ -232,5 +233,17 @@ class ResourceFromNamespaceZipTests(
MODULE = 'namespacedata01'


class ResourceFromMainModuleWithNoneSpecTests(unittest.TestCase):
# `__main__.__spec__` can be `None` depending on how it is populated.
# https://docs.python.org/3/reference/import.html#main-spec
def test_main_module_with_none_spec(self):
mainmodule = types.ModuleType("__main__")

self.assertIsNone(mainmodule.__spec__)

with self.assertRaises(TypeError):
resources.files(mainmodule)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``importlib.resources.files(package)`` raises an error with a clearer message when ``package.__spec__`` is ``None``
Loading