Skip to content

SUGGESTION: enforce docstring consistency in PyRIT with Ruff #1176

@paulinek13

Description

@paulinek13

Related issue: #653

In short, this issue proposes adding Ruff as a dev dependency to enforce documentation consistency in PyRIT.

Why

PyRIT currently lacks automated verification of docs consistency.
The proposed solution will automatically check for the docstring requirements defined in the project's style guide (.github/instructions/style-guide.instructions.md), including:

- Google-style docstring format
- Type information in parameter descriptions
- Documented return values
- Documented exceptions (Raises sections)
- Triple quotes for all docstrings

It will also ensure that, for example, new public classes/methods/functions actually are documented to prevent missing entries in the docs.

Of course, things like that can be manually reviewed during PR reviews, but this could be easily automated + this change makes it now easier in fixing #653, I think.

Proposed Solution

As stated above, the proposal is to add Ruff as a new dev dependency, configure it properly to match as much of the existing style as possible, and add ruff check pyrit/ to the .pre-commit-config.yaml.

https://pypi.org/project/ruff/
https://docs.astral.sh/ruff/

In future ruff could replace flake8/isort/pylint in PyRIT, but for now, I'm proposing to add it just for docs checks.

More info

ruff check pyrit\ takes about 200-600 ms, so it's super fast.

Currently, there are about 1700 "errors":

Image

config I've used:

[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.ruff.lint]
preview = true
select = [
    "D",  # https://docs.astral.sh/ruff/rules/#pydocstyle-d
    "DOC"  # https://docs.astral.sh/ruff/rules/#pydoclint-doc
]
ignore = [
    "D100",  # Missing docstring in public module
    "D200",  # One-line docstring should fit on one line
    "D205",  # 1 blank line required between summary line and description
    "D212",  # Multi-line docstring summary should start at the first line
    "DOC502",  # Raised exception is not explicitly raised: {id}
]
extend-select = [
    "D204",  # 1 blank line required after class docstring
    "D213",  # Multi-line docstring summary should start at the second line
    "D401",  # First line of docstring should be in imperative mood: "{first_line}"
    "D404",  # First word of the docstring should not be "This"
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions