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

feat(pathlike): support pathlike throughout public interface #1730

Merged
merged 10 commits into from
Mar 3, 2023

Conversation

wpbonelli
Copy link
Member

@wpbonelli wpbonelli commented Feb 23, 2023

Working toward #1729, continuation of #1712, #1603, and #1583

  • update code where needed to support os.Pathlike in user-facing API
  • update docstrings and type hints
  • update most tests to use Path instead of str
  • add tests checking both Path and str for core classes/utilities
  • addverbose flag to more utility methods to optionally enable/suppress output
  • update some notebooks to use pathlib, but not all
  • add sim_path property to MFSimulation as a shortcut to simulation_data.mfpath.get_sim_path()
    • guaranteed Path whether user provides workspace as str or PathLike
    • update tests and mf6 tutorial notebook to exercise/demonstrate
  • preserve str properties where this was already the convention
    • e.g. BaseModel.model_ws
    • could consider refactoring to Path in future, but may break usages
  • update resolve_exe function, add tests, and use it from both BaseModel as well as run_model()

@codecov
Copy link

codecov bot commented Feb 23, 2023

Codecov Report

Merging #1730 (9ac6f66) into develop (cf3a517) will increase coverage by 0.1%.
The diff coverage is 82.0%.

@@            Coverage Diff            @@
##           develop   #1730     +/-   ##
=========================================
+ Coverage     71.9%   72.1%   +0.1%     
=========================================
  Files          253     253             
  Lines        56004   56043     +39     
=========================================
+ Hits         40282   40416    +134     
+ Misses       15722   15627     -95     
Impacted Files Coverage Δ
flopy/plot/map.py 82.3% <ø> (ø)
flopy/utils/formattedfile.py 88.8% <ø> (-0.2%) ⬇️
flopy/utils/compare.py 40.7% <42.8%> (+10.3%) ⬆️
flopy/utils/zonbud.py 66.7% <66.6%> (-0.1%) ⬇️
flopy/export/utils.py 65.3% <72.3%> (+0.1%) ⬆️
flopy/mbase.py 69.7% <81.9%> (+0.4%) ⬆️
flopy/utils/datafile.py 74.0% <83.3%> (+0.1%) ⬆️
flopy/utils/modpathfile.py 86.0% <83.3%> (+<0.1%) ⬆️
flopy/utils/gridgen.py 84.4% <88.8%> (+0.1%) ⬆️
flopy/export/shapefile_utils.py 90.0% <90.0%> (-0.3%) ⬇️
... and 29 more

Copy link
Contributor

@mwtoews mwtoews left a comment

Choose a reason for hiding this comment

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

Thanks, looks good so far, see following comments and suggestions. Are there a few representative tests that use str remaining?

flopy/export/utils.py Outdated Show resolved Hide resolved
flopy/mbase.py Outdated Show resolved Hide resolved
flopy/mbase.py Outdated Show resolved Hide resolved
flopy/utils/zonbud.py Outdated Show resolved Hide resolved
flopy/export/netcdf.py Outdated Show resolved Hide resolved
@wpbonelli wpbonelli force-pushed the support-pathlike branch 2 times, most recently from 38f4499 to 10cc5a0 Compare February 24, 2023 03:50
@wpbonelli
Copy link
Member Author

Added/expanded a few tests covering both str and Path for Modflow and MFSimulation read/write, gridgen, some export utilities. Planning to add a few more.

@wpbonelli wpbonelli force-pushed the support-pathlike branch 3 times, most recently from f09aba2 to 1b4102d Compare February 27, 2023 16:07
@wpbonelli wpbonelli marked this pull request as ready for review February 27, 2023 17:39
@wpbonelli wpbonelli force-pushed the support-pathlike branch 6 times, most recently from c5e98f3 to d0b83c6 Compare February 28, 2023 16:22
Copy link
Contributor

@mwtoews mwtoews left a comment

Choose a reason for hiding this comment

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

Nice stuff, getting closer, see attached suggestions/comments. I'm primarily looking at the main module, and have not seen many of the edits to notebooks or autotests.

flopy/utils/compare.py Show resolved Hide resolved
flopy/utils/compare.py Show resolved Hide resolved
flopy/utils/compare.py Show resolved Hide resolved
flopy/utils/compare.py Show resolved Hide resolved
flopy/utils/compare.py Show resolved Hide resolved
flopy/mbase.py Show resolved Hide resolved
flopy/mbase.py Show resolved Hide resolved
flopy/mbase.py Show resolved Hide resolved
flopy/mbase.py Outdated Show resolved Hide resolved
flopy/mf6/mfbase.py Show resolved Hide resolved
Copy link
Contributor

@langevin-usgs langevin-usgs left a comment

Choose a reason for hiding this comment

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

Looks great @w-bonelli. Nice addition. As you mentioned, a few remaining os.path.join remain in notebooks, but we can get those later.

Comment on lines +157 to +163
output_filename = str(
Path(output_filename).expanduser().absolute()
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, why is str needed here?

Copy link
Member Author

Choose a reason for hiding this comment

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

it's not strictly — previously output_filename was a str, switching to Path would require changes to L160 and L523 and I figured it may be safer to keep it a string in case any external usages had similar expectations

@wpbonelli
Copy link
Member Author

@mwtoews thanks for the catches, I think they've all been fixed.

@aleaf aleaf mentioned this pull request Mar 2, 2023
@wpbonelli wpbonelli force-pushed the support-pathlike branch 2 times, most recently from 3db3072 to 008f1c5 Compare March 2, 2023 17:19
Copy link
Contributor

@mwtoews mwtoews left a comment

Choose a reason for hiding this comment

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

Just a few (probably) final comments. And again, I'm only reviewing the changes in flopy/*

flopy/mbase.py Outdated Show resolved Hide resolved
flopy/mbase.py Outdated Show resolved Hide resolved
model_ws="./",
scrub=False,
exe_name: Union[str, os.PathLike],
namefile: Optional[str],
Copy link
Contributor

Choose a reason for hiding this comment

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

Currently this is a required parameter, because it does not have a default e.g. = None.

Copy link
Member Author

Choose a reason for hiding this comment

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

The docstring indicated namefile=None could be used for models that don't require namefiles — the function seems to handle this case OK, just omits it from the args passed to the executable

flopy/export/shapefile_utils.py Outdated Show resolved Hide resolved
flopy/export/shapefile_utils.py Outdated Show resolved Hide resolved
- support pathlike or str paths in public APIs
- add verbose flag to more utility functions
- add sim_path property to MFSimulation
- update some notebooks to use pathlib
- update most autotests to use pathlib
- add unit tests for path arguments
- update docstrings and type hints
@wpbonelli
Copy link
Member Author

Fixed more docstrings/typehints, thanks @mwtoews, also updated exe resolve function introduced in #1727

Copy link
Contributor

@mwtoews mwtoews left a comment

Choose a reason for hiding this comment

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

Thanks, looks good! I only have a minor suggestion remaining. This PR could be merged soon.

autotest/test_grid.py Outdated Show resolved Hide resolved
@wpbonelli wpbonelli merged commit 656751a into modflowpy:develop Mar 3, 2023
@wpbonelli wpbonelli deleted the support-pathlike branch March 3, 2023 20:22
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.

3 participants