grass.script.core: Use a context manager for opening files (SIM115) to solve some ResourceWarnings #4559
+21
−28
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 appendThe 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 ofnewline
argument, they specify that:This meant that writing the DEFAULT_WIND file in
_create_location_xy()
can be simplified to a static text write, and can usePath().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.