Style guide diverges from "Single-Source-of-Truth" regarding function parameter type documentation
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 35/100
- Tipo di issue
- Documentazione
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- python
- Ambito
- documentation
Direzione di ricerca
Inizia con la sezione Parameters della guida al formato nella pagina di documentazione collegata, quindi esamina le indicazioni correlate su Returns e Yields. Il risultato della proposta deve essere una politica definita e documentata in modo coerente riguardo alla sostituzione o meno dei campi di tipo delle docstring con le annotazioni di tipo Python, incluso il modo in cui devono essere formattati Returns e Yields.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
In section https://numpydoc.readthedocs.io/en/latest/format.html#parameters (also applies to Returns, ...) the style guide defines that types should be added to each parameter description.
Nowadays, having a well designed Python typing system, documenting parameter types in docstrings is not only outdated, but will also cause code and documentation to diverge from the key principle of Single-Source-of-Truth.
F.i. the following method:
def my_shiny_method(num_a: float, count: int) -> list[float]:
"""
Create list containing some dummy numbers.
Parameters
----------
num_a
Base number to use for filling list.
count
Target list length.
Returns
-------
List filled of length `count` with floats.
"""
return [num_a / count for i in range(count)]
is fully defined, documented and typed, using the typing syntax as the Single-Source-of-Truth.
Requiring type definitions in the docstrings, f.i.
"""
Create list containing some dummy numbers.
Parameters
----------
num_a : float
Base number to use for filling list.
count : int
Target list length.
Returns
-------
list[float]
List filled of length `count` with floats.
"""
is not only superfluous, but also - in my experience - dangerous, because methods etc. tend to be updated over time. And method interfaces may change. In my experience, method interfaces - even in productive code - change more often than desired. And most developers - especially in productive environments - are pushed for time and thus tend to forget to change the irrelevant part - the duplicate types in the docstrings - resulting in something like the following:
def my_shiny_method(num_a: int, count: int) -> list[float]:
"""
Create list containing some dummy numbers.
Parameters
----------
num_a : float
Base number to use for filling list.
count : int
Target list length.
Returns
-------
list[float]
List filled of length `count` with floats.
"""
return tuple(num_a / count for i in range(count))
--> Now we have diverging typing information
--> Most users will look at the help(method) to see what a method expects. This is, depending on the user's preference, misleading.
This becomes even more problematic when we look at stuff like Literals, which - in modern Python, should be typed
def foo(order: Literal["C", "F", "A"]) -> None: ...
whereas the style guide recommends something using a completely different syntax (set != Literal --> faulty):
order : {'C', 'F', 'A'}
Description of `order`.
Proposal
- Recommend to only use the Python typing system
- If there is a specific requirement of needing types in docstrings, the type syntax should be aligned with the core Python library
Advantages
- Single-Source-of-Truth!!!
- No risk of diverging type descriptions
- All type definitions may easily be checked by mypy or alike
- All type definitions may be style checked and reformatted by tools like ruff, black, ... Thus type information is less prone to syntax errors like
param: typeinstead ofparam : type - types from namespaces are resolved correctly
- One and only one typing syntax
- Doc generators like sphinx nowadays focus on Python typing anyways: no more ambiguous option handling
- less typing
- cleaner and simpler docstrings
Challenge
How to format Returns and Yields sections?
- 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
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 57/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
Tutte le issue di numpy/numpydoc
Issue simili
-
todo
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
tonybaloney/CSnakes#864 ·
-
infra
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
libre-embedded/svgen#72 · 1 commento ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
Genesis-Embodied-AI/genesis-world#3950 ·
I maintainer di solito rispondono entro 1 giorno
-
accessibility
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno