-
-
Notifications
You must be signed in to change notification settings - Fork 327
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
python: Use a context manager for opening files (SIM115) to solve some ResourceWarnings #4224
Conversation
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.
Most of the PR looks good and can be merged, but the t.rast.what code is a lot of review for the change it brings and there is a problem with closing sys.stdout, so I suggest removing that file from this PR.
I'll take a new look at my next available coding time. Since that time, I've found other ways to not have to indent everything, by moving the static contents up and keeping the part that has to be indented small. Otherwise, it might be easier to look at the diff side by side rather than inline, and VS Code with whitespace highlighting is really good at seing that there are literally no change apart 4 spaces + black. |
This PR is not ready yet, there's still the stdout handling to go through |
55a2f38
to
ecab2a0
Compare
Ready now to review. See the review comment thread for why contextlib.nullcontext is used for sys.stdout, allowing to use the same shape of code (with a |
This PR starts fixing some ResourceWarnings. I'm starting small, I am juggling with 4 sets of changes that are tested individually and one on top of the others, plus on top of my coverage branch. There are at least 368 places where a context manager should be used to open and close files. It shouldn't be done in one PR.
This first set is ready to be merged. It already solves some resource warnings appearing in macOS Pytest jobs that I was seeing.
Later on, I will merge some changes, but will explicitly make sure that changes in an input/output pair are in separate PRs. For example, if stds_export is changed, stds_import will be in another one. For changes in gunittest, it will be on their own. That way, it will help isolate the possibility of changing the code and the code treating the expected value, and will help make sure the existing code works well with only this part of the changes.