Skip to content
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

grass.script.core: Use a context manager for opening files (SIM115) to solve some ResourceWarnings #4559

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

echoix
Copy link
Member

@echoix echoix commented Oct 20, 2024

I'm keeping this PR small as grass.script.core is a more commonly used file.

For find_program() and _set_location_description(), a context manager was used directly, as I didn't consider there were better options.
In _set_location_description(), I don't understand why the codec module needed to be used to write to a text file, in append mode, in utf-8, instead of a standard open() with encoding set to utf-8, in text mode, and with append

The optional arguments of pathlib.Path().write_text() have the same meaning as in https://docs.python.org/3/library/functions.html#open. At the description of newline argument, they specify that:

When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.

This meant that writing the DEFAULT_WIND file in _create_location_xy() can be simplified to a static text write, and can use Path().write_text(). I kept the list of lines for now.

As with other PRs using pathlib to fix SIM115, when changing a function to use pathlib's Path, I change the rest of the function to use the same Path variables and calls. The amount of non-automated changes required for solving the PTH issues https://docs.astral.sh/ruff/rules/#flake8-use-pathlib-pth are too big to do by themselves in a separate PR. So while analyzing the whole function in scope, when possible I apply them.

@github-actions github-actions bot added Python Related code is in Python libraries labels Oct 20, 2024
@echoix echoix enabled auto-merge (squash) October 20, 2024 20:18
@echoix echoix requested a review from ninsbl October 20, 2024 20:18
@echoix echoix disabled auto-merge November 16, 2024 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libraries Python Related code is in Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant