@@ -366,8 +366,15 @@ This metadata finder search defaults to ``sys.path``, but varies slightly in how
366
366
- ``importlib.metadata `` will incidentally honor :py:class: `pathlib.Path ` objects on ``sys.path `` even though such values will be ignored for imports.
367
367
368
368
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 *.
371
378
372
379
Because `Distribution Package <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package >`_ metadata
373
380
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
377
384
``importlib.metadata `` queries the list of :term: `meta path finders <meta path finder> ` on
378
385
:data: `sys.meta_path `.
379
386
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.
384
389
385
390
The abstract class :py:class: `importlib.abc.MetaPathFinder ` defines the
386
391
interface expected of finders by Python's import system.
@@ -391,16 +396,16 @@ interface expected of finders by Python's import system.
391
396
method::
392
397
393
398
@abc.abstractmethod
394
- def find_distributions(context=DistributionFinder.Context()):
399
+ def find_distributions(context=DistributionFinder.Context()) -> Iterable[Distribution] :
395
400
"""Return an iterable of all Distribution instances capable of
396
401
loading the metadata for packages for the indicated ``context``.
397
402
"""
398
403
399
404
The ``DistributionFinder.Context `` object provides ``.path `` and ``.name ``
400
405
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 .
402
407
403
- What this means in practice is that to support finding distribution package
408
+ In practice, to support finding distribution package
404
409
metadata in locations other than the file system, subclass
405
410
``Distribution `` and implement the abstract methods. Then from
406
411
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
409
414
Example
410
415
-------
411
416
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::
414
418
415
419
class DatabaseImporter(importlib.abc.MetaPathFinder):
416
420
def __init__(self, db):
0 commit comments