-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-128862: use importlib.resources to acquire doctest resources #128865
base: main
Are you sure you want to change the base?
gh-128862: use importlib.resources to acquire doctest resources #128865
Conversation
Co-authored-by: Jason R. Coombs <[email protected]>
def _load_testfile(filename, package, module_relative, encoding): | ||
if module_relative: | ||
package = _normalize_module(package, 3) | ||
with contextlib.suppress(Exception): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
text = resources.read_text(package, filename, encoding=encoding)
except:
filename = _module_relative_path(package, filename)
if text is None:
with open(filename, encoding=encoding) as f:
text = f.read()
return text, filename
return ( | ||
importlib.resources.read_text(package, filename, encoding=encoding), | ||
filename, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nitpick. I think that the following code may look better:
return ( | |
importlib.resources.read_text(package, filename, encoding=encoding), | |
filename, | |
) | |
text = importlib.resources.read_text(package, filename, | |
encoding=encoding) | |
return text, filename |
doctest._load_testfile
uses deprecated Loader.get_data API, it should use importlib.resources #128862