python / python/cpython

Move `ContextManager` and `AsyncContextManager` type hinting docs from `typing` to `contextlib`

Aperta
#123,584 0 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

docs topic-typing
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Documentation

PEP585 deprecated typing.ContextManager and typing.AsyncContextManager in favour of their contextlib counterparts. However, contextlib documentation lacks any type hinting information and does not even list generic parameters.

Several relevant changes were discussed along #123523.

Currently there's no backlink from contextlib to typing docs, hence users don't have any trivial way to discover type parameters of ContextManager (that's worse than collections.abc.Generator where such a link exists).

Similarly to #123583, I propose to add a "Type hinting" section to both of them in contextlib documentation:

.. class:: AbstractContextManager

   An :term:`abstract base class` for classes that implement
   :meth:`object.__enter__` and :meth:`object.__exit__`. A default
   implementation for :meth:`object.__enter__` is provided which returns
   ``self`` while :meth:`object.__exit__` is an abstract method which by default
   returns ``None``. See also the definition of :ref:`typecontextmanager`.

   :class:`!AbstractContextManager` can be used in type annotations and accepts
   two type parameters: ``AbstractContextManager[T_co, ExitT_co]``. 
   The first type parameter, ``T_co``, represents the type returned by
   the :meth:`~object.__enter__` method. The optional second type parameter, 
   ``ExitT_co``, which defaults to ``bool | None``, represents the type 
   returned by the :meth:`~object.__exit__` method.

   .. versionadded:: 3.6

   .. versionchanged:: 3.13
      Added the optional second type parameter, ``ExitT_co``.

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 con le voci esistenti di ContextManager e AsyncContextManager nella documentazione di contextlib, quindi esamina la discussione correlata in #123523 e la documentazione di typing collegata. Aggiungi le sezioni di Type hinting richieste e i backlink per i parametri generici, includendo entrambi i tipi di context manager e i relativi argomenti di tipo documentati.

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

Valutazione

Stack tecnologico
python
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
52/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.