Skip to content

Migrate from os.path to pathlib - #3119

Merged
seisman merged 16 commits into
mainfrom
pathlib
Mar 20, 2024
Merged

seisman merged 16 commits into
mainfrom
pathlib

Conversation

@seisman

@seisman seisman commented Mar 18, 2024 •

Copy link
Copy Markdown
Member

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

seisman commented Mar 20, 2024

Copy link
Copy Markdown
Member Author

Will merge the PR in 24 hours if no objections.

@weiji14

weiji14 commented Mar 20, 2024 •

Copy link
Copy Markdown
Member

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

@seisman

seisman commented Mar 20, 2024

Copy link
Copy Markdown
Member Author

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().

@weiji14 weiji14 left a comment •

Copy link
Copy Markdown
Member

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.

Comment thread pygmt/tests/test_figure.py Resolved
Comment thread pygmt/tests/test_figure.py Resolved
Comment thread pygmt/tests/test_psconvert.py Resolved
Comment thread pygmt/tests/test_psconvert.py Resolved
Comment thread pygmt/tests/test_psconvert.py Resolved
@weiji14

weiji14 commented Mar 20, 2024

Copy link
Copy Markdown
Member

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

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

@seisman

seisman commented Mar 20, 2024

Copy link
Copy Markdown
Member Author

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
@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