Skip to content

Commit a6b3a49

Browse files
committed
pythongh-123976: Refresh docs around custom providers.
1 parent 3bd942f commit a6b3a49

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

Doc/library/importlib.metadata.rst

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,15 @@ This metadata finder search defaults to ``sys.path``, but varies slightly in how
366366
- ``importlib.metadata`` will incidentally honor :py:class:`pathlib.Path` objects on ``sys.path`` even though such values will be ignored for imports.
367367

368368

369-
Extending the search algorithm
370-
==============================
369+
Implementing Custom Providers
370+
=============================
371+
372+
``importlib.metadata`` address two API surfaces, one for *consumers*
373+
and another for *providers*. Most users are consumers, consuming
374+
metadata provided by the packages. There are other use-cases, however,
375+
where users wish to expose metadata through some other mechanism,
376+
such as alongside a custom importer. Such a use case calls for a
377+
*custom provider*.
371378

372379
Because `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_ metadata
373380
is not available through :data:`sys.path` searches, or
@@ -377,10 +384,8 @@ system :ref:`finders <finders-and-loaders>`. To find a distribution package's m
377384
``importlib.metadata`` queries the list of :term:`meta path finders <meta path finder>` on
378385
:data:`sys.meta_path`.
379386

380-
By default ``importlib.metadata`` installs a finder for distribution packages
381-
found on the file system.
382-
This finder doesn't actually find any *distributions*,
383-
but it can find their metadata.
387+
The implementation has hooks integrated into the ``PathFinder``,
388+
serving metadata for distribution packages found on the file system.
384389

385390
The abstract class :py:class:`importlib.abc.MetaPathFinder` defines the
386391
interface expected of finders by Python's import system.
@@ -391,16 +396,16 @@ interface expected of finders by Python's import system.
391396
method::
392397

393398
@abc.abstractmethod
394-
def find_distributions(context=DistributionFinder.Context()):
399+
def find_distributions(context=DistributionFinder.Context()) -> Iterable[Distribution]:
395400
"""Return an iterable of all Distribution instances capable of
396401
loading the metadata for packages for the indicated ``context``.
397402
"""
398403

399404
The ``DistributionFinder.Context`` object provides ``.path`` and ``.name``
400405
properties indicating the path to search and name to match and may
401-
supply other relevant context.
406+
supply other relevant context sought by the consumer.
402407

403-
What this means in practice is that to support finding distribution package
408+
In practice, to support finding distribution package
404409
metadata in locations other than the file system, subclass
405410
``Distribution`` and implement the abstract methods. Then from
406411
a custom finder, return instances of this derived ``Distribution`` in the
@@ -409,8 +414,7 @@ a custom finder, return instances of this derived ``Distribution`` in the
409414
Example
410415
-------
411416

412-
Consider for example a custom finder that loads Python
413-
modules from a database::
417+
Imagine a custom finder that loads Python modules from a database::
414418

415419
class DatabaseImporter(importlib.abc.MetaPathFinder):
416420
def __init__(self, db):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refresh docs around custom providers.

0 commit comments

Comments
 (0)