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

fix/normalize storage paths #2384

Merged
merged 11 commits into from
Oct 16, 2024
Merged

fix/normalize storage paths #2384

merged 11 commits into from
Oct 16, 2024

Conversation

d-v-b
Copy link
Contributor

@d-v-b d-v-b commented Oct 15, 2024

This PR adds some of the path normalization logic from zarr v2 into v3. I largely copied this function as-is with a few tweaks. That function is actually not terribly strict as far as path parsing is concerned, so I'm open to requests that we make it stricter. The main thing to note is that normalize_path (the new name for that function) strips leading / characters from strings, which ensures that paths are relative.

I wired this function up to make_store_path, which previously didn't take path as a parameter (now it does). This led to some nice code deletion opportunities in our various creation routines.

Something that always bugged me in zarr v2 was use of None as a default value for types where there's already a default value. In the case of strings, the empty string '' is a perfectly good default value, and so allowing None is just noise IMO. I was tempted to try and remove None as a valid path argument in this PR, but I held back. If people agree that None is a silly default for a stringy value like path when we can just use '' instead, then I can make that change as well.

Fixes #2357

TODO:

  • Add unit tests and/or doctests in docstrings
  • Add docstrings and API docs for any new/modified user-facing classes and functions
  • New/modified features documented in docs/tutorial.rst
  • Changes documented in docs/release.rst
  • GitHub Actions have all passed
  • Test coverage is 100% (Codecov passes)

src/zarr/storage/common.py Outdated Show resolved Hide resolved
tests/v3/test_api.py Outdated Show resolved Hide resolved
Copy link
Member

@jhamman jhamman left a comment

Choose a reason for hiding this comment

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

Good stuff @d-v-b - I'm glad we're moving path into make_store_path.

Copy link
Contributor

@TomAugspurger TomAugspurger left a comment

Choose a reason for hiding this comment

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

Thanks, a couple questions about the implementation.

One meta-comment I've been thinking about, probably for a future PR: we could define PathLike as a NewType. The user-facing API could accept str | PathLike, and internally we could use just PathLike. Then we can verify with mypy that we've normalized all paths provided by the user.

src/zarr/storage/_utils.py Show resolved Hide resolved
src/zarr/storage/_utils.py Outdated Show resolved Hide resolved
src/zarr/storage/_utils.py Outdated Show resolved Hide resolved
src/zarr/storage/_utils.py Show resolved Hide resolved
@d-v-b d-v-b merged commit 29246d6 into main Oct 16, 2024
25 checks passed
@d-v-b d-v-b deleted the fix/normalize-storage-paths branch October 16, 2024 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Paths with leading slashes do bad things
3 participants