Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent extra normalisation in Requirement.__str__ #845

Open
abravalheri opened this issue Oct 22, 2024 · 1 comment
Open

Inconsistent extra normalisation in Requirement.__str__ #845

abravalheri opened this issue Oct 22, 2024 · 1 comment

Comments

@abravalheri
Copy link
Contributor

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).

@abravalheri abravalheri changed the title Possibly inconsistent extra normalisation in Requirement.__str__? Inconsistent extra normalisation in Requirement.__str__ Nov 4, 2024
@abravalheri
Copy link
Contributor Author

abravalheri commented Nov 11, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant