jsh9 / pydoclint

A Python docstring linter that checks arguments, returns, yields, and raises sections
https://pypi.org/project/pydoclint/
MIT License
151 stars 15 forks source link

Check for missing or incorrectly formatted docstring #128

Closed cidrblock closed 6 months ago

cidrblock commented 6 months ago

Hey there, first thanks for pydoclint, it's been super handy across our projects.

I'm not sure if this is in scope for pydoclint or not, but I've noticed a couple cases where I would expect to see an error but none was generated. (This may be outside the scope of what you want pydoclint to do)

Given the following pyproject.toml file:


[tool.pydoclint]
allow-init-docstring = true
arg-type-hints-in-docstring = false
check-return-types = false
style = 'google'

Running against the following file:

"""This is a module doc string."""

def test_missing(foo: str, bar: int) -> None:
    """This is a doc string."""
    pass

def test_wrong_format(foo: str, bar: int) -> str:
    """This is a doc string.

    :param foo: This is a parameter doc string.
    :param bar: This is a parameter doc string.
    :return: This is a return doc string.
    """
    return "foo"

def test_missing_args(foo: str, bar: int) -> str:
    """This is a doc string.

    Args:
        foo: This is a parameter doc string.

    Returns:
        This is a return doc string.
    """
    return "foo"

These are the errors reported:

(venv) m910x ➜  pydoclint-bug pydoclint sample.py
Loading config from user-specified .toml file: pyproject.toml
Found options defined in pyproject.toml:
{'allow_init_docstring': True, 'arg_type_hints_in_docstring': False, 'check_return_types': False, 'style': 'google'}
Skipping files that match this pattern: \.git|\.tox
sample.py
sample.py
    19: DOC101: Function `test_missing_args`: Docstring contains fewer arguments than in function signature. 
    19: DOC103: Function `test_missing_args`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [bar: int].

It appears that pydoclint correctly identifies the missing argument in the third function but reports no errors in the first 2.

Would it be possible to:

1) Raise an error when none of the arguments are present in the docstring? (function 1) 2) Raise an error when the docstring has an icorrect format ? (function2) This might be covered by number 1 depending on how it is implemented

Thanks again!

-Brad

cidrblock commented 6 months ago

If I took the time to read the docs......

It seems I'm missing a critical part of the configuration:

skip-checking-short-docstrings = false

[tool.pydoclint]
allow-init-docstring = true
arg-type-hints-in-docstring = false
check-return-types = false
skip-checking-short-docstrings = false
style = 'google'

The following is then generated:

4: DOC101: Function `test_missing`: Docstring contains fewer arguments than in function signature. 
    4: DOC103: Function `test_missing`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [bar: int, foo: str].
    9: DOC101: Function `test_wrong_format`: Docstring contains fewer arguments than in function signature. 
    9: DOC103: Function `test_wrong_format`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [bar: int, foo: str].
    9: DOC201: Function `test_wrong_format` does not have a return section in docstring 
    19: DOC101: Function `test_missing_args`: Docstring contains fewer arguments than in function signature. 
    19: DOC103: Function `test_missing_args`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [bar: int].

Sorry to have bothered you!

-Brad