python / python/cpython

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

Offen
#123,584 0 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

docs topic-typing
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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``.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit den bestehenden ContextManager- und AsyncContextManager-Einträgen in der contextlib-Dokumentation. Sehen Sie sich anschließend die zugehörige Diskussion in #123523 und die verlinkte typing-Dokumentation an. Fügen Sie die angeforderten Type-hinting-Abschnitte und Rückverweise für die generischen Parameter hinzu, wobei beide Context-Manager-Typen und ihre dokumentierten Typargumente abgedeckt werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
52/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.