Skip to content

Commit db01735

Browse files
committed
Add PEP 794: Import name metadata
1 parent 56c1fef commit db01735

File tree

2 files changed

+213
-17
lines changed

2 files changed

+213
-17
lines changed

source/specifications/core-metadata.rst

Lines changed: 117 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Core metadata specifications
77
============================
88

9-
This page describes version 2.4, approved in August 2024.
9+
This page describes version 2.5, approved in September 2025.
1010

1111
Fields defined in the following specification should be considered valid,
1212
complete and not subject to change. The required fields are:
@@ -50,7 +50,7 @@ Metadata-Version
5050
.. versionadded:: 1.0
5151

5252
Version of the file format; legal values are "1.0", "1.1", "1.2", "2.1",
53-
"2.2", "2.3", and "2.4".
53+
"2.2", "2.3", "2.4", and "2.5".
5454

5555
Automated tools consuming metadata SHOULD warn if ``metadata_version`` is
5656
greater than the highest version they support, and MUST fail if
@@ -718,6 +718,101 @@ user SHOULD be warned and the value ignored to avoid ambiguity. Tools MAY choose
718718
to raise an error when reading an invalid name for older metadata versions.
719719

720720

721+
.. _core-metadata-import-name:
722+
723+
Import-Name (multiple use)
724+
===========================
725+
726+
.. versionadded:: 2.5
727+
728+
A string containing an import name that the project exclusively provides when
729+
installed. The specified import name MUST be a valid Python identifier or can
730+
be empty. The import names listed in this field MUST be importable when the
731+
project is installed on *some* platform for the same version of the project.
732+
This implies that the metadata MUST be consistent across all sdists and wheels
733+
for a project release.
734+
735+
An import name MAY be followed by a semicolon and the term "private"
736+
(e.g. ``; private``) with any amount of whitespace surrounding the semicolon.
737+
This signals to tools that the import name is not part of the public API for
738+
the project.
739+
740+
Projects SHOULD list all the shortest import names that are exclusively provided
741+
by the project. If any of the shortest names are dotted names, all intervening
742+
names from that name to the top-level name should also be listed appropriately
743+
in ``Import-Name`` and/or ``Import-Namespace``.
744+
745+
If a project lists the same name in both ``Import-Name`` and
746+
``Import-Namespace``, tools MUST raise an error due to ambiguity.
747+
748+
Tools SHOULD raise an error when two projects that are about to be installed
749+
list names that overlap in each other's ``Import-Name`` entries, or when a
750+
project has an entry in ``Import-Name`` that overlaps with another project's
751+
``Import-Namespace`` entries. This is to avoid projects unexpectedly shadowing
752+
another project's code. Tools MAY warn or raise an error when installing a
753+
project into a preexisting environment where there is import name overlap with
754+
a project that is already installed.
755+
756+
Projects MAY have an empty ``Import-Name`` field in their metadata to represent
757+
a project with no import names (i.e. there are no Python modules of any kind in
758+
the distribution file).
759+
760+
Since projects MAY have no ``Import-Name`` metadata (either because the
761+
project uses an older metadata version, or because it didn't specify any), then
762+
tools have no information about what names the project provides. However, in
763+
practice the majority of projects have their project name match what their
764+
import name would be. As such, it is a reasonable assumption to make that a
765+
project name that is normalized in some way to an import name
766+
(e.g. ``packaging.utils.canonicalize_name(name, validate=True).replace("-", "_")``)
767+
can be used if some answer is needed.
768+
769+
Examples::
770+
771+
Import-Name: PIL
772+
Import-Name: _private_module ; private
773+
Import-Name: zope.interface
774+
Import-Name:
775+
776+
777+
.. _core-metadata-import-namespace:
778+
779+
Import-Namespace (multiple use)
780+
================================
781+
782+
.. versionadded:: 2.5
783+
784+
A string containing an import name that the project provides when installed, but
785+
not exclusively. The specified import name MUST be a valid Python identifier.
786+
This field is used for namespace packages where multiple projects can contribute
787+
to the same import namespace. Projects all listing the same import name in
788+
``Import-Namespace`` can be installed together without shadowing each other.
789+
790+
An import name MAY be followed by a semicolon and the term "private" (e.g.
791+
``; private``) with any amount of whitespace surrounding the semicolon. This
792+
signals to tools that the import name is not part of the public API for the
793+
project.
794+
795+
Projects SHOULD list all the shortest import names that are exclusively provided
796+
by the project. If any of the shortest names are dotted names, all intervening
797+
names from that name to the top-level name should also be listed appropriately
798+
in ``Import-Name`` and/or ``Import-Namespace``.
799+
800+
The import names listed in this field MUST be importable when the project is
801+
installed on *some* platform for the same version of the project. This implies
802+
that the metadata MUST be consistent across all sdists and wheels for a project
803+
release.
804+
805+
If a project lists the same name in both ``Import-Name`` and
806+
``Import-Namespace``, tools MUST raise an error due to ambiguity.
807+
808+
Note that ``Import-Namespace`` CANNOT be empty like ``Import-Name``.
809+
810+
Examples::
811+
812+
Import-Namespace: zope
813+
Import-Name: _private_module ; private
814+
815+
721816
Rarely Used Fields
722817
==================
723818

@@ -933,34 +1028,39 @@ Example::
9331028
History
9341029
=======
9351030

936-
- August 2025: Clarified that ``Dynamic`` only affects how fields
937-
must be treated when building a wheel from a sdist, not when modifying
938-
a wheel.
1031+
- March 2001: Core metadata 1.0 was approved through :pep:`241`.
9391032

940-
- August 2024: Core metadata 2.4 was approved through :pep:`639`.
1033+
- April 2003: Core metadata 1.1 was approved through :pep:`314`.
9411034

942-
- Added the ``License-Expression`` field.
943-
- Added the ``License-File`` field.
1035+
- February 2010: Core metadata 1.2 was approved through :pep:`345`.
9441036

945-
- March 2022: Core metadata 2.3 was approved through :pep:`685`.
1037+
- February 2018: Core metadata 2.1 was approved through :pep:`566`.
9461038

947-
- Restricted extra names to be normalized.
1039+
- Added ``Description-Content-Type`` and ``Provides-Extra``.
1040+
- Added canonical method for transforming metadata to JSON.
1041+
- Restricted the grammar of the ``Name`` field.
9481042

9491043
- October 2020: Core metadata 2.2 was approved through :pep:`643`.
9501044

9511045
- Added the ``Dynamic`` field.
9521046

953-
- February 2018: Core metadata 2.1 was approved through :pep:`566`.
1047+
- March 2022: Core metadata 2.3 was approved through :pep:`685`.
9541048

955-
- Added ``Description-Content-Type`` and ``Provides-Extra``.
956-
- Added canonical method for transforming metadata to JSON.
957-
- Restricted the grammar of the ``Name`` field.
1049+
- Restricted extra names to be normalized.
9581050

959-
- February 2010: Core metadata 1.2 was approved through :pep:`345`.
1051+
- August 2024: Core metadata 2.4 was approved through :pep:`639`.
9601052

961-
- April 2003: Core metadata 1.1 was approved through :pep:`314`:
1053+
- Added the ``License-Expression`` field.
1054+
- Added the ``License-File`` field.
9621055

963-
- March 2001: Core metadata 1.0 was approved through :pep:`241`.
1056+
- August 2025: Clarified that ``Dynamic`` only affects how fields
1057+
must be treated when building a wheel from a sdist, not when modifying
1058+
a wheel.
1059+
1060+
- September 2025: Core metadata 2.5 was approved through :pep:`794`.
1061+
1062+
- Added the ``Import-Name`` field.
1063+
- Added the ``Import-Namespace`` field.
9641064

9651065
----
9661066

source/specifications/pyproject-toml.rst

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ The complete list of keys allowed in the ``[project]`` table are:
136136
- ``dynamic``
137137
- ``entry-points``
138138
- ``gui-scripts``
139+
- ``import-names``
140+
- ``import-namespaces``
139141
- ``keywords``
140142
- ``license``
141143
- ``license-files``
@@ -466,6 +468,97 @@ matching :ref:`Provides-Extra <core-metadata-provides-extra>`
466468
metadata.
467469

468470

471+
.. _pyproject-toml-import-names:
472+
473+
``import-names``
474+
----------------
475+
476+
- TOML_ type: array of strings
477+
- Corresponding :ref:`core metadata <core-metadata>` field:
478+
:ref:`Import-Name <core-metadata-import-name>`
479+
480+
An array of strings specifying the import names that the project exclusively
481+
provides when installed. Each string MUST be a valid Python identifier or can
482+
be empty. An import name MAY be followed by a semicolon and the term "private"
483+
(e.g. ``"; private"``) with any amount of whitespace surrounding the semicolon.
484+
485+
Projects SHOULD list all the shortest import names that are exclusively provided
486+
by the project. If any of the shortest names are dotted names, all intervening
487+
names from that name to the top-level name should also be listed appropriately
488+
in ``import-names`` and/or ``import-namespaces``. For instance, a project which
489+
is a single package named spam with multiple submodules would only list
490+
``project.import-names = ["spam"]``. A project that lists ``spam.bacon.eggs``
491+
would also need to account for ``spam`` and ``spam.bacon`` appropriately in
492+
``import-names`` and ``import-namespaces``. Listing all names acts as a check
493+
that the intent of the import names is as expected. As well, projects SHOULD
494+
list all import names, public or private, using the ``; private`` modifier as
495+
appropriate.
496+
497+
If a project lists the same name in both ``import-names`` and
498+
``import-namespaces``, then tools MUST raise an error due to ambiguity.
499+
500+
Projects MAY set ``import-names`` to an empty array to represent a project with
501+
no import names (i.e. there are no Python modules of any kind in the
502+
distribution file).
503+
504+
Build back-ends MAY support dynamically calculating the value if the user
505+
declares the key in ``project.dynamic``.
506+
507+
Examples:
508+
509+
.. code-block:: toml
510+
511+
[project]
512+
name = "pillow"
513+
import-names = ["PIL"]
514+
515+
.. code-block:: toml
516+
517+
[project]
518+
name = "myproject"
519+
import-names = ["mypackage", "_private_module ; private"]
520+
521+
522+
.. _pyproject-toml-import-namespaces:
523+
524+
``import-namespaces``
525+
---------------------
526+
527+
- TOML_ type: array of strings
528+
- Corresponding :ref:`core metadata <core-metadata>` field:
529+
:ref:`Import-Namespace <core-metadata-import-namespace>`
530+
531+
An array of strings specifying the import names that the project provides when
532+
installed, but not exclusively. Each string MUST be a valid Python identifier.
533+
An import name MAY be followed by a semicolon and the term "private" (e.g.
534+
``"; private"``) with any amount of whitespace surrounding the semicolon. Note
535+
that unlike ``import-names``, ``import-namespaces`` CANNOT be an empty array.
536+
537+
Projects SHOULD list all the shortest import names that are exclusively provided
538+
by the project. If any of the shortest names are dotted names, all intervening
539+
names from that name to the top-level name should also be listed appropriately
540+
in ``import-names`` and/or ``import-namespaces``.
541+
542+
This field is used for namespace packages where multiple projects can contribute
543+
to the same import namespace. Projects all listing the same import name in
544+
``import-namespaces`` can be installed together without shadowing each other.
545+
546+
If a project lists the same name in both ``import-names`` and
547+
``import-namespaces``, then tools MUST raise an error due to ambiguity.
548+
549+
Build back-ends MAY support dynamically calculating the value if the user
550+
declares the key in ``project.dynamic``.
551+
552+
Example:
553+
554+
.. code-block:: toml
555+
556+
[project]
557+
name = "zope-interface"
558+
import-namespaces = ["zope"]
559+
import-names = ["zope.interface"]
560+
561+
469562
.. _pyproject-toml-dynamic:
470563
.. _declaring-project-metadata-dynamic:
471564

@@ -540,5 +633,8 @@ History
540633
- December 2024: The ``license`` key was redefined, the ``license-files`` key was
541634
added and ``License::`` classifiers were deprecated through :pep:`639`.
542635

636+
- September 2025: The ``import-names`` and ``import-namespaces`` keys were added
637+
through :pep:`794`.
638+
543639

544640
.. _TOML: https://toml.io

0 commit comments

Comments
 (0)