Skip to content

Commit 2e597d1

Browse files
committed
bpo-134740: Add missing documentation for importlib.metadata.distributions()
Add documentation for the distributions() function, Distribution.discover() method, and DistributionFinder classes that were missing from the importlib.metadata documentation despite being part of the public API.
1 parent 3c05251 commit 2e597d1

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Doc/library/importlib.metadata.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,20 @@ Distributions
410410
Raises :exc:`PackageNotFoundError` if the named distribution
411411
package is not installed in the current Python environment.
412412

413+
.. function:: distributions(**kwargs)
414+
415+
Get all :class:`Distribution` instances in the current environment.
416+
417+
Any keyword arguments are passed to the ``find_distributions()`` method
418+
of the registered distribution finders (see :class:`DistributionFinder`).
419+
420+
Common keyword arguments include:
421+
422+
* ``name``: Filter to distributions matching this package name
423+
* ``path``: Search these path segments (defaults to :data:`sys.path`)
424+
425+
:return: An iterable of :class:`Distribution` instances.
426+
413427
.. class:: Distribution
414428

415429
Details of an installed distribution package.
@@ -418,6 +432,47 @@ Distributions
418432
equal, even if they relate to the same installed distribution and
419433
accordingly have the same attributes.
420434

435+
.. classmethod:: discover(*, context=None, **kwargs)
436+
437+
Return an iterable of Distribution objects for all packages.
438+
439+
Pass a ``context`` or pass keyword arguments for constructing
440+
a context.
441+
442+
:param context: A :class:`DistributionFinder.Context` object.
443+
:param kwargs: Context parameters used to construct a new context if one
444+
is not supplied.
445+
:return: Iterable of Distribution objects for packages matching
446+
the context.
447+
448+
.. class:: DistributionFinder
449+
450+
An abstract base class subclass of :class:`importlib.abc.MetaPathFinder`
451+
capable of discovering installed distributions.
452+
453+
Custom providers should implement this interface to supply metadata
454+
for distributions that cannot be discovered through the file system
455+
or other built-in mechanisms.
456+
457+
See the section on :ref:`implementing-custom-providers` for more details.
458+
459+
.. class:: DistributionFinder.Context
460+
461+
Context object used to provide parameters when discovering distributions.
462+
463+
Keyword arguments supplied to :func:`distributions` or
464+
:meth:`Distribution.discover` are stored as attributes in the context
465+
object and can be used by distribution finders to filter or customize
466+
their search results.
467+
468+
The context provides these attributes:
469+
470+
* ``name``: A package name to match, or ``None`` to match all distributions
471+
* ``path``: A list of directories to search (defaults to :data:`sys.path`)
472+
473+
Custom distribution finders can accept other keyword parameters through
474+
this context.
475+
421476
While the module level API described above is the most common and convenient usage,
422477
you can get all of that information from the :class:`!Distribution` class.
423478
:class:`!Distribution` is an abstract object that represents the metadata for
@@ -467,6 +522,8 @@ This metadata finder search defaults to ``sys.path``, but varies slightly in how
467522
- ``importlib.metadata`` will incidentally honor :py:class:`pathlib.Path` objects on ``sys.path`` even though such values will be ignored for imports.
468523

469524

525+
.. _implementing-custom-providers:
526+
470527
Implementing Custom Providers
471528
=============================
472529

0 commit comments

Comments
 (0)