Expand ~ in file names, like pandas and xarray - #1035
Open
ecomodeller wants to merge 3 commits into
Open
Conversation
MIKE IO passed file names straight through to mikecore without expanding a leading ~, so paths like "~/data/wl.dfs0" did not work. pandas and xarray both expand ~ on read and write, and MIKE IO aims to be consistent with them. Reads failed with a FileNotFoundError naming the unexpanded "~/..." path, which was at least diagnosable. Writes were worse: Dataset.to_dfs() silently created a directory literally named "~" in the current working directory and wrote the file there, so nothing landed in the user's home directory and no error was raised. Add mikeio._path.normalize_path() and apply it at the public API boundaries: - mikeio.open/read via the Dfs0/Dfs1/Dfs2/Dfs3 constructors, _get_dfsu_info (Dfsu2DH, DfsuLayered, DfsuSpectral), the dfsu factory and Mesh - Dataset.to_dfs and DataArray.to_dfs, and the write_dfs0/1/2/3 and write_dfsu functions - Mesh.write, GeometryFM2D.to_mesh, GeometryFM3D.to_mesh, Grid2D.to_mesh - read_pfs and PfsDocument.write - every generic.py function: scale, fill_corrupt, sum, add, diff, concat, extract, avg_time, quantile, change_datatype, transform - extract_track when given a file name normalize_path is expanduser(fspath(...)), so it is a no-op for paths without a leading ~ and is idempotent. Since it returns a str, several now-redundant str(filename) wrappers are dropped. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Member
Author
|
@copilot is this behaviour and implementation similar to pandas |
Contributor
Yes — this is aligned with pandas. pandas’ |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MIKE IO passes file names straight through to
mikecorewithout expanding a leading~, so~/data/wl.dfs0doesn't work. pandas and xarray both expand~on read and write, and MIKE IO aims to be consistent with them.Reads fail with a
FileNotFoundErrornaming the unexpanded path — annoying, but diagnosable:Writes are worse — they fail silently.
Dataset.to_dfscreates a directory literally named~in the current working directory and writes the file there. Nothing lands in$HOMEand no error is raised:A
~directory inside a repo is also a nuisance to clean up —rm -rf ~is exactly the command you don't want someone reaching for.The failure mode was inconsistent across formats, too:
PfsDocument.writeandMesh.writeuse Python'sopen()/write_text()and so did raiseFileNotFoundError, while the dfs writers went throughmikecoreand silently mis-wrote.Fix
Add
mikeio._path.normalize_path()—expanduser(fspath(...)), mirroring pandas'stringify_path— and apply it at the public API boundaries:mikeio.open/mikeio.read, via theDfs0/Dfs1/Dfs2/Dfs3constructors,_get_dfsu_info(Dfsu2DH,DfsuLayered,DfsuSpectral), the dfsu factory, andMeshDataset.to_dfsandDataArray.to_dfs, pluswrite_dfs0/1/2/3andwrite_dfsuMesh.write,GeometryFM2D.to_mesh,GeometryFM3D.to_mesh,Grid2D.to_meshread_pfsandPfsDocument.writegeneric.pyfunction:scale,fill_corrupt,sum,add,diff,concat,extract,avg_time,quantile,change_datatype,transformextract_trackwhen given a file namenormalize_pathis a no-op for paths without a leading~and is idempotent, so this is backwards-compatible. Because it returns astr, a number of now-redundantstr(filename)wrappers are dropped.Only
expanduseris applied — notabspath. pandas does the same, and it keeps_filename(which shows up inrepr) as the user wrote it.Tests
New
tests/test_path_expansion.py(35 tests) points$HOMEat atmp_pathviamonkeypatchand covers read, open, write, mesh, pfs, track, and everygenericfunction. Each write test also asserts no literal~directory is created.Verified these are not vacuous: 32 of the 35 fail on
main.Full suite: 878 passed.
mypyclean,ruff checkshows only the 3 pre-existing errors onmain.🤖 Generated with Claude Code