Skip to content

Commit 12d363b

Browse files
emmatypingjohnslavikncoghlan
authored
gh-134740: Document distribution discovery in importlib.metadata (#134751)
Document the following items listed in `__all__` but missing from documentation: - `distributions()`: mentioned in doc strings as well - `DistributionFinder`: mentioned but didn't have it's own :class: entry - `DistributionFinder.Context`: mentioned but didn't have it's own :class: entry) - `Distribution.discover()`: mentioned in doc strings Closes #134740 Partially addresses #110937 --------- Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com> Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
1 parent 04ace41 commit 12d363b

File tree

1 file changed

+72
-5
lines changed

1 file changed

+72
-5
lines changed

Doc/library/importlib.metadata.rst

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ Distributions
418418
equal, even if they relate to the same installed distribution and
419419
accordingly have the same attributes.
420420

421+
.. method:: discover(cls, *, context=None, **kwargs)
422+
423+
Returns an iterable of :class:`Distribution` instances for all packages.
424+
425+
The optional argument *context* is a :class:`DistributionFinder.Context`
426+
instance, used to modify the search for distributions. Alternatively,
427+
*kwargs* may contain keyword arguments for constructing a new
428+
:class:`!DistributionFinder.Context`.
429+
430+
421431
While the module level API described above is the most common and convenient usage,
422432
you can get all of that information from the :class:`!Distribution` class.
423433
:class:`!Distribution` is an abstract object that represents the metadata for
@@ -466,6 +476,61 @@ This metadata finder search defaults to ``sys.path``, but varies slightly in how
466476
- ``importlib.metadata`` does not honor :class:`bytes` objects on ``sys.path``.
467477
- ``importlib.metadata`` will incidentally honor :py:class:`pathlib.Path` objects on ``sys.path`` even though such values will be ignored for imports.
468478

479+
.. class:: DistributionFinder
480+
481+
A :class:`~importlib.abc.MetaPathFinder` subclass capable of discovering
482+
installed distributions.
483+
484+
Custom providers should implement this interface in order to
485+
supply metadata.
486+
487+
.. class:: Context(**kwargs)
488+
489+
A :class:`!Context` gives a custom provider a means to
490+
solicit additional details from the callers of distribution discovery
491+
functions like :func:`distributions` or :meth:`Distribution.discover`
492+
beyond :attr:`!.name` and :attr:`!.path` when searching
493+
for distributions.
494+
495+
For example, a provider could expose suites of packages in either a
496+
"public" or "private" ``realm``. A caller of distribution discovery
497+
functions may wish to query only for distributions in a particular realm
498+
and could call ``distributions(realm="private")`` to signal to the
499+
custom provider to only include distributions from that
500+
realm.
501+
502+
Each :class:`!DistributionFinder` must expect any parameters and should
503+
attempt to honor the canonical parameters defined below when
504+
appropriate.
505+
506+
See the section on :ref:`implementing-custom-providers` for more details.
507+
508+
.. attribute:: name
509+
510+
Specific name for which a distribution finder should match.
511+
512+
A :attr:`!.name` of ``None`` matches all distributions.
513+
514+
.. attribute:: path
515+
516+
A property providing the sequence of directory paths that a
517+
distribution finder should search.
518+
519+
Typically refers to Python installed package paths such as
520+
"site-packages" directories and defaults to :attr:`sys.path`.
521+
522+
523+
.. function:: distributions(**kwargs)
524+
525+
Returns an iterable of :class:`Distribution` instances for all packages.
526+
527+
The *kwargs* argument may contain either a keyword argument ``context``, a
528+
:class:`DistributionFinder.Context` instance, or pass keyword arguments for
529+
constructing a new :class:`!DistributionFinder.Context`. The
530+
:class:`!DistributionFinder.Context` is used to modify the search for
531+
distributions.
532+
533+
.. _implementing-custom-providers:
469534

470535
Implementing Custom Providers
471536
=============================
@@ -493,7 +558,7 @@ interface expected of finders by Python's import system.
493558
``importlib.metadata`` extends this protocol by looking for an optional
494559
``find_distributions`` callable on the finders from
495560
:data:`sys.meta_path` and presents this extended interface as the
496-
``DistributionFinder`` abstract base class, which defines this abstract
561+
:class:`DistributionFinder` abstract base class, which defines this abstract
497562
method::
498563

499564
@abc.abstractmethod
@@ -502,9 +567,11 @@ method::
502567
loading the metadata for packages for the indicated ``context``.
503568
"""
504569

505-
The ``DistributionFinder.Context`` object provides ``.path`` and ``.name``
506-
properties indicating the path to search and name to match and may
507-
supply other relevant context sought by the consumer.
570+
The :class:`DistributionFinder.Context` object provides
571+
:attr:`~DistributionFinder.Context.path` and
572+
:attr:`~DistributionFinder.Context.name` properties indicating the path to
573+
search and name to match and may supply other relevant context sought by the
574+
consumer.
508575

509576
In practice, to support finding distribution package
510577
metadata in locations other than the file system, subclass
@@ -529,7 +596,7 @@ Imagine a custom finder that loads Python modules from a database::
529596
That importer now presumably provides importable modules from a
530597
database, but it provides no metadata or entry points. For this
531598
custom importer to provide metadata, it would also need to implement
532-
``DistributionFinder``::
599+
:class:`DistributionFinder`::
533600

534601
from importlib.metadata import DistributionFinder
535602

0 commit comments

Comments
 (0)