|
| 1 | +.. _caveats: |
| 2 | + |
| 3 | +========= |
| 4 | + Caveats |
| 5 | +========= |
| 6 | + |
| 7 | +``importlib_metadata`` and :mod:`importlib.metadata` define separate class |
| 8 | +hierarchies. This package supplies its own ``Distribution``, ``PackagePath``, |
| 9 | +and metadata classes, and neither set derives from the other. Both libraries |
| 10 | +discover distributions by querying the finders on :data:`sys.meta_path`, so one |
| 11 | +custom provider serves consumers of both and has no say in which of the two a |
| 12 | +given consumer uses. |
| 13 | + |
| 14 | +Both libraries present the same protocols, ``IDistribution``, ``IPackagePath``, |
| 15 | +and ``PackageMetadata``, and the classes of both satisfy them. Those protocols, |
| 16 | +rather than any concrete class, are the interface to write against. |
| 17 | + |
| 18 | +For consumers |
| 19 | +============= |
| 20 | + |
| 21 | +A consumer calling ``distributions()`` expects this package's objects, but a |
| 22 | +custom provider may supply objects from :mod:`importlib.metadata`. This package |
| 23 | +settles most of that on the consumer's behalf, substituting its own class for |
| 24 | +each object it recognizes, such as a ``PathDistribution``, a ``PackagePath``, |
| 25 | +or an :class:`email.message.Message`. It has no way to reconstruct an arbitrary |
| 26 | +``Distribution`` subclass from a custom provider, so it passes such an object |
| 27 | +through unchanged. |
| 28 | + |
| 29 | +Annotate against the protocols, then, and do not test an object with |
| 30 | +:func:`isinstance` against a concrete class. |
| 31 | + |
| 32 | +.. _caveats-custom-providers: |
| 33 | + |
| 34 | +For custom providers |
| 35 | +==================== |
| 36 | + |
| 37 | +A provider's objects reach consumers of both libraries, so supply objects that |
| 38 | +satisfy ``IDistribution`` whole, not in part. Deriving from ``Distribution`` |
| 39 | +achieves this: the base class builds the rest of the interface on |
| 40 | +``read_text`` and ``locate_file``. Where a class omits a member the protocol |
| 41 | +requires, this package emits a :exc:`UserWarning` naming what is missing; a |
| 42 | +provider satisfying the protocol whole draws no warning. |
| 43 | + |
| 44 | +Deriving from whichever of the two libraries happens to be importable is not a |
| 45 | +general answer. A provider cannot know whether a consumer will import this |
| 46 | +package, and importing it to find out activates it in environments that would |
| 47 | +otherwise never have used it. |
| 48 | + |
| 49 | +.. versionchanged:: 10.0.0 |
| 50 | + Objects a provider supplies from :mod:`importlib.metadata` are adapted to |
| 51 | + this package's classes where they can be, and the warning reports the |
| 52 | + members a class omits rather than firing for any class derived from |
| 53 | + :mod:`importlib.metadata`. |
0 commit comments