pypa / pypa/packaging

utils.canonicalize_version produces an abnormal version

Open
#792 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

packaging.utils packaging.version
Dominant language
Python
Stars
751
Forks
324
Avg merge
12h 58m
Merged PRs (30d)
3

Description

In https://github.com/pypa/setuptools/issues/4302, we learned that using packaging.utils.canonicalize_version to canonicalize a version produces an aberrant version.

 @ pip-run packaging -- -c 'import packaging.utils; print(packaging.utils.canonicalize_version("69.3.0"))'
69.3

Instead of matching the convention of wheels, which will produce a wheel with a version of "69.3.0", canonicalize_version strips the trailing zero and produces 69.3.

image

Moreover, semver indicates that the version number should include a trailing zero, so it's not possible to honor both semver and the canonicalized_version.

In https://github.com/pypa/setuptools/issues/3593#issuecomment-2052518260, @pfmoore says that wheels and sdists should use the same format.

What's the correct format?

Also, why does a function called canonicalize_version produce both canonical and non-canonical versions depending on a flag?

https://github.com/pypa/packaging/blob/32deafe8668a2130a3366b98154914d188f3718e/src/packaging/utils.py#L81-L83

I see that wheel implements its own safer_version function.

If packaging.utils.canonicalize_version isn't the right implementation for a PEP 625 canonicalized version, what is?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/packaging/utils.py at the linked canonicalize_version implementation, then compare its behavior with wheel/bdist_wheel.py's safer_version and the referenced setuptools discussions. Determine the intended PEP 625, wheel, and semver behavior and clarify whether the flag and resulting API need changing. Done means the expected format and implementation path are agreed and covered by relevant tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.