Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
graingert wants to merge16 commits intopython:main
base:main
Choose a base branch
Loading
fromgraingert:use-importlib-resources-for-doctest

Conversation

graingert
Copy link
Contributor

@graingertgraingert commentedJan 15, 2025
edited by bedevere-appbot
Loading

@jaracojaraco self-assigned thisFeb 2, 2025
@graingertgraingert requested a review fromjaracoMarch 20, 2025 16:32
def _load_testfile(filename, package, module_relative, encoding):
if module_relative:
package = _normalize_module(package, 3)
with contextlib.suppress(Exception):

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.

Copy link
Member

@AA-TurnerAA-TurnerApr 9, 2025
edited
Loading

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 asuppress context manager.

Perhaps:

text=Noneifmodule_relative:package=_normalize_module(package,depth=3)try:file=importlib.resources.files(package)/filenametext=file.read_text(encoding=encoding)exceptAttributeError:filename=_module_relative_path(package,filename)iftextisNone:withopen(filename,encoding=encoding)asf:text=f.read()returntext,filename

Copy link
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
ContributorAuthor

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
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 fromException toAttributeError, 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.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment
edited
Loading

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
Copy link

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 phraseI 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.

Copy link
ContributorAuthor

@graingertgraingert left a comment

Choose a reason for hiding this comment

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

thanks!

try:
file = importlib.resources.files(package) / filename
text = file.read_text(encoding=encoding)
except AttributeError:

Choose a reason for hiding this comment

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

Is it raised byimportlib.resources.files() or byfile.read_text()? Cantext be None?

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka requested changes

@brettcannonbrettcannonAwaiting requested review from brettcannon

@AA-TurnerAA-TurnerAwaiting requested review from AA-Turner

@jaracojaracoAwaiting requested review from jaraco

Assignees

@jaracojaraco

Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@graingert@jaraco@serhiy-storchaka@AA-Turner

[8]ページ先頭

©2009-2025 Movatter.jp