Working on #2715 I realised that there might be parts of the codebase where we want to force keyword only API.
Here are some code samples to show what keyword-only arguments are:
# current code
def from_sgrid_conventions(
ds, mesh, vector_fields
):
pass
ds = ...
mapping = ...
from_sgrid_conventions(ds, "flat", mapping)
# passes
def from_sgrid_conventions(
ds, *, mesh, vector_fields # the `*` forces all subsequent arguments to be called by keyword=
):...
ds = ...
mapping = ...
from_sgrid_conventions(ds, "flat", mapping)
# Traceback (most recent call last):
# File "/Users/Hodgs004/coding/repos/parcels/t.py", line 22, in <module>
# from_sgrid_conventions(ds, "flat", mapping)
# ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
#TypeError: from_sgrid_conventions() takes 1 positional argument but 3 were given
from_sgrid_conventions(ds, mesh="flat", vector_fields=mapping)
# passes
.
In the case of the Fieldset.from_... constructor methods I find that using keyword arguments is much more readable and less error prone. Keyword only arguments also has the very significant benefit of allowing us to seamlessly make API changes. If we want to remove mesh as an argument down the line we can just see if the user uses it and raise a warning. This becomes quite difficult if they provide this as a positional argument.
I wonder as well if other parts of the codebase can benefit from keyword only arguments - something to discuss more @erikvansebille before 4.x release
Working on #2715 I realised that there might be parts of the codebase where we want to force keyword only API.
Here are some code samples to show what keyword-only arguments are:
.
In the case of the
Fieldset.from_...constructor methods I find that using keyword arguments is much more readable and less error prone. Keyword only arguments also has the very significant benefit of allowing us to seamlessly make API changes. If we want to removemeshas an argument down the line we can just see if the user uses it and raise a warning. This becomes quite difficult if they provide this as a positional argument.I wonder as well if other parts of the codebase can benefit from keyword only arguments - something to discuss more @erikvansebille before 4.x release