Skip to content

Replace open(file, 'r') with open(file) #9591

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

Merged
merged 2 commits into from
Feb 23, 2021
Merged

Conversation

hexagonrecursion
Copy link
Contributor

Suggested by pyupgrade --py3-plus

@hexagonrecursion
Copy link
Contributor Author

Tests failed on python==3.9 with INTERNALERROR. Passed on other versions

@uranusjr uranusjr closed this Feb 10, 2021
@uranusjr uranusjr reopened this Feb 10, 2021
@uranusjr
Copy link
Member

Nudging CI to try again.

@uranusjr uranusjr added the skip news Does not need a NEWS file entry (eg: trivial changes) label Feb 10, 2021
@@ -11,7 +11,7 @@ def read(rel_path):
here = os.path.abspath(os.path.dirname(__file__))
# intentionally *not* adding an encoding option to open, See:
# https://github.com/pypa/virtualenv/issues/201#issuecomment-3145690
with open(os.path.join(here, rel_path), 'r') as fp:
with open(os.path.join(here, rel_path)) as fp:
Copy link
Contributor

Choose a reason for hiding this comment

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

This is OK for now. But it will fail when non-ASCII characters are added in README.rst or __init__.py.
Please add encoding="ascii" or encoding="utf-8".

Copy link
Member

@uranusjr uranusjr Feb 12, 2021

Choose a reason for hiding this comment

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

encoding="utf-8" is intentionally not addedd here for setuptools compatibility. setuptools does not write metadata with UTF-8, but platform encoding, so using UTF-8 here would break packaging.

One possible denominator is to use encoding="ascii", error="replace".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch!

https://docs.python.org/3/library/functions.html#open
The default encoding is platform dependent

We should probably go through all calls to open() and pathlib.Path.read_text() add encoding=something everywhere. It may even be a good idea to add encoding=locale.getpreferredencoding() where the dependence on the OS and configuration is intentional. I think this is out of scope for this PR. As far as I understand open(filename, 'r') is exactly equivalent to open(filename) in python3

Copy link
Contributor

Choose a reason for hiding this comment

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

If so, why not encoding="ascii"?

It prevents non-ASCII characters are added in README accidentally.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, encoding="ascii" without replace would make sense here. I’d prefer this be made into a separate PR, however, and include reasoning in that PR to make it more discoverable for future maintainers.

@@ -145,7 +145,7 @@ def deduce_helpful_msg(req):
msg = " The path does exist. "
# Try to parse and check if it is a requirements file.
try:
with open(req, 'r') as fp:
with open(req) as fp:
Copy link
Contributor

Choose a reason for hiding this comment

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

This may fail on Windows if non-ASCII comments in the requirements file.
Please add encoding="utf-8".

Copy link
Member

Choose a reason for hiding this comment

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

Similarly, the encoding parameter would be a backward-incompatible change, because historically pip has been reading the file with platform encoding. We can (and probably should at some point) switch to UTF-8 everywhere, but that needs to go through a deprecation cycle, and should be treated as a separate feature request.

@@ -543,7 +543,7 @@ def from_dist(cls, dist):

elif develop_egg_link:
# develop egg
with open(develop_egg_link, 'r') as fh:
with open(develop_egg_link) as fh:
Copy link
Contributor

Choose a reason for hiding this comment

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

What is encoding of egg_link file. What happen if non-ASCII path on Windows?

Copy link
Member

Choose a reason for hiding this comment

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

It’s undefined because distutils is so ancient people didn’t care about this back then. Platform encoding is the backward compatible choice here.

Copy link
Member

@pradyunsg pradyunsg left a comment

Choose a reason for hiding this comment

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

LGTM. This doesn't change any of our existing behaviours.

We can file a follow up issue, in case someone wants to discuss how we should improve the handling of these files going forward. :)

@pradyunsg pradyunsg merged commit baaf66f into pypa:master Feb 23, 2021
@hexagonrecursion hexagonrecursion deleted the open branch February 23, 2021 13:55
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
skip news Does not need a NEWS file entry (eg: trivial changes)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants