-
Notifications
You must be signed in to change notification settings - Fork 1
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
Checking function inputs (landlab/landlab#1223) #33
Comments
@DavidLitwin The Pythonic way of doing it would be to try to call the supplied callback function and handling the error if the callback function doesn't have the correct number of arguments. Another solution would be to examine the signature of the callback using For the first method, your code would look something like, try:
callback_function(grid, recharge_rate, substep_dt, **kwargs)
except TypeError as error:
# do something to handle the error You have to be careful here though as you don't want to hide an unhandled For the second method, your code might include something like, parameters = inspect.signature(callback_function).parameters
n_args = 0
for p in parameters.values():
if p.kind in (p. POSITIONAL_ONLY, p.POSITIONAL_OR_KEYWORD) and p.default is p.empty):
n_args += 1 There are however cases where the above wouldn't work. For example, if the signature of the callback looked like I think the easiest, and most straightforward (and pythonic!) is probably the first method. |
@mcflugen Thank you for your thoughts on this! I think I will go with the first option. I do have a few questions though.
|
I want to change the callback function in the GroundwaterDupuitPercolator to have the form:
callback_function(grid, recharge_rate, substep_dt, **kwargs)
. Presently it has the formcallback_function(grid, substep_dt, **kwargs)
. @kbarnhart suggested that the pr should include testing the number of arguments in the user-provided function. This seems tricky given that the user will likely provide additional keyword arguments (e.g. the folder and file location to write an output) that confuse most of the methods I've seen that count the number of arguments a function takes. Is there a simple solution to this that can correctly ensure three (or two, giving a depricated warning) non-keyword arguments are supplied?The text was updated successfully, but these errors were encountered: