ecmwf-ifs / loki

Freely programmable source-to-source translation for Fortran
https://sites.ecmwf.int/docs/loki/
Apache License 2.0
29 stars 12 forks source link

Passing no config to Scheduler leads to runtime error. #373

Open wertysas opened 2 weeks ago

wertysas commented 2 weeks ago

The default argument for the config in the Scheduler leads to a runtime error. However, the documentation of the Scheduler class says that the config parameter is optional.

It seems to me that an easy fix could be to change the default argument from None to {}

reuterbal commented 2 weeks ago

Thanks, that's a good observation. Unfortunately, your proposed solution would bear dangerous side effects because using a dict as the default argument makes it effectively a global variable, see for example:

>>> def routine_with_opt_arg(arg={}):
...     arg['test'] = arg.get('test', 0) + 1
...     print(arg)
...
>>> routine_with_opt_arg()
{'test': 1}
>>> routine_with_opt_arg()
{'test': 2}
>>> routine_with_opt_arg()
{'test': 3}

The correct pattern for this is instead to add a manual check to the constructor:

if config is None:
    config = {}