Skip to content

Commit be2ea93

Browse files
jaracoclaude
andcommitted
Add a caveats page describing interoperation with importlib.metadata.
Documents the two class hierarchies, the protocols both satisfy, and what that asks of consumers and of custom providers, following the protocols and conformance-based warning added in #505. Moved here from a draft against the stdlib docs (python/cpython#160344), where the concern is largely this package's rather than the stdlib's. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent c16ef23 commit be2ea93

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

‎docs/caveats.rst‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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`.

‎docs/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ for ``importlib.metadata``.
1818
:maxdepth: 1
1919

2020
api
21+
caveats
2122
migration
2223
history
2324

‎newsfragments/505.doc.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added a caveats page describing how this package and ``importlib.metadata`` interoperate: the two class hierarchies, the protocols both satisfy, and what that asks of consumers and of custom providers.

0 commit comments

Comments
 (0)