Skip to content

Commit 0c883a7

Browse files
authored
PEP 794: Introduce a way to specify private import names (#4567)
Also explicitly allow for an empty `Import-Name` to signal there are no import names provided by a distribution file.
1 parent 85e70f9 commit 0c883a7

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

peps/pep-0794.rst

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ This PEP proposes extending the core metadata specification for Python
1717
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
20-
added to the ``[project]`` table in ``pyproject.toml`` for providing the values
21-
for the new core metadata fields. This also leads to the introduction of core
22-
metadata version 2.5.
20+
added to the ``[project]`` table in :file:`pyproject.toml` for providing the
21+
values for the new core metadata fields. This also leads to the introduction of
22+
core metadata version 2.5.
2323

2424

2525
Motivation
@@ -74,9 +74,19 @@ projects to only have to check a single file's core metadata to get all
7474
possible import names instead of checking all the released files. This also
7575
means one does not need to worry if a file is missing when reading the core
7676
metadata or one can work solely from an sdist if the metadata is provided. As
77-
well, it simplifies having ``project.import-names`` and ``project.import-namespaces``
78-
keys in ``pyproject.toml`` by having it be consistent for the entire project
79-
version and not unique per released file for the same version.
77+
well, it simplifies having ``project.import-names`` and
78+
``project.import-namespaces`` keys in :file:`pyproject.toml` by having it be
79+
consistent for the entire project version and not unique per released file for
80+
the same version.
81+
82+
A distribution file containing modules and packages can have any combination of
83+
public and private APIs at the module/package level. Distribution files can also
84+
contain no modules or packages of any kind. Being able to distinguish between
85+
the situations all have various tool uses that could be beneficial to users. For
86+
instance, knowing all import names regardless of whether they are public or
87+
private helps detect clashes at install time. But knowing what is explicitly
88+
public or private allows tools such as editors to not suggest private import
89+
names as part of auto-complete.
8090

8191
This PEP is not overly strict on what to (not) list in the proposed metadata on
8292
purpose. Having build back-ends verify that a project is accurately following
@@ -111,12 +121,17 @@ Because this PEP introduces a new field to the core metadata, it bumps the
111121
latest core metadata version to 2.5.
112122

113123
The ``Import-Name`` and ``Import-Namespace`` fields are "multiple uses" fields.
114-
Each entry of both fields MUST be a valid import name. The names specified MUST
115-
be importable when the project is installed on *some* platform for the same
116-
version of the project (e.g. the metadata MUST be consistent across all sdists
117-
and wheels for a project release). This does imply that the information isn't
118-
specific to the distribution artifact it is found in, but to the release
119-
version the distribution artifact belongs to.
124+
Each entry of both fields MUST be a valid import name or can be empty in the
125+
case of ``Import-Name``. Any names specified MUST be importable when the project
126+
is installed on *some* platform for the same version of the project (e.g. the
127+
metadata MUST be consistent across all sdists and wheels for a project release).
128+
This does imply that the information isn't specific to the distribution artifact
129+
it is found in, but to the release version the distribution artifact belongs to.
130+
131+
An import name MAY be followed by a semicolon and the term "private" (e.g.
132+
``; private``). This signals to tools that the import name is not part of the
133+
public API for the project. Any number of spaces surrounding the ``;`` is
134+
allowed.
120135

121136
``Import-Name`` lists import names which a project, when installed, would
122137
*exclusively* provide (i.e. if two projects were installed with the same import
@@ -142,7 +157,9 @@ name should also be listed appropriately in ``Import-Namespace`` and/or
142157
``project.import-names = ["spam"]``. A project that lists ``spam.bacon.eggs``
143158
would also need to account for ``spam`` and ``spam.bacon`` appropriately in
144159
``import-names`` and ``import-namespaces``. Listing all names acts as a check
145-
that the intent of the import names is as expected.
160+
that the intent of the import names is as expected. As well, projects SHOULD
161+
list all import names, public or private, using the ``; private`` modifier
162+
as appropriate.
146163

147164
If a project lists the same name in both ``Import-Name`` and
148165
``Import-Namespace``, then tools MUST raise an error due to ambiguity; this also
@@ -158,7 +175,7 @@ a project has an entry in ``Import-Name`` that overlaps with another project's
158175
Projects MAY leave ``Import-Name`` and ``Import-Namespace`` out of the core
159176
metadata for a project. In that instance, tools SHOULD assume that when the
160177
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
178+
an import name, would be an entry in ``Import-Name`` (i.e. ``-`` replaced with
162179
``_`` in the normalized project name). This is deemed reasonable as this will
163180
only occur for projects that make a new release once their build back-end
164181
supports core metadata 2.5 or newer as proposed by this PEP.
@@ -168,6 +185,13 @@ Projects MAY set ``import-names`` or ``import-namespaces`` -- as well as
168185
import name of the project to explicitly declare that the project's name
169186
is also the import name.
170187

188+
Projects MAY set ``import-names`` an empty array and not set
189+
``import-namespaces`` at all in a :file:`pyproject.toml` file (e.g.
190+
``import-names = []``). To match this, projects MAY have an empty
191+
``Import-Name`` field in their metadata. This represents a project with NO
192+
import names, public or private (i.e. there are no Python modules of any kind
193+
in the distribution file).
194+
171195

172196
Examples
173197
--------
@@ -225,8 +249,8 @@ projects provide for importing. If their project name matches the module or
225249
package name their project provides they don't have to do anything. If there is
226250
a difference, though, they should record all the import names their project
227251
provides, using the shortest names possible. If any of the names are implicit
228-
namespaces, those go into ``project.import-namespaces`` in ``pyproject.toml``,
229-
otherwise the name goes into ``project.import-names``.
252+
namespaces, those go into ``project.import-namespaces`` in
253+
:file:`pyproject.toml`, otherwise the name goes into ``project.import-names``.
230254

231255
Users of projects don't necessarily need to know about this new metadata.
232256
While they may be exposed to it via tooling, the details of where that data

0 commit comments

Comments
 (0)