Use dataclasses to store and document parameters #68
Labels
code quality
documentation
Improvements or additions to documentation
enhancement
New feature or request
Use python dataclasses instead of dicts to store and use the various large sets of parameters required in the Hypnos workflow.
Documentation here: https://docs.python.org/3/library/dataclasses.html
Dataclasses provide a convenient place for us to document the required parameters and options in the dataclass docstring, as well as add type hints, in a much more convient format than the equivalent standard class object init we would need to write/maintain. Dataclasses can be quickly instantiated from dictionaries by unpacking (i.e.
DataClass(**dict)
) assuming the keys match the args.For example:
Another benefit is the
__post_init__
method, which gives us a sensible place to compute derived parameters automatically upon instantiation. It is especially useful to do this when multiple components of an assembly can access the same dataclass instance for their parameters.Instantiating the classes would be relatively seamless from the JSON format, with something like this in the
MyAssembly.__init__()
:Or to go a step further, we could have the components/assemblies ask for their corresponding dataclasses as arguements, which would allow readers of the code to easily find the dataclass docstrings which describe the parameters required by a component. (This would also enable things like IDE autocompletion for developers).
The existing ease of instantiation from json could be maintained with a json classmethod which unpacks the file, and keeping dict as a valid arg type.
Something like:
An alternate version of the above code block, which avoids the variable type args would be:
The text was updated successfully, but these errors were encountered: