mtkennerly / dunamai

Dynamic versioning library and CLI
https://dunamai.readthedocs.io/en/latest
MIT License
320 stars 23 forks source link

Wrong PEP440 version being outputted #32

Closed yajo closed 2 years ago

yajo commented 2 years ago

With a repo like this:

❯ git log --graph --decorate
* commit a6ff36f297c7468c6e68303ba2bf4e9832f60505 (HEAD, tag: v2.0.0.alpha1, origin/master, origin/HEAD, master)
| Author: Jairo Llopis <jairo.llopis@tecnativa.com>
| Date:   Sun Oct 31 07:34:03 2021 +0000
| 
|     v2a1
| 
* commit fde2a1107b5f2d6610227019d2679593d5677161 (tag: v2.0.0.dev1)
| Author: Jairo Llopis <jairo.llopis@tecnativa.com>
| Date:   Sun Oct 31 07:34:03 2021 +0000
| 
|     v2dev1
| 
* commit afd5acd645b4617968ea1089c4bf1100bfd3ea68 (tag: v1.0.0)
  Author: Jairo Llopis <jairo.llopis@tecnativa.com>
  Date:   Sun Oct 31 07:34:03 2021 +0000

      v1

You get a version like this:

❯ dunamai from git --style pep440
1.0.0.post2.dev0+a6ff36f

However packaging seems to parse the version just fine:

>>> from packaging.version import Version
>>> Version('v2.0.0.alpha1')
<Version('2.0.0a1')>

Because v2.0.0.alpha1 is a valid PEP440 version. See:

mtkennerly commented 2 years ago

Technically, I wouldn't say that v2.0.0.alpha1 is a "valid PEP-440 version", but rather that it's a legacy syntax for which PEP-440 defines how to convert it into a valid version. Or I suppose you could say it's a valid input version, but not a valid output version.

https://www.python.org/dev/peps/pep-0440/#normalization

In order to maintain better compatibility with existing versions there are a number of "alternative" syntaxes that MUST be taken into account when parsing versions. These syntaxes MUST be considered when parsing a version, however they should be "normalized" to the standard syntax defined above.

"Existing versions" meaning pre-PEP-440.

PEP-440 says that these legacy syntaxes must be considered when "parsing a version", but strictly speaking, Dunamai isn't parsing a version, but rather parsing a version-like tag. For example, you might have a tag like v-a1-1.2.3 and then define Dunamai's --pattern such that it's converted into the version 1.2.3a1.

Essentially, the request here is to recognize the pre-normalized alternative syntaxes when parsing a version-like tag (i.e., expand Dunamai's default tag regex) and then normalize them in serialize_pep440(). Some of these are supported already as a side effect of supporting multiple versioning styles (e.g., v2.0.0-alpha1 is already recognized in tags, although alpha isn't normalized to a for PEP 440), so I think it makes sense to fill in the gaps (e.g., v2.0.0.alpha1 and v2.0.0_alpha1 currently aren't recognized in tags).

mtkennerly commented 2 years ago

Your tag v2.0.0.alpha1 will now work as expected in Dunamai v1.7.0:

$ dunamai from git
2.0.0a1