scipp / essreduce

Common functionality for ESS data reduction
https://scipp.github.io/essreduce/
BSD 3-Clause "New" or "Revised" License
1 stars 1 forks source link

Type aliases and descriptions for parameters #63

Open SimonHeybrock opened 3 months ago

SimonHeybrock commented 3 months ago

Problem: When using NewType or the Python 3.12 type alias, docstrings don't work. Sphinx extract the description from the comment after the alias definition, but for generating parameters for widgets we would like to use __doc__. Currently we have, e.g.,

BackgroundRun = NewType('BackgroundRun', int)
"""Background run: the run with only the solvent which the sample is placed in."""

I now tried the following:

def alias(name: str, tp: type, doc: str) -> type:
    ntp = NewType(name, tp)
    ntp.__doc__ = doc
    return ntp

BackgroundRun = alias(
    'BackgroundRun',
    int,
    "Background run: the run with only the solvent which the sample is placed in.",
)

Unless some Sphinx cache tricked be, this seems to work with both Sphinx autodoc and defined a usable __doc__.