Skip to content

Commit a47cd21

Browse files
authored
gh-123976: Refresh docs around custom providers. (#123977)
* gh-123976: Refresh docs around custom providers. * Remove excess whitespace.
1 parent f4e5643 commit a47cd21

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

Doc/library/importlib.metadata.rst

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ such as its entry points
1919
or its top-level names (`Import Package <https://packaging.python.org/en/latest/glossary/#term-Import-Package>`_\s, modules, if any).
2020
Built in part on Python's import system, this library
2121
intends to replace similar functionality in the `entry point
22-
API`_ and `metadata API`_ of ``pkg_resources``. Along with
22+
API`_ and `metadata API`_ of ``pkg_resources``. Along with
2323
:mod:`importlib.resources`,
2424
this package can eliminate the need to use the older and less efficient
2525
``pkg_resources`` package.
@@ -46,7 +46,7 @@ and metadata defined by the `Core metadata specifications <https://packaging.pyt
4646

4747
By default, distribution metadata can live on the file system
4848
or in zip archives on
49-
:data:`sys.path`. Through an extension mechanism, the metadata can live almost
49+
:data:`sys.path`. Through an extension mechanism, the metadata can live almost
5050
anywhere.
5151

5252

@@ -68,7 +68,7 @@ Overview
6868

6969
Let's say you wanted to get the version string for a
7070
`Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_ you've installed
71-
using ``pip``. We start by creating a virtual environment and installing
71+
using ``pip``. We start by creating a virtual environment and installing
7272
something into it:
7373

7474
.. code-block:: shell-session
@@ -87,7 +87,7 @@ You can get the version string for ``wheel`` by running the following:
8787
'0.32.3'
8888
8989
You can also get a collection of entry points selectable by properties of the EntryPoint (typically 'group' or 'name'), such as
90-
``console_scripts``, ``distutils.commands`` and others. Each group contains a
90+
``console_scripts``, ``distutils.commands`` and others. Each group contains a
9191
collection of :ref:`EntryPoint <entry-points>` objects.
9292

9393
You can get the :ref:`metadata for a distribution <metadata>`::
@@ -114,7 +114,7 @@ Entry points
114114
The ``entry_points()`` function returns a collection of entry points.
115115
Entry points are represented by ``EntryPoint`` instances;
116116
each ``EntryPoint`` has a ``.name``, ``.group``, and ``.value`` attributes and
117-
a ``.load()`` method to resolve the value. There are also ``.module``,
117+
a ``.load()`` method to resolve the value. There are also ``.module``,
118118
``.attr``, and ``.extras`` attributes for getting the components of the
119119
``.value`` attribute.
120120

@@ -167,7 +167,7 @@ Inspect the resolved entry point::
167167

168168
The ``group`` and ``name`` are arbitrary values defined by the package author
169169
and usually a client will wish to resolve all entry points for a particular
170-
group. Read `the setuptools docs
170+
group. Read `the setuptools docs
171171
<https://setuptools.pypa.io/en/latest/userguide/entry_point.html>`_
172172
for more information on entry points, their definition, and usage.
173173

@@ -240,12 +240,12 @@ number, as a string::
240240
Distribution files
241241
------------------
242242

243-
You can also get the full set of files contained within a distribution. The
243+
You can also get the full set of files contained within a distribution. The
244244
``files()`` function takes a `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_ name
245245
and returns all of the
246-
files installed by this distribution. Each file object returned is a
246+
files installed by this distribution. Each file object returned is a
247247
``PackagePath``, a :class:`pathlib.PurePath` derived object with additional ``dist``,
248-
``size``, and ``hash`` properties as indicated by the metadata. For example::
248+
``size``, and ``hash`` properties as indicated by the metadata. For example::
249249

250250
>>> util = [p for p in files('wheel') if 'util.py' in str(p)][0] # doctest: +SKIP
251251
>>> util # doctest: +SKIP
@@ -321,9 +321,9 @@ Distributions
321321
=============
322322

323323
While the above API is the most common and convenient usage, you can get all
324-
of that information from the ``Distribution`` class. A ``Distribution`` is an
324+
of that information from the ``Distribution`` class. A ``Distribution`` is an
325325
abstract object that represents the metadata for
326-
a Python `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_. You can
326+
a Python `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_. You can
327327
get the ``Distribution`` instance::
328328

329329
>>> from importlib.metadata import distribution # doctest: +SKIP
@@ -366,21 +366,26 @@ 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
374381
package loaders directly,
375382
the metadata for a distribution is found through import
376-
system :ref:`finders <finders-and-loaders>`. To find a distribution package's metadata,
383+
system :ref:`finders <finders-and-loaders>`. To find a distribution package's metadata,
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)