Skip to content

Inconsistent extra normalisation in Requirement.__str__ #845

Closed

Description

@abravalheri

I noticed what seems to be an inconsistent normalisation when running the following:

$ pipx run --python python3.12 -- pip-run 'packaging==24.1'
# Python 3.12.7 (main, Oct  1 2024, 08:52:11) [GCC 9.4.0] on linux
# Type "help", "copyright", "credits" or "license" for more information.
>>> from packaging.requirements import Requirement
>>> req1 = Requirement('mariadb>=1.0.1; extra == "mariadb_connector"')
>>> str(req1)
'mariadb>=1.0.1; extra == "mariadb-connector"'
>>> req2 = Requirement('mariadb>=1.0.1; python_version >= "3" and extra == "mariadb_connector"')
>>> str(req2)
'mariadb>=1.0.1; python_version >= "3" and extra == "mariadb_connector"'

Here for req1, Requirement.__str__ normalises the extra to mariadb-connector, in accordance to PEP 685.
But when there are other markers involved like in req2, it seems to preserve the original value without normalisation.

Is this behaviour expected? It looks a bit inconsistent to me.
Related to #644 (comment).

Activity

  1. changed the title [-]Possibly inconsistent `extra` normalisation in `Requirement.__str__`?[/-] [+]Inconsistent `extra` normalisation in `Requirement.__str__`[/+] on Nov 4, 2024
  2. abravalheri commented on Nov 11, 2024

    @abravalheri
    ContributorAuthor

    This is still the behaviour in in 24.2:

    $ pipx run --python python3.12 -- pip-run 'packaging==24.2'
    # Python 3.12.7 (main, Oct  1 2024, 08:52:11) [GCC 9.4.0] on linux
    # Type "help", "copyright", "credits" or "license" for more information.
    >>> from packaging.requirements import Requirement
    >>> req1 = Requirement('mariadb>=1.0.1; extra == "mariadb_connector"')
    >>> str(req1)
    'mariadb>=1.0.1; extra == "mariadb-connector"'
    >>> req2 = Requirement('mariadb>=1.0.1; python_version >= "3" and extra == "mariadb_connector"')
    >>> str(req2)
    'mariadb>=1.0.1; python_version >= "3" and extra == "mariadb_connector"'

    I believe this normalisation in the environment markers are implied by PEP 685:

    For tools writing core metadata, they MUST write out extra names in their normalized form.
    This applies to the Provides-Extra field and the extra marker when used in the Requires-Dist field.

    Now, packaging itself is not writing anything, but it is what other tools use to normalise the requirements before writing.

  3. brettcannon commented on Dec 9, 2024

    @brettcannon
    Member

    I suspect we are not checking every key but just if there's a single key in the normalization case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions