Skip to content

Conversation

@whitphx
Copy link
Contributor

@whitphx whitphx commented Sep 5, 2025

importlib.resources.files(package) raises an error when package.__spec__ is None because the code below refers to spec.name but spec is None.

reader = spec.loader.get_resource_reader(spec.name)

The error would be

AttributeError: 'NoneType' object has no attribute 'name'`

and #121190 said it's not helpful.

This PR adds a None-check to handle such a case and raise an error with a specific message.

(This is from PyConTW 2025 sprint)

Copy link
Contributor

@danielhollas danielhollas left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! 👏


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!


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?

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 👍

Copy link
Contributor

@danielhollas danielhollas left a comment

Choose a reason for hiding this comment

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

By the way, importlib.resources module is somewhat special as its development is mainly in importlib_resources package which is then backported here, see https://github.com/python/importlib_metadata/wiki/Development-Methodology

So I am not sure if this PR should be merged here or moved over to the importlib_resources repo (CC @jaraco)

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.

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

@FFY00
Copy link
Member

FFY00 commented Oct 4, 2025

By the way, importlib.resources module is somewhat special as its development is mainly in importlib_resources package which is then backported here, see https://github.com/python/importlib_metadata/wiki/Development-Methodology

So I am not sure if this PR should be merged here or moved over to the importlib_resources repo (CC @jaraco)

It can be merged here, but we prefer to merge changes in importlib_resources to make it easier to keep the code in sync.

Copy link
Member

@FFY00 FFY00 left a comment

Choose a reason for hiding this comment

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

@whitphx, thank you so much for this PR!

It looks good to me, but I'd like @jaraco to have a look too.

whitphx and others added 2 commits October 22, 2025 22:03
Co-authored-by: Daniel Hollas <danekhollas@gmail.com>
@whitphx
Copy link
Contributor Author

whitphx commented Oct 22, 2025

Thank you, I created a PR to python/importlib_resources as well: python/importlib_resources#331

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants