Skip to content

Commit 395f4db

Browse files
authored
PEP 794: Declare names can't be listed in both import-names and import-namespaces (#4563)
Also clarify it's okay for a project to list its name to be explicit about opting into this metadata. Fix some grammar and spelling along the way.
1 parent fd49055 commit 395f4db

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

peps/pep-0794.rst

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ packaging to include two new, repeatable fields named ``Import-Name`` and
1818
``Import-Namespace`` to record the import names that a project provides once
1919
installed. New keys named ``import-names`` and ``import-namespaces`` will be
2020
added to the ``[project]`` table in ``pyproject.toml`` for providing the values
21-
for the new core metadata field. This also leads to the introduction of core
21+
for the new core metadata fields. This also leads to the introduction of core
2222
metadata version 2.5.
2323

2424

@@ -27,7 +27,7 @@ Motivation
2727

2828
In Python packaging there is no requirement that a project name match the
2929
name(s) that you can import for that project. As such, there is no clean,
30-
easy, accurate way to go from import name to project name and vice-versa.
30+
easy, accurate way to go from import name to project name and vice versa.
3131
This can make it difficult for tools that try to help people in discovering
3232
the right project to install when they know the import name or knowing what
3333
import names a project will provide once installed.
@@ -48,7 +48,7 @@ There is also no easy way to know whether installing two projects will conflict
4848
with one another based on the import names they provide. For instance, if two
4949
different projects have a ``_utils`` module, installing both projects will lead
5050
to a clash as one project's ``_utils`` module would take precedence over the
51-
other project's version by overwriting the other project's file; this issue
51+
other's, by overwriting the other project's file; this issue
5252
has been `seen in the wild <https://github.com/astral-sh/uv/pull/13437>`__.
5353

5454
It may also help with spam detection. If a project specifies the same import
@@ -80,16 +80,16 @@ version and not unique per released file for the same version.
8080

8181
This PEP is not overly strict on what to (not) list in the proposed metadata on
8282
purpose. Having build back-ends verify that a project is accurately following
83-
a specification that is somehow strict about what can be listed would be near
83+
a specification that is somehow strict about what can be listed would be nearly
8484
impossible to get right due to how flexible Python's import system is. As such,
8585
this PEP only requires that valid import names be used and that projects don't
86-
lie (and it is acknowledged the latter requirements cannot be validated
86+
lie (and it is acknowledged the latter requirement cannot be validated
8787
programmatically). Projects do, though, need to account for all levels of the
8888
names they list (e.g. you can't list ``a.b.c`` and not account for ``a`` and
8989
``a.b``).
9090

9191
Various other attempts have been made to solve this, but they all have to
92-
make various trade-offs. For instance, one could download every wheel for
92+
make trade-offs. For instance, one could download every wheel for
9393
every project release and look at what files are provided via the
9494
:ref:`packaging:binary-distribution-format`, but that's a lot of CPU and
9595
bandwidth for something that is static information (although tricks can be
@@ -99,10 +99,9 @@ currently repeated by everyone independently instead of having the metadata
9999
hosted by a central index server like PyPI. It also doesn't work for sdists
100100
as the structure of the wheel isn't known yet, and so inferring the structure
101101
of the code installed isn't possible. As well, these solutions are not
102-
necessarily accurate as it is based on inference instead of being explicitly
102+
necessarily accurate as they are based on inference instead of being explicitly
103103
provided by the project owners. All of these accuracy issues affect even having
104-
an index hosting the information to avoid the compute costs of gathering the
105-
information.
104+
an index host the information to avoid the compute costs of gathering it.
106105

107106

108107
Specification
@@ -116,7 +115,7 @@ Each entry of both fields MUST be a valid import name. The names specified MUST
116115
be importable when the project is installed on *some* platform for the same
117116
version of the project (e.g. the metadata MUST be consistent across all sdists
118117
and wheels for a project release). This does imply that the information isn't
119-
specific to the distribution artifact it is found in, but for the release
118+
specific to the distribution artifact it is found in, but to the release
120119
version the distribution artifact belongs to.
121120

122121
``Import-Name`` lists import names which a project, when installed, would
@@ -138,27 +137,37 @@ Projects SHOULD list all the shortest import names that are exclusively provided
138137
by a project which would cover all import name scenarios. If any of the shortest
139138
names are dotted names, all intervening names from that name to the top-level
140139
name should also be listed appropriately in ``Import-Namespace`` and/or
141-
``Import-Names``. For instance, a project which is a single package named
140+
``Import-Name``. For instance, a project which is a single package named
142141
``spam`` with multiple submodules would only list
143142
``project.import-names = ["spam"]``. A project that lists ``spam.bacon.eggs``
144143
would also need to account for ``spam`` and ``spam.bacon`` appropriately in
145144
``import-names`` and ``import-namespaces``. Listing all names acts as a check
146145
that the intent of the import names is as expected.
147146

147+
If a project lists the same name in both ``Import-Name`` and
148+
``Import-Namespace``, then tools MUST raise an error due to ambiguity; this also
149+
applies to ``import-names`` and ``import-namespaces``, respectively.
150+
148151
Tools SHOULD raise an error when two projects that are to be installed list
149-
names that overlap in each others' ``Import-Name`` entries. This is to avoid
152+
names that overlap in each other's ``Import-Name`` entries. This is to avoid
150153
projects unexpectedly shadowing another project's code. The same applies to when
151154
a project has an entry in ``Import-Name`` that overlaps with another project's
152-
``Import-Namespace`` entries.
155+
``Import-Namespace`` entries. This does not apply to overlapping
156+
``Import-Namespace`` entries as that's the purpose of namespace packages.
153157

154158
Projects MAY leave ``Import-Name`` and ``Import-Namespace`` out of the core
155159
metadata for a project. In that instance, tools SHOULD assume that when the
156-
core metadata is 2.5 or newer that the normalized project name when converted to
157-
an import name would be an entry in ``Import-Name`` (i.e. ``-`` substituted for
158-
``-`` in the normalized project name). This is deemed reasonable as this will
160+
core metadata is 2.5 or newer, the normalized project name, when converted to
161+
an import name, would be an entry in ``Import-Name`` (i.e. ``-`` substituted for
162+
``_`` in the normalized project name). This is deemed reasonable as this will
159163
only occur for projects that make a new release once their build back-end
160164
supports core metadata 2.5 or newer as proposed by this PEP.
161165

166+
Projects MAY set ``import-names`` or ``import-namespaces`` -- as well as
167+
``Import-Name`` or ``Import-Namespace``, respectively -- to the normalized
168+
import name of the project to explicitly declare that the project's name
169+
is also the import name.
170+
162171

163172
Examples
164173
--------
@@ -222,8 +231,8 @@ otherwise the name goes into ``project.import-names``.
222231
Users of projects don't necessarily need to know about this new metadata.
223232
While they may be exposed to it via tooling, the details of where that data
224233
came from isn't critical. It's possible they may come across it if an index
225-
server exposed it (e.g., listed the values from ``Import-Name`` and marked
226-
whether the file structure backed up the claims the metadata makes), but that
234+
server exposes it (e.g., lists the values from ``Import-Name`` and marks
235+
whether the file structure backs up the claims the metadata makes), but that
227236
still wouldn't require users to know the technical details of this PEP.
228237

229238

@@ -242,7 +251,7 @@ Infer the value for ``Import-Namespace``
242251

243252
A previous version of this PEP inferred what would have been the values for
244253
``Import-Namespace`` based on dotted names in ``Import-Name``. It was decided
245-
that it would better to be explicit not only to avoid mistakes by accidentally
254+
that it would be better to be explicit not only to avoid mistakes by accidentally
246255
listing something that would be interpreted as an implicit namespace, but it
247256
also made the data more self-documenting.
248257

@@ -251,7 +260,7 @@ Require that names listed in ``Import-Namespace`` never be contained by a name i
251260
-------------------------------------------------------------------------------------------------
252261

253262
The way Python's import system works by default means that it isn't possible to
254-
have an import name contain an namespace. But Python's import system is flexible
263+
have an import name contain a namespace. But Python's import system is flexible
255264
enough that user code could make that possible. As such, the requirement that
256265
tools error out if an import name contained a namespace name --
257266
``import-names = ["spam"]`` and ``import-namespaces = ["spam.bacon"]`` -- was
@@ -272,7 +281,7 @@ made to go with a new field.
272281
Name the field ``Namespace``
273282
----------------------------
274283

275-
While the term "namespace" name is technically accurate from an import
284+
While the term "namespace" is technically accurate from an import
276285
perspective, it could be confused with implicit namespace packages.
277286

278287

@@ -284,7 +293,7 @@ During `discussions about a pre-PEP version
284293
PEP, it was suggested that the ``RECORD`` file from wheels be served from
285294
index servers instead of this new metadata. That would have the benefit of
286295
being implementable immediately. But in order to provide the equivalent
287-
information there would be necessary inference based on the file structure of
296+
information, inference would be necessary based on the file structure of
288297
what would be installed by the wheel. That could lead to inaccurate
289298
information. It also doesn't support sdists.
290299

@@ -300,7 +309,7 @@ An earlier version of this PEP was much more strict in what could be put into
300309
``Import-Name``. This included turning some "SHOULD" guidelines into "MUST"
301310
requirements and being specific about how to calculate what a project "owned".
302311
In the end it was decided that was too restrictive and risked being implemented
303-
incorrectly or the spec being unexpectedy too strict.
312+
incorrectly or the spec being unexpectedly too strict.
304313

305314
Since the metadata was never expected to be exhaustive as it can't be verified
306315
to be, the looser spec that is currently in this PEP was chosen instead.
@@ -315,7 +324,7 @@ N/A
315324
Acknowledgments
316325
===============
317326

318-
Thanks to HeeJae Chang for ~~complaining about~~ bringing up regularly the
327+
Thanks to HeeJae Chang for ~~complaining about~~ regularly bringing up the
319328
usefulness that this metadata would provide. Thanks to Josh Cannon (no
320329
relation) for reviewing drafts of this PEP and providing feedback. Also,
321330
thanks to everyone who participated in a `previous discussion

0 commit comments

Comments
 (0)