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

Migrate from os.path to pathlib #3119

Merged
merged 16 commits into from
Mar 20, 2024
Merged

Migrate from os.path to pathlib #3119

merged 16 commits into from
Mar 20, 2024

Conversation

seisman
Copy link
Member

@seisman seisman commented Mar 18, 2024

Migrate from os.path to pathlib.Path following this table: https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module.

Please note that:

if os.path.exists(fname):
   os.unlink(fname)

is equivalent to Path(fname).unlink(missing_ok=True).

assert os.path.exists(fname)
os.unlink(fname)

is equivalent to Path(fname).unlink(missing_ok=False) or just Path(fname).unlink().

Closes #1834.

@seisman seisman marked this pull request as ready for review March 18, 2024 08:09
@seisman seisman added the maintenance Boring but important stuff for the core devs label Mar 18, 2024
@seisman seisman added this to the 0.12.0 milestone Mar 18, 2024
@seisman seisman added the needs review This PR has higher priority and needs review. label Mar 18, 2024
@seisman seisman added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Mar 20, 2024
@seisman
Copy link
Member Author

seisman commented Mar 20, 2024

Will merge the PR in 24 hours if no objections.

@weiji14
Copy link
Member

weiji14 commented Mar 20, 2024

Do you want to enable ruff's PTH rules in this PR as mentioned at #1834 (comment), or in a separate PR?

@seisman
Copy link
Member Author

seisman commented Mar 20, 2024

Yes, but enabling PTH rules result in many violations against the PTH123 rule.

Better to do it in a separate PR and also need to discuss if we want to replace open with Path().open().

Copy link
Member

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

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

I'm seeing that a lot of the assert os.path.exists() lines have been removed, but not replaced with Path.exists(), possibly because there is a Path.unlink() directly afterwards that would raise a FileNotFoundError if the file was missing (see https://docs.python.org/3/library/pathlib.html#pathlib.Path.unlink), is that correct?

I think there are some cases where we do need to explicitly do assert Path.exists(), especially if the test name is something like test_file_exists. Otherwise it's not apparent that .unlink() actually implicitly checks if the file exists already.

pygmt/tests/test_figure.py Outdated Show resolved Hide resolved
pygmt/tests/test_figure.py Show resolved Hide resolved
pygmt/tests/test_psconvert.py Outdated Show resolved Hide resolved
pygmt/tests/test_psconvert.py Outdated Show resolved Hide resolved
pygmt/tests/test_psconvert.py Outdated Show resolved Hide resolved
@weiji14
Copy link
Member

weiji14 commented Mar 20, 2024

Yes, but enabling PTH rules result in many violations against the PTH123 rule.

We could also apply PTH, but ignore PTH123 for now?

@seisman
Copy link
Member Author

seisman commented Mar 20, 2024

I think there are some cases where we do need to explicitly do assert Path.exists(), especially if the test name is something like test_file_exists. Otherwise it's not apparent that .unlink() actually implicitly checks if the file exists already.

Added back in ee0ba18.

Yes, but enabling PTH rules result in many violations against the PTH123 rule.

We could also apply PTH, but ignore PTH123 for now?

In some cases migrating from open to Path.open makes sense, so I prefer to do it in a separate PR.

@seisman seisman merged commit 0cdb87d into main Mar 20, 2024
19 checks passed
@seisman seisman deleted the pathlib branch March 20, 2024 09:23
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace os.path with pathlib
2 participants