Skip to content

gh-128862: use importlib.resources to acquire doctest resources - #128865

Closed
graingert wants to merge 16 commits into
python:mainfrom
graingert:use-importlib-resources-for-doctest
Closed

graingert wants to merge 16 commits into
python:mainfrom
graingert:use-importlib-resources-for-doctest

Conversation

@graingert

@graingert graingert commented Jan 15, 2025 •

Copy link
Copy Markdown
Contributor

@graingert
graingert requested a review from brettcannon January 15, 2025 13:42
Comment thread Lib/doctest.py
@graingert
graingert requested a review from jaraco January 26, 2025 09:35
@jaraco jaraco self-assigned this Feb 2, 2025
Comment thread Lib/doctest.py Outdated
Comment thread Lib/doctest.py Outdated
graingert and others added 2 commits March 20, 2025 16:31
@graingert
graingert requested a review from jaraco March 20, 2025 16:32
Comment thread Lib/doctest.py Outdated
def _load_testfile(filename, package, module_relative, encoding):
if module_relative:
package = _normalize_module(package, 3)
with contextlib.suppress(Exception):

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.

What exceptions should be suppressed?

Exception is too wide class. It includes OSError, UnicodeDecodingError, MemoryError, which currently are not suppressed.

@AA-Turner AA-Turner Apr 9, 2025 •

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.

This control flow feels odd, because we're returning in a suppress context manager.

Perhaps:

text = None
if module_relative:
    package = _normalize_module(package, depth=3)
    try:
        file = importlib.resources.files(package) / filename
        text = file.read_text(encoding=encoding)
    except AttributeError:
        filename = _module_relative_path(package, filename)

if text is None:
    with open(filename, encoding=encoding) as f:
        text = f.read()

return text, filename

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.

cc @graingert, are you able to get to this before Tuesday (feature freeze)?

A

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.

I think @jaraco has taken over this PR

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.

I've applied my suggestion, narrowing from Exception to AttributeError, per Jason above:

Even better would be if importlib.resources could throw a more specific exception when the indicated module is unable to resolve resources, but since it currently just raises an AttributeError, perhaps it's best to just trap that exception, rather than try to pre-emptively detect that exception will occur.

Comment thread Lib/doctest.py Outdated
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>

@serhiy-storchaka serhiy-storchaka 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.

Do not suppress arbitrary Exception, suppress only the necessary.

Also, I do not think that it is worth to import contextlib for a simple except.

@bedevere-app

bedevere-app Bot commented May 1, 2025

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@graingert graingert left a comment

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.

thanks!

Comment thread Misc/NEWS.d/next/Library/2025-01-15-11-57-42.gh-issue-128862.TDHQdR.rst Outdated
Comment thread Lib/doctest.py
try:
file = importlib.resources.files(package) / filename
text = file.read_text(encoding=encoding)
except AttributeError:

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.

Is it raised by importlib.resources.files() or by file.read_text()? Can text be None?

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@brettcannon

Copy link
Copy Markdown
Member

@graingert FYI your PR has merge conflicts and you have not left a reply with the magic sentence to tell Bedevere that you're ready for a review.

@brettcannon
brettcannon removed their request for review October 7, 2025 18:52
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 21, 2026
@graingert graingert closed this Aug 31, 2026
@graingert
graingert deleted the use-importlib-resources-for-doctest branch August 31, 2026 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants