Skip to content

4523: Migrate to pathlib.Path#7087

Open
SgtVarmint wants to merge 9 commits into
SciTools:mainfrom
SgtVarmint:4523-migrate_to_pathlib
Open

4523: Migrate to pathlib.Path#7087
SgtVarmint wants to merge 9 commits into
SciTools:mainfrom
SgtVarmint:4523-migrate_to_pathlib

Conversation

@SgtVarmint
Copy link
Copy Markdown

@SgtVarmint SgtVarmint commented May 13, 2026

Closes #4523 in migrating uses of os.path to pathlib.Path where possible.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 13, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown
Contributor

@scitools-ci scitools-ci Bot left a comment

Choose a reason for hiding this comment

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

Templating

This PR includes changes that may be worth sharing via templating. For each file listed below, please either:

  • Action the suggestion via a pull request editing/adding the relevant file in the SciTools/.github templates/ directory. 1
  • Raise an issue against the SciTools/.github repo for the above action if you really don't have 10mins spare right now. Include an assignee, to avoid it being forgotten.
  • Dismiss the suggestion if the changes are not suitable for templating.

You will need to dismiss this review before this PR can be merged. Recommend the reviewer does this as their final action before merging, as this text will continually update as commits come in.

Template candidates

The following changed files are not currently templated, but their parent directories suggest they may be good candidates for a new template to be created:

Footnotes

  1. Include this text in the PR body to avoid any notifications about applying the template changes back to the source repo!
    @scitools-templating: please no update notification on: iris

@SgtVarmint
Copy link
Copy Markdown
Author

I also wanted to get some insight on the comment on templating. I'm not sure whether docs/src/conf.py is actually a good candidate for templating, but I wanted to confirm before definitively saying so.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 14, 2026

Codecov Report

❌ Patch coverage is 85.29412% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.13%. Comparing base (6835fd3) to head (cb5db70).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
lib/iris/config.py 62.50% 2 Missing and 1 partial ⚠️
lib/iris/fileformats/netcdf/saver.py 50.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7087   +/-   ##
=======================================
  Coverage   90.12%   90.13%           
=======================================
  Files          91       91           
  Lines       24951    24956    +5     
  Branches     4683     4684    +1     
=======================================
+ Hits        22488    22495    +7     
+ Misses       1684     1683    -1     
+ Partials      779      778    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@ukmo-ccbunney ukmo-ccbunney left a comment

Choose a reason for hiding this comment

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

Hi @SgtVarmint

Thanks very much for all the hard work converting os.path to pathlib.Path 🙏🏼

I've reviewed you changes and have a few suggested changes.

Comment thread docs/src/whatsnew/latest.rst Outdated
except for mo_pack.
(:issue:`6832`, :pull:`6976`)

#. `@SgtVarmint` _ migrated codebase from os.path to pathlib.Path where possible
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few syntax corrections:

Suggested change
#. `@SgtVarmint` _ migrated codebase from os.path to pathlib.Path where possible
#. `@SgtVarmint`_ migrated codebase from ``os.path`` to ``pathlib.Path`` where possible

Comment thread lib/iris/fileformats/netcdf/saver.py Outdated
# Given a filepath string/path : create a dataset from that
try:
self.filepath = os.path.abspath(filename)
self.filepath = Path(filename)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Prefer absolute path here?

Suggested change
self.filepath = Path(filename)
self.filepath = Path(filename).absolute()

Comment on lines +1369 to +1370
if file_source.startswith("https:"):
self._filename = file_source
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why were these lines added?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

There was a test (lib/iris/tests/test_load.py::TestOPeNDAP::test_net_cdf_dataset_call) that runs using a web URL. Since Path() strips all double-slashes from the input string, any web URLs would fail. The lines I added were just a check whether or not the input string was a URL and set self._filename accordingly.

I'm open to any suggestions or modifications to this logic or to learning whether my assumption there is wrong.

# len == 23
path = "0000000000000000jan0000"

msg = "ABFField expects a filename of 24 characters: {}".format(path)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Prefer f-strings:

Suggested change
msg = "ABFField expects a filename of 24 characters: {}".format(path)
msg = f"ABFField expects a filename of 24 characters: {path}"

fromfile.assert_called_once_with(path, dtype=">u1")
assert read.call_count == 1

def test_invalid_name_length(self, mocker):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the extra test 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Fully adopt pathlib.PurePath for file paths

3 participants