Skip to content

Commit e4d2f89

Browse files
committed
Document distribution discovery in importlib.metadata docs
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
1 parent 0e3bc96 commit e4d2f89

File tree

1 file changed

+67
-5
lines changed

1 file changed

+67
-5
lines changed

Doc/library/importlib.metadata.rst

Lines changed: 67 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 contian 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,58 @@ 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:`~abc.MetaPathFinder` subclass capable of discovering installed
482+
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 for
493+
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+
.. attribute:: name
507+
508+
Specific name for which a distribution finder should match.
509+
510+
A :attr:`!name` of ``None`` matches all distributions.
511+
512+
.. attribute:: path
513+
514+
A property providing the sequence of directory paths that a
515+
distribution finder should search.
516+
517+
Typically refers to Python installed package paths such as
518+
"site-packages" directories and defaults to :attr:`sys.path`.
519+
520+
521+
.. function:: distributions(**kwargs)
522+
523+
Returns an iterable of :class:`Distribution` instances for all packages.
524+
525+
The *kwargs* argument may contain either a keyword argument ``context``, a
526+
:class:`DistributionFinder.Context` instance, or pass keyword arguments for
527+
constructing a new :class:`!DistributionFinder.Context`. The
528+
:class:`!DistributionFinder.Context` is used to modify the search for
529+
distributions.
530+
469531

470532
Implementing Custom Providers
471533
=============================
@@ -493,7 +555,7 @@ interface expected of finders by Python's import system.
493555
``importlib.metadata`` extends this protocol by looking for an optional
494556
``find_distributions`` callable on the finders from
495557
:data:`sys.meta_path` and presents this extended interface as the
496-
``DistributionFinder`` abstract base class, which defines this abstract
558+
:class:`DistributionFinder` abstract base class, which defines this abstract
497559
method::
498560

499561
@abc.abstractmethod
@@ -502,9 +564,9 @@ method::
502564
loading the metadata for packages for the indicated ``context``.
503565
"""
504566

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.
567+
The :class:`DistributionFinder.Context` object provides :attr:`.path` and
568+
:attr:`.name` properties indicating the path to search and name to match and
569+
may supply other relevant context sought by the consumer.
508570

509571
In practice, to support finding distribution package
510572
metadata in locations other than the file system, subclass
@@ -529,7 +591,7 @@ Imagine a custom finder that loads Python modules from a database::
529591
That importer now presumably provides importable modules from a
530592
database, but it provides no metadata or entry points. For this
531593
custom importer to provide metadata, it would also need to implement
532-
``DistributionFinder``::
594+
:class:`DistributionFinder`::
533595

534596
from importlib.metadata import DistributionFinder
535597

0 commit comments

Comments
 (0)