diff --git a/flopy4/singledispatch/__init__.py b/flopy4/singledispatch/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/flopy4/singledispatch/plot.py b/flopy4/singledispatch/plot.py new file mode 100644 index 0000000..bb7879e --- /dev/null +++ b/flopy4/singledispatch/plot.py @@ -0,0 +1,8 @@ +from functools import singledispatch + + +@singledispatch +def plot(obj, **kwargs): + raise NotImplementedError( + "plot method not implemented for type {}".format(type(obj)) + ) diff --git a/flopy4/singledispatch/plot_int.py b/flopy4/singledispatch/plot_int.py new file mode 100644 index 0000000..6da9e6a --- /dev/null +++ b/flopy4/singledispatch/plot_int.py @@ -0,0 +1,6 @@ +from flopy4.singledispatch.plot import plot + + +@plot.register +def _(v: int, **kwargs): + print(f"Plotting a model with kwargs: {kwargs}") diff --git a/pyproject.toml b/pyproject.toml index 2260648..517f0e4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -108,3 +108,6 @@ ignore = [ "E722", # do not use bare `except` "E741", # ambiguous variable name ] + +[project.entry-points.flopy4] +plot = "flopy4.singledispatch.plot_int" diff --git a/test/test_singledispatch.py b/test/test_singledispatch.py new file mode 100644 index 0000000..2a98597 --- /dev/null +++ b/test/test_singledispatch.py @@ -0,0 +1,12 @@ +from importlib.metadata import entry_points + +from flopy4.singledispatch.plot import plot + + +def test_register_singledispatch_with_entrypoints(): + eps = entry_points(group="flopy4", name="plot") + for ep in eps: + _ = ep.load() + + # should not throw an error, because plot_int was loaded via entry points + plot(5)