Skip to content

Commit

Permalink
refactor(expired deprecation): raise AttributeError with to_shapefile (
Browse files Browse the repository at this point in the history
…#2200)

This PR changes deprecated .to_shapefile() functions to raise AttributeError with a helpful message.

These are the affected functions:

    flopy.utils.util_list.MfList.to_shapefile
    flopy.pakbase.Package.to_shapefile
    flopy.mbase.BaseModel.to_shapefile

While these functions have been deprecated since 3.2.4 released in 2016, they were never documented as such. Thus a helpful exception message and cross-referenced docstring is used instead of removing these functions.
  • Loading branch information
mwtoews authored May 30, 2024
1 parent 388ac8d commit 31955a7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 93 deletions.
18 changes: 18 additions & 0 deletions autotest/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -2061,3 +2061,21 @@ def test_vtk_export_disu_model(function_tmpdir):
strt_vtk = vtk_to_numpy(grid.GetCellData().GetArray("strt"))
if not np.allclose(gwf.ic.strt.array, strt_vtk):
raise AssertionError("'strt' array not written in proper node order")


def test_to_shapefile_raises_attributeerror():
# deprecated 3.2.4, changed to raise AttributeError version 3.8
# these attributes and this test may eventually be removed
m = flopy.modflow.Modflow()
assert isinstance(m, flopy.mbase.BaseModel)
with pytest.raises(AttributeError, match="was removed"):
m.to_shapefile("nope.shp")
dis = flopy.modflow.ModflowDis(m)
assert isinstance(dis, flopy.pakbase.Package)
with pytest.raises(AttributeError, match="was removed"):
dis.to_shapefile("nope.shp")
wel = flopy.modflow.ModflowWel(m)
spd = wel.stress_period_data
assert isinstance(spd, flopy.utils.MfList)
with pytest.raises(AttributeError, match="was removed"):
spd.to_shapefile("nope.shp", kper=1)
32 changes: 4 additions & 28 deletions flopy/mbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -1703,34 +1703,10 @@ def plot(self, SelPackList=None, **kwargs):
)
return axes

def to_shapefile(
self, filename: Union[str, os.PathLike], package_names=None, **kwargs
):
"""
Wrapper function for writing a shapefile for the model grid. If
package_names is not None, then search through the requested packages
looking for arrays that can be added to the shapefile as attributes
Parameters
----------
filename : str or PathLike
Path of the shapefile to write
package_names : list of package names (e.g. ["dis","lpf"])
Packages to export data arrays to shapefile. (default is None)
Returns
-------
None
Examples
--------
>>> import flopy
>>> m = flopy.modflow.Modflow()
>>> m.to_shapefile('model.shp', SelPackList)
"""
warnings.warn("to_shapefile() is deprecated. use .export()")
self.export(filename, package_names=package_names)
def to_shapefile(self, *args, **kwargs):
"""Raises AttributeError, use :meth:`export`."""
# deprecated 3.2.4, changed to raise AttributeError version 3.8
raise AttributeError(".to_shapefile() was removed; use .export()")


def run_model(
Expand Down
35 changes: 4 additions & 31 deletions flopy/pakbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,37 +838,10 @@ def plot(self, **kwargs):
axes = PlotUtilities._plot_package_helper(self, **kwargs)
return axes

def to_shapefile(self, filename, **kwargs):
"""
Export 2-D, 3-D, and transient 2-D model data to shapefile (polygons).
Adds an attribute for each layer in each data array
Parameters
----------
filename : str
Shapefile name to write
Returns
----------
None
See Also
--------
Notes
-----
Examples
--------
>>> import flopy
>>> ml = flopy.modflow.Modflow.load('test.nam')
>>> ml.lpf.to_shapefile('test_hk.shp')
"""
import warnings

warnings.warn("to_shapefile() is deprecated. use .export()")
self.export(filename)
def to_shapefile(self, *args, **kwargs):
"""Raises AttributeError, use :meth:`export`."""
# deprecated 3.2.4, changed to raise AttributeError version 3.8
raise AttributeError(".to_shapefile() was removed; use .export()")

def webdoc(self):
"""Open the web documentation."""
Expand Down
38 changes: 4 additions & 34 deletions flopy/utils/util_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,40 +998,10 @@ def plot(

return axes

def to_shapefile(self, filename, kper=None):
"""
Export stress period boundary condition (MfList) data for a specified
stress period
Parameters
----------
filename : str
Shapefile name to write
kper : int
MODFLOW zero-based stress period number to return. (default is None)
Returns
----------
None
See Also
--------
Notes
-----
Examples
--------
>>> import flopy
>>> ml = flopy.modflow.Modflow.load('test.nam')
>>> ml.wel.to_shapefile('test_hk.shp', kper=1)
"""
import warnings

warnings.warn(
"Deprecation warning: to_shapefile() is deprecated. use .export()"
)
self.export(filename, kper=kper)
def to_shapefile(self, *args, **kwargs):
"""Raises AttributeError, use :meth:`export`."""
# deprecated 3.2.4, changed to raise AttributeError version 3.8
raise AttributeError(".to_shapefile() was removed; use .export()")

def to_array(self, kper=0, mask=False):
"""
Expand Down

0 comments on commit 31955a7

Please sign in to comment.