PyDMD icon indicating copy to clipboard operation
PyDMD copied to clipboard

Cleaning the constructor of `DMDBase` subclasses

Open fandreuz opened this issue 3 years ago • 2 comments

Every time a new subclass of DMDBase is created there is a lot of recurrent documentation to be copied from another instance to the new one, especially that for __init__(). It might be interested to use the same approach of other libraries (e.g. Matplotlib) and redirect the user to some base class (DMDBase) where the documentation for all the parameters available is stored.

Otherwise we may also store the documentation in a separate class DMDAttributes to reduce the lines dedicated to docstrings in dmdbase.py.

Here are some proposals for the new structure of the constructor:

**kwargs

def __init__(self, **kwargs):
    """
    ...
    Refer to :func:`DMDBase.__init__` for the available parameters.
    """

    self._svd_rank = kwargs['svd_rank']
    ...
  • :white_check_mark: Backward compatible
  • :x: No way to set shared default values
  • :x: No suggestions in IDE

**kwargs + default dict

def __init__(self, **kwargs):
    """
    ...
    Refer to :func:`DMDBase.__init__` for the available parameters.
    """

    attributes_dict = self.get_default_attributes()
    attributes_dict.update(kwargs)

    self._svd_rank = attributes_dict['svd_rank']
    ...
  • :white_check_mark: Backward compatible
  • :white_check_mark: Default values
  • :x: No suggestions in IDE

DMDAttributes

def __init__(self, dmd_attributes):
    """
    ...
    Refer to :func:`DMDBase.__init__` for the available parameters.
    """

    self._svd_rank = dmd_attributes['svd_rank']
    ...
  • :white_check_mark: Default values
  • :white_check_mark: Suggestions in IDE
  • :x: Not backward compatible

DMDAttributes + **kwargs + default dict

def __init__(self, dmd_attributes, *, **kwargs):
    """
    ...
    Refer to :func:`DMDBase.__init__` for the available parameters.
    """

    attributes_dict = self.get_default_attributes()
    attributes_dict.update(dmd_attributes.as_dict())
    attributes_dict.update(kwargs)

    self._svd_rank = attributes_dict['svd_rank']
    ...
  • :white_check_mark: Backward compatible
  • :white_check_mark: Suggestions in IDE if attributes are passed via DMDAttributes
  • :white_check_mark: Default values
  • :x: No suggestions in IDE for kwargs

Note: the star * in the constructor (before **kwargs) is needed because we do not want anything to be given as a non-keyword argument in this kind of setting.

fandreuz avatar Jan 22 '22 08:01 fandreuz

See also __doc__ attribute (https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html).

fandreuz avatar Jan 31 '22 11:01 fandreuz

Dataclases are now very used in the Python environment and there's some tooling we could leverage (e.g. Pydantic).

fandreuz avatar Jul 30 '23 22:07 fandreuz