python / python/cpython

Improve the `help()` of type alias objects

Aperta
#156,925 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

interpreter-core topic-typing type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature or enhancement

EDIT: See https://discuss.python.org/t/runtime-docstrings-for-type-aliases/108901

Proposal:

Consider recording __doc__ on type alias objects.

For instance:

type Pair[T] = tuple[T, T]
"""A pair of values of the same type."""

Now Pair.__doc__ is "A pair of values of the same type.".

Use it in the REPL:

>>> from mymodule import Pair
>>> help(Pair)
Help on type alias Pair in module mymodule:

Pair[T] = tuple[T, T]
 |  A pair of values of the same type.
 |
 |  Attributes:
 |
 |  __value__
 |      Lazily evaluated value of the type alias.
 |
 |  evaluate_value
 |      Evaluation function for __value__.
 |
 |  __type_params__
 |      Type parameters declared by the type alias.
 |
 |  __parameters__
 |      Type parameters after expansion.
 |
 |  __name__
 |      Name of the type alias.
 |
 |  __qualname__
 |      Qualified name of the type alias.
 |
 |  __module__
 |      Module in which the type alias was defined.

(I wrote this text by hand, the actual thing could probably be improved.)
I assume IDEs/LSPs already catch it, so this is mainly a runtime feature.

Current help() just shows a slightly confusing help on the type alias type itself:

>>> type x = int
>>> help(x)
Help on TypeAliasType in module __main__ object:

x = class TypeAliasType(builtins.object)
 |  Type alias.
 |
 |  Type aliases are created through the type statement::
 |
...

It is confusing because (1) it reads like TypeAliasType was defined in __main__ (Help on TypeAliasType in module __main__ object), and (2) almost like if i did x = typing.TypeAliasType. (Which could be improved on its own? We could instead say x = instance of class ..., not just x = class ...)

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Guida per i contributori

Apri la guida per i contributori

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.

Direzione di ricerca

Inizia riproducendo l’output attuale di help(x) per un alias di tipo nella REPL, quindi segui la gestione degli oggetti alias di tipo da parte di help() e l’implementazione degli alias di tipo a runtime. Il lavoro è completato quando è possibile registrare le stringhe di documentazione a runtime e help(Pair) presenta l’alias, la relativa documentazione e gli attributi pertinenti senza descrivere fuorviante l’alias come la classe TypeAliasType.

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

Valutazione

Stack tecnologico
python
Ambito
developer-experience
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.