Skip to content

Use numpydoc pre-commit #15386

Closed
Closed

Description

@nstarman

What is the problem this feature will solve?

We use numpydoc. But the actual docstrings aren’t checked for correctness.

Describe the desired outcome

Their new pre-commit hook means docstrings are checked as part of the CI.

Additional context

numpy/numpydoc#450

Activity

  1. eerovaher commented on Sep 26, 2023

    @eerovaher
    Member

    Is this supposed to replace docformatter?

  2. nstarman commented on Sep 26, 2023

    @nstarman
    MemberAuthor

    They shouldn't be mutually incompatible, but yes that would be the hope. numpydoc has many more specific checks.

  3. namurphy commented on Dec 27, 2023

    @namurphy
    Contributor

    I tried out numpydoc on PlasmaPy recently and ran into a problem (numpy/numpydoc#519) where the crashed with an exception, rather than noting that the problem was with a specific rule. I'm waiting until this issue gets fixed before adding numpydoc to pre-commit.

    This particular error happens when a line in a See Also section starts with a ~, `~, or |. I ran validate-docstring on Astropy's source code just now and ran into the same issue.

  4. mhvk commented on Dec 27, 2023

    @mhvk
    Contributor

    Ah, that explains what happened at https://github.com/numpy/numpy/pull/25416/files#r1434272271 - I added a comment to your numpydoc issue as well.

  5. pllim commented on Jan 3, 2024

    @pllim
    Member

    But the actual docstrings aren’t checked for correctness.

    What kind of correctness? We already have RTD set to turn warning into errors. Is that not enough?

  6. astrofrog commented on Jan 4, 2024

    @astrofrog
    Member

    This will do things like check that all arguments are documented, etc

  7. tactipus commented on Jan 4, 2024

    @tactipus

    so how can we solve this problem? What is the solution?

  8. nstarman commented on Jan 4, 2024

    @nstarman
    MemberAuthor

    so how can we solve this problem? What is the solution?

    Numpydoc has a pre-commit we can use. Numpydoc uses a config file specifying what error codes to check. We'd want to start by turning off any that we are currently failing then sequentially turn them on and fix the issues.

  9. astrofrog commented on Jan 4, 2024

    @astrofrog
    Member

    Yes we use it in reproject and it has found countless real issues so I am very much in favour of doing this. It is going to be a lot of work to fix issues!

  10. pllim commented on Jan 4, 2024

    @pllim
    Member

    But would this be obsolete with the typing stuff taking over in the future?

  11. nstarman commented on Jan 5, 2024

    @nstarman
    MemberAuthor

    In short, this would not be made obsolete by typing because numpydoc checks docstrings, which typing does not interact with. Longer answer is in 3-4 years kinda because Python is adding the typing.Doc annotation and it will be possible to build descriptive docstrings from type annotations, but even then there will be room for Numpydoc (to check the format of all the sections not built from typing.Doc, e.g. Notes, References, etc.).

  12. namurphy commented on Jan 5, 2024

    @namurphy
    Contributor

    I hadn't heard about typing.Doc but it sounds cool! Here's the draft of PEP 727 which is proposing typing.Doc with a target version of Python 3.13.

  13. nstarman commented on Jan 5, 2024

    @nstarman
    MemberAuthor

    Yeah, I've played around with it and it is interesting. On the downside, basically all function signatures become 1 parameter per line because of the attached string description. On the upsides: 1) docstrings are guaranteed to be in sync with the function arguments; 2) IDEs can surface descriptive information, not just type hints, about a Parameter on hover-over, 3) it's easier to switch docstring formats, like numpy to google (though I'm not sure why one would want to switch, but it is easier).

  14. mhvk commented on Jan 5, 2024

    @mhvk
    Contributor

    Had a look too and really like the idea of doc'd arguments (and class variables, allowing us to remove the sphinx-only docstrings under them). Indeed, I'd go as far as this being something that would actually convince me to use typing myself... (and maybe to have typing_extensions as a required dependency...)

    I think for one-line argument docs, the length issue is OK, but for multi-line I'm less sure. More generally, the really descriptive part of an argument, of how it influences the function/method, may still belong in the docstring -- this may need some thought (sadly).

    Given that using Doc may well lead to a bit of a rethink of docstrings, I think the case for having numpydoc pre-commit is weakened; we don't want to go through such mini-edits multiple times.

  15. pllim commented on Sep 9, 2026

    @pllim
    Member

    Follow-up issue is #20353

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions