`numpydoc lint` reports PR02 for documented dataclass fields

Aperta
#716 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
57/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
tooling

Direzione di ricerca

Start with AstValidator.signature_parameters in numpydoc/hooks/validate_docstrings.py and inspect how the AST validator handles class decorators and AnnAssign nodes. Use the minimal Point reproduction to check PR02 behavior, including the listed dataclass field cases and exclusions. Done when the AST hook accepts documented dataclass constructor fields consistently with the runtime validator, with relevant validation coverage.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Description

The AST-based hook (numpydoc lint / the numpydoc-validation pre-commit hook) reports every documented parameter of a dataclass as unknown (PR02). Following the numpydoc convention, the constructor arguments are documented under Parameters in the class docstring. For a dataclass, those arguments are the fields declared in the class body.

The runtime validator (numpydoc.validate.validate) imports the class, sees the __init__ that dataclass generates, and reports no PR02. So the two validators disagree.

Minimal reproduction

example.py:

"""Minimal example."""

from dataclasses import dataclass


@dataclass
class Point:
    """A point in the plane.

    Parameters
    ----------
    x : float
        The x coordinate.
    y : float
        The y coordinate.
    """

    x: float
    y: float

pyproject.toml:

[tool.numpydoc_validation]
checks = ["all", "EX01", "SA01", "ES01"]
$ numpydoc lint example.py
example.py:7: PR02 Unknown parameters {'y', 'x'}
$ python -c "from numpydoc.validate import validate; print(validate('example.Point')['errors'])"
[('ES01', 'No extended summary found'), ('SA01', 'See Also section not found'), ('EX01', 'No examples section found')]

(numpydoc 1.11.0, Python 3.13.)

Cause

AstValidator.signature_parameters (numpydoc/hooks/validate_docstrings.py) only takes a class's parameters from an explicit def __init__ in the class body. If there is none, the parameter tuple is empty, and every documented parameter becomes PR02. I think this has happened since the class/constructor checks were added to the AST validator in #622.

Proposed fix

When a class has no explicit __init__ and has a dataclass / dataclasses.dataclass decorator (bare, called, or with arguments), build the parameters from the class body's AnnAssign nodes:

  • skip annotations that are ClassVar[...] (or typing.ClassVar[...]);
  • skip fields whose value is field(..., init=False);
  • include InitVar[...] fields;
  • respect _: KW_ONLY and kw_only=True for ordering and kind, if the checks ever care about it.

This doesn't cover fields inherited from base classes defined elsewhere, or classes made by wrappers around dataclass. A tool that only reads the source can't resolve either of those. Two options:

  • treat them leniently and skip PR02 when the decorator or base can't be resolved;
  • recognise typing.dataclass_transform, at least for decorators defined in the same file.

Real-world example: astropy's astropy.cosmology uses dataclass-based classes throughout. Enabling the hook there needed # numpydoc ignore=PR02 on about 15 classes.

I'm happy to open a PR for the plain @dataclass case.

Lingua principale
Python
Stelle
354
Fork
185
Merge medio
4m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di numpy/numpydoc

Tutte le issue di numpy/numpydoc

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.