Is there a way to validate that methods and attributes listed in a Python class docstring exist within the class?

27 views Asked by At

When writing docstrings for a class I wondered if there was a Python package that would parse the doc string of a class and validate that the class has the methods and attributes listed?

My motivation is for something that could be used as part of CI/CD pipeline to check that new code is documented correctly. PEP 257 does seem to suggest that class methods should be listed in the docstring, but I know listing the methods might be a contentious topic.

Consider my example class below :

"""An example module to ask a question on SO."""


class ExampleClass:
    """
    An example class.

    Attrs:
    ------------
    typo_ : str
        mistake I made

    Methods
    --------------
    my_method()
        method which doesn't exist

    """

    typo: str

    def my_methd(self) -> None:
        """Typo in the name."""
        return



Note that the attribute and method I define are mistyped in the docstring as a example of what I am trying to catch.

I have tried the following packages (which all pass without detecting any problems):

  • Interrogate (1.5.0)
  • pydocstyle(6.3.0)
  • pydoctest (0.1.22)
  • ruff (0.2.2)
  • darglint2 (1.8.2)
  • docsig (0.44.2)
  • pydoclint (0.4.1)

(Note that I wanted to include links to these packages but SO prevented me from posting this question with the links in place)

Then the following package which does catch the fact that the typo attribute is not described in the docstring (it points me to this:

  • flake8-docstring-complete (1.3.0)

So I get halfway to my goal, but have yet to find anything that checks class methods. Does anybody know of any ways to achieve what I am looking to do?

0

There are 0 answers