`numpydoc lint` reports PR02 for documented dataclass fields
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 57/100
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[...](ortyping.ClassVar[...]); - skip fields whose value is
field(..., init=False); - include
InitVar[...]fields; - respect
_: KW_ONLYandkw_only=Truefor 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
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di numpy/numpydoc
-
Don't require ``RT01`` for propertiesForse di nuovo libera @Nani-2007-code l’ha presa 30 giorni fa e non c’è nessuna pull request aperta. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 68/100
-
DOC: Version link overflowing on navbarForse di nuovo libera @swoyer2 l’ha presa 52 giorni fa e non c’è nessuna pull request aperta. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 52/100
-
needs-decision
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 63/100
Tutte le issue di numpy/numpydoc
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
reflex-dev/reflex#8026 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 86/100
superlinked/sie#1033 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
apache/ponymail-foal#365 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
NousResearch/hermes-labs-plugins#116 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
gcivil-nyu-org/team-3-fall26#192 ·
I maintainer di solito rispondono entro 1 giorno