FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-121190: A better error message from `importlib.resources.files()` when module spec is None by whitphx · Pull Request #138531 · python/cpython · GitHub

Repository navigation

gh-121190: A better error message from importlib.resources.files() when module spec is None - #138531

Closed
whitphx wants to merge 9 commits into
python:mainfrom
whitphx:gh-121190
Closed

whitphx wants to merge 9 commits into
python:mainfrom
whitphx:gh-121190

Conversation

whitphx commented Sep 5, 2025 •
edited
Loading

Copy link
Copy Markdown
Contributor

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)

danielhollas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks for working on this! 👏

Comment thread Lib/importlib/resources/_adapters.py Outdated

TYPE_CHECKING = False
if TYPE_CHECKING:
from ..machinery import ModuleSpec

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yes, please revert all type annotation changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks updated!

Comment thread Lib/importlib/resources/_common.py Outdated

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks. I proposed a new message. wdyt?

Comment thread Lib/importlib/resources/_common.py Outdated
Adapt a package spec to adapt the underlying loader.
"""

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks, got it 👍

danielhollas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

FFY00 commented Oct 4, 2025

Copy link
Copy Markdown
Member

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.

FFY00 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@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 commented Oct 22, 2025

Copy link
Copy Markdown
Contributor Author

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

jaraco self-assigned this Apr 12, 2026

jaraco commented Apr 12, 2026

Copy link
Copy Markdown
Member

Thanks for this change. I made some changes in the importlib_resources port, so I'd rather apply that here. This work will happen in gh-148460.

jaraco closed this Apr 12, 2026
hswong3i pushed a commit to alvistack/python-importlib_resources that referenced this pull request Apr 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

6 participants


Back | FazBrowse Home | New Git URL