ImperialCollegeLondon / pyrealm

Development of the pyrealm package, providing an integrated toolbox for modelling plant productivity, growth and demography using Python.
https://pyrealm.readthedocs.io/
MIT License
21 stars 8 forks source link

Incorrect display of function/class argument types in documentation. #262

Open j-emberton opened 3 months ago

j-emberton commented 3 months ago

Describe the bug Incorrect display of function/class argument types in documentation.

Expected behavior I would expect to just see a display of arguments with a simple type e.g. my function(arg1: NDArray)

Screenshots

Screenshot 2024-07-17 at 15 07 25

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

j-emberton commented 3 months ago

Possible solution:

Create a python file with the following contents and add to sphinx project:

`# sphinx_initvar_filter.py from sphinx.ext.autodoc import ClassDocumenter, ModuleLevelDocumenter from sphinx.util.inspect import getdoc from sphinx.util import logging

logger = logging.getLogger(name)

class InitVarFilterDocumenter(ClassDocumenter): def add_directive_header(self, sig): self.options.noindex = True super().add_directive_header(sig) self.options.noindex = False

def filter_members(self, members, want_all):
    filtered_members = []
    for name, obj in members:
        if not name.startswith('__') and 'InitVar' not in str(obj):
            filtered_members.append((name, obj))
        else:
            logger.info(f'Filtering out: {name} ({obj})')
    return filtered_members

def setup(app): app.add_autodocumenter(InitVarFilterDocumenter)`

Then modify conf.py to include the custom operation:

`# conf.py extensions = [ 'sphinx.ext.autodoc', 'sphinx_autodoc_typehints', 'sphinx.ext.napoleon', 'sphinx_initvar_filter' # Add your custom extension here ]

autodoc_typehints = "description" autodoc_typehints_format = "short"`

j-emberton commented 3 months ago

Hasn't been tried and tested yet

j-emberton commented 3 months ago

also more in pressure submodule:

Screenshot 2024-07-18 at 14 22 46
davidorme commented 3 months ago

I suspect this might be linked in to the state of our nitpick_ignore settings in the sphinx conf.py, which is a total dumpster fire of things that sphinx moans about.

https://github.com/ImperialCollegeLondon/pyrealm/blob/84e005d0112faf29b67376e27e97ac85c000022a/docs/source/conf.py#L91-L143

davidorme commented 3 months ago

This looks relevant: https://stackoverflow.com/questions/67473396/shorten-display-format-of-python-type-annotations-in-sphinx

j-emberton commented 3 months ago

Nice find.

is there a simple way to build the docs locally to test the changes?

davidorme commented 3 months ago

Should be able just to do cd docs; poetry run make html. All the requirements for docs are included in the poetry requirements. It doesn't seem to work though 😢