2022-03-08 20:40:12 -05:00
|
|
|
PEP: 685
|
|
|
|
Title: Comparison of extra names for optional distribution dependencies
|
|
|
|
Author: Brett Cannon <brett@python.org>
|
2022-03-09 16:28:00 -05:00
|
|
|
PEP-Delegate: Paul Moore <p.f.moore@gmail.com>
|
2022-03-09 13:50:00 -05:00
|
|
|
Discussions-To: https://discuss.python.org/t/14141
|
2022-03-21 20:21:18 -04:00
|
|
|
Status: Accepted
|
2022-03-08 20:40:12 -05:00
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
|
|
|
Created: 08-Mar-2022
|
2022-03-16 16:35:17 -04:00
|
|
|
Post-History: `08-Mar-2022 <https://discuss.python.org/t/14141>`__
|
2022-03-21 20:21:18 -04:00
|
|
|
Resolution: https://discuss.python.org/t/pep-685-comparison-of-extra-names-for-optional-distribution-dependencies/14141/55
|
2022-03-08 20:40:12 -05:00
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
2022-03-09 13:50:00 -05:00
|
|
|
This PEP specifies how to normalize `distribution extra <Provides-Extra_>`_
|
2022-03-08 20:40:12 -05:00
|
|
|
names when performing comparisons.
|
2022-03-09 13:50:00 -05:00
|
|
|
This prevents tools from either failing to find an extra name, or
|
2022-03-08 20:40:12 -05:00
|
|
|
accidentally matching against an unexpected name.
|
|
|
|
|
|
|
|
|
|
|
|
Motivation
|
|
|
|
==========
|
|
|
|
|
2022-03-09 13:50:00 -05:00
|
|
|
The `Provides-Extra`_ core metadata specification states that an extra's
|
2022-03-08 20:40:12 -05:00
|
|
|
name "must be a valid Python identifier".
|
2022-03-09 13:50:00 -05:00
|
|
|
:pep:`508` specifies that the value of an ``extra`` marker may contain a
|
2022-03-08 20:40:12 -05:00
|
|
|
letter, digit, or any one of ``.``, ``-``, or ``_`` after the initial character.
|
2022-03-21 20:32:12 -04:00
|
|
|
There is no other `PyPA specification
|
2022-03-09 13:50:00 -05:00
|
|
|
<https://packaging.python.org/en/latest/specifications/>`_
|
2022-03-16 16:35:17 -04:00
|
|
|
which outlines how extra names should be written or normalized for comparison.
|
2022-03-09 13:50:00 -05:00
|
|
|
Due to the amount of packaging-related code in existence,
|
2022-03-08 20:40:12 -05:00
|
|
|
it is important to evaluate current practices by the community and
|
2022-03-16 16:35:17 -04:00
|
|
|
standardize on one that doesn't break most existing code, while being
|
2022-03-08 20:40:12 -05:00
|
|
|
something tool authors can agree to following.
|
|
|
|
|
2022-03-16 16:35:17 -04:00
|
|
|
The issue of there being no consistent standard was brought forward by an
|
2022-03-09 13:50:00 -05:00
|
|
|
`initial discussion <https://discuss.python.org/t/7614>`__
|
|
|
|
noting that the extra ``adhoc-ssl`` was not considered equal to the name
|
2022-03-10 17:22:21 -05:00
|
|
|
``adhoc_ssl`` by pip 22.
|
2022-03-08 20:40:12 -05:00
|
|
|
|
|
|
|
|
|
|
|
Rationale
|
|
|
|
=========
|
|
|
|
|
2022-03-09 13:50:00 -05:00
|
|
|
:pep:`503` specifies how to normalize distribution names::
|
|
|
|
|
|
|
|
re.sub(r"[-_.]+", "-", name).lower()
|
|
|
|
|
2022-03-16 16:35:17 -04:00
|
|
|
This collapses any run of the characters ``-``, ``_`` and ``.``
|
|
|
|
down to a single ``-``.
|
|
|
|
For example, ``---`` ``.`` and ``__`` all get converted to just ``-``.
|
|
|
|
This does **not** produce a valid Python identifier, per
|
2022-03-11 18:03:41 -05:00
|
|
|
the core metadata 2.2 specification for extra names.
|
2022-03-08 20:40:12 -05:00
|
|
|
|
2022-03-16 16:35:17 -04:00
|
|
|
`Setuptools 60 performs normalization <https://github.com/pypa/setuptools/blob/b2f7b8f92725c63b164d5776f85e67cc560def4e/pkg_resources/__init__.py#L1324-L1330>`__
|
2022-03-09 13:50:00 -05:00
|
|
|
via::
|
|
|
|
|
2022-03-11 18:03:41 -05:00
|
|
|
re.sub(r'[^A-Za-z0-9-.]+', '_', name).lower()
|
2022-03-09 13:50:00 -05:00
|
|
|
|
2022-03-16 16:35:17 -04:00
|
|
|
The use of an underscore/``_`` differs from PEP 503's use of a hyphen/``-``,
|
2022-03-21 20:32:12 -04:00
|
|
|
and it also normalizes characters outside of those allowed by :pep:`508`.
|
2022-03-16 16:35:17 -04:00
|
|
|
Runs of ``.`` and ``-``, unlike PEP 503, do **not** get normalized to one ``_``,
|
|
|
|
e.g. ``..`` stays the same. To note, this is inconsistent with this function's
|
|
|
|
docstring, which *does* specify that all non-alphanumeric characters
|
|
|
|
(which would include ``-`` and ``.``) are normalized and collapsed.
|
2022-03-08 20:40:12 -05:00
|
|
|
|
2022-03-10 17:22:21 -05:00
|
|
|
For pip 22, its
|
2022-03-16 16:35:17 -04:00
|
|
|
"extra normalisation behaviour is quite convoluted and erratic" [pip-erratic]_
|
2022-03-08 20:40:12 -05:00
|
|
|
and so its use is not considered.
|
|
|
|
|
2022-03-16 16:35:17 -04:00
|
|
|
.. [pip-erratic] Tzu-ping Chung on Python Discourse <https://discuss.python.org/t/7614/10
|
2022-03-10 17:22:21 -05:00
|
|
|
|
2022-03-08 20:40:12 -05:00
|
|
|
|
|
|
|
Specification
|
|
|
|
=============
|
|
|
|
|
|
|
|
When comparing extra names, tools MUST normalize the names being compared
|
2022-03-24 16:02:08 -04:00
|
|
|
using the semantics outlined in :pep:`PEP 503 <0503#normalized-names>`
|
|
|
|
for names::
|
2022-03-09 13:50:00 -05:00
|
|
|
|
2022-03-10 17:22:21 -05:00
|
|
|
re.sub(r"[-_.]+", "-", name).lower()
|
2022-03-09 13:50:00 -05:00
|
|
|
|
2022-03-10 17:22:21 -05:00
|
|
|
The `core metadata`_ specification will be updated such that the allowed
|
|
|
|
names for `Provides-Extra`_ matches what :pep:`508` specifies for names.
|
2022-03-11 18:03:41 -05:00
|
|
|
This will bring extra naming in line with that of the Name_ field.
|
|
|
|
Because this changes what is considered valid, it will lead to a core
|
|
|
|
metadata version increase to ``2.3``.
|
2022-03-08 20:40:12 -05:00
|
|
|
|
|
|
|
For tools writing `core metadata`_,
|
|
|
|
they MUST write out extra names in their normalized form.
|
2022-03-24 16:02:08 -04:00
|
|
|
This applies to the `Provides-Extra`_ field and the
|
|
|
|
:pep:`extra marker <0508#extras>` when used in the `Requires-Dist`_ field.
|
2022-03-08 20:40:12 -05:00
|
|
|
|
2022-03-09 13:50:00 -05:00
|
|
|
Tools generating metadata MUST raise an error if a user specified
|
2022-03-08 20:40:12 -05:00
|
|
|
two or more extra names which would normalize to the same name.
|
2022-03-11 18:03:41 -05:00
|
|
|
Tools generating metadata MUST raise an error if an invalid extra
|
|
|
|
name is provided as appropriate for the specified core metadata version.
|
2022-03-30 23:20:33 -04:00
|
|
|
If a project's metadata specifies an older core metadata version and
|
|
|
|
the name would be invalid with newer core metadata versions,
|
2022-03-21 20:32:12 -04:00
|
|
|
tools reading that metadata SHOULD warn the user.
|
2022-03-30 23:20:33 -04:00
|
|
|
Tools SHOULD warn users when an invalid extra name is read and SHOULD
|
|
|
|
ignore the name to avoid ambiguity.
|
2022-03-11 18:03:41 -05:00
|
|
|
Tools MAY raise an error instead of a warning when reading an
|
2022-03-16 16:35:17 -04:00
|
|
|
invalid name, if they so desire.
|
2022-03-08 20:40:12 -05:00
|
|
|
|
|
|
|
|
|
|
|
Backwards Compatibility
|
|
|
|
=======================
|
|
|
|
|
2022-03-16 16:35:17 -04:00
|
|
|
Moving to :pep:`503` normalization and :pep:`508` name acceptance
|
2022-03-10 17:22:21 -05:00
|
|
|
allows for all preexisting, valid names to continue to be valid.
|
|
|
|
|
|
|
|
Based on research looking at a collection of wheels on PyPI [pypi-results]_,
|
2022-03-16 16:35:17 -04:00
|
|
|
the risk of extra name clashes is limited to 73 instances when considering
|
|
|
|
all extras names on PyPI, valid or not (not just those within a single package)
|
2022-03-10 17:22:21 -05:00
|
|
|
while *only* looking at valid names leads to only 3 clashes:
|
2022-03-08 20:40:12 -05:00
|
|
|
|
2022-03-16 16:35:17 -04:00
|
|
|
* ``dev-test``: ``dev_test``, ``dev-test``, ``dev.test``
|
|
|
|
* ``dev-lint``: ``dev-lint``, ``dev.lint``, ``dev_lint``
|
|
|
|
* ``apache-beam``: ``apache-beam``, ``apache.beam``
|
2022-03-10 17:22:21 -05:00
|
|
|
|
|
|
|
By requiring tools writing core metadata to only record the normalized name,
|
2022-03-16 16:35:17 -04:00
|
|
|
the issue of preexisting, invalid extra names should diminish over time.
|
2022-03-10 17:22:21 -05:00
|
|
|
|
2022-03-16 16:35:17 -04:00
|
|
|
.. [pypi-results] Paul Moore on Python Discourse https://discuss.python.org/t/14141/17
|
2022-03-08 20:40:12 -05:00
|
|
|
|
|
|
|
|
|
|
|
Security Implications
|
|
|
|
=====================
|
|
|
|
|
2022-03-09 13:50:00 -05:00
|
|
|
It is possible that for a distribution that has conflicting extra names, a
|
2022-03-16 16:35:17 -04:00
|
|
|
tool ends up installing dependencies that somehow weaken the security
|
2022-03-08 20:40:12 -05:00
|
|
|
of the system.
|
2022-03-10 17:22:21 -05:00
|
|
|
This is only hypothetical and if it were to occur,
|
|
|
|
it would probably be more of a security concern for the distributions
|
|
|
|
specifying such extras names rather than the distribution that pulled
|
|
|
|
them in together.
|
2022-03-08 20:40:12 -05:00
|
|
|
|
|
|
|
|
|
|
|
How to Teach This
|
|
|
|
=================
|
|
|
|
|
|
|
|
This should be transparent to users on a day-to-day basis.
|
|
|
|
It will be up to tools to educate/stop users when they select extra
|
|
|
|
names which conflict.
|
|
|
|
|
|
|
|
|
|
|
|
Reference Implementation
|
|
|
|
========================
|
|
|
|
|
2022-03-09 13:50:00 -05:00
|
|
|
No reference implementation is provided aside from the code above,
|
2022-03-08 20:40:12 -05:00
|
|
|
but the expectation is the `packaging project`_ will provide a
|
2022-03-16 16:35:17 -04:00
|
|
|
function in its ``packaging.utils`` module that will implement extra name
|
2022-03-08 20:40:12 -05:00
|
|
|
normalization.
|
|
|
|
It will also implement extra name comparisons appropriately.
|
|
|
|
Finally, if the project ever gains the ability to write out metadata,
|
|
|
|
it will also implement this PEP.
|
|
|
|
|
|
|
|
|
2022-03-30 23:20:33 -04:00
|
|
|
Transition Plan
|
|
|
|
===============
|
|
|
|
|
|
|
|
There is a risk that a build tool will produce core metadata
|
|
|
|
conforming to version 2.3 and thus this PEP but which is consumed by a
|
|
|
|
tool that is unaware of this PEP (if that tool chooses to attempt to
|
|
|
|
read a core metadata version it does not directly support).
|
|
|
|
In such a case there is a chance that a user may specify an extra
|
|
|
|
using an non-normalized name which worked previously but which fails
|
|
|
|
now.
|
|
|
|
|
|
|
|
As such, consumers of this PEP should be prioritized more than
|
|
|
|
producers so that users can be notified that they are specifying extra
|
|
|
|
names which are not normalized (and thus may break in the future).
|
|
|
|
|
|
|
|
|
2022-03-08 20:40:12 -05:00
|
|
|
Rejected Ideas
|
|
|
|
==============
|
|
|
|
|
2022-03-11 18:03:41 -05:00
|
|
|
Using setuptools 60's normalization
|
|
|
|
-----------------------------------
|
2022-03-08 20:40:12 -05:00
|
|
|
|
2022-03-16 16:35:17 -04:00
|
|
|
Initially, this PEP proposed using setuptools ``safe_extra()`` for normalization
|
|
|
|
to try to minimize backwards-compatibility issues.
|
|
|
|
However, after checking various wheels on PyPI,
|
2022-03-11 18:03:41 -05:00
|
|
|
it became clear that standardizing **all** naming on :pep:`508` and
|
2022-03-16 16:35:17 -04:00
|
|
|
:pep:`503` semantics was easier and better long-term,
|
|
|
|
while causing minimal backwards compatibility issues.
|
2022-03-08 20:40:12 -05:00
|
|
|
|
|
|
|
|
|
|
|
Open Issues
|
|
|
|
===========
|
|
|
|
|
|
|
|
N/A
|
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document is placed in the public domain or under the
|
|
|
|
CC0-1.0-Universal license, whichever is more permissive.
|
|
|
|
|
|
|
|
|
|
|
|
.. _core metadata: https://packaging.python.org/en/latest/specifications/core-metadata/
|
2022-03-10 17:22:21 -05:00
|
|
|
.. _Name: https://packaging.python.org/en/latest/specifications/core-metadata/#name
|
2022-03-08 20:40:12 -05:00
|
|
|
.. _packaging project: https://packaging.pypa.io
|
|
|
|
.. _Provides-Extra: https://packaging.python.org/en/latest/specifications/core-metadata/#provides-extra-multiple-use
|
2022-03-10 17:22:21 -05:00
|
|
|
.. _Requires-Dist: https://packaging.python.org/en/latest/specifications/core-metadata/#requires-dist-multiple-use
|