Skip to content

Commit

Permalink
Allow more separator styles in tags and normalize certain alternative…
Browse files Browse the repository at this point in the history
… stage names for PEP 440
  • Loading branch information
mtkennerly committed Oct 31, 2021
1 parent 6e4e290 commit 0f8c364
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
## Unreleased

* Broadened the default version tag pattern to allow more separator styles
recognized in PEP 440 pre-normalized forms (`-`, `.`, and `_`).
* Enhanced `serialize_pep440()` to normalize the alternative prerelease names
(`alpha` -> `a`, `beta` -> `b`, `c`/`pre`/`preview` -> `rc`) and
capitalizations (`RC` -> `rc`, etc).
* Added a `py.typed` file for PEP-561.
([Contributed by wwuck](https://github.com/mtkennerly/dunamai/pull/25))
* Replaced `pkg_resources` dependency with `packaging` and `importlib_metadata`.
([Contributed by flying-sheep](https://github.com/mtkennerly/dunamai/pull/29))
* Added some missing public items to `__all__`.

## v1.6.0 (2021-08-09)

Expand Down
29 changes: 23 additions & 6 deletions dunamai/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
__all__ = ["check_version", "get_version", "Style", "Vcs", "Version"]
__all__ = [
"bump_version",
"check_version",
"get_version",
"serialize_pep440",
"serialize_pvp",
"serialize_semver",
"Style",
"Vcs",
"Version",
]

import datetime as dt
import re
Expand All @@ -23,10 +33,10 @@
)

_VERSION_PATTERN = r"""
(?x) (?# ignore whitespace)
^v(?P<base>\d+(\.\d+)*) (?# v1.2.3)
(-?((?P<stage>[a-zA-Z]+)\.?(?P<revision>\d+)?))? (?# b0)
(\+(?P<tagged_metadata>.+))?$ (?# +linux)
(?x) (?# ignore whitespace)
^v(?P<base>\d+(\.\d+)*) (?# v1.2.3)
([-._]?((?P<stage>[a-zA-Z]+)[-._]?(?P<revision>\d+)?))? (?# b0)
(\+(?P<tagged_metadata>.+))?$ (?# +linux)
""".strip()

# PEP 440: [N!]N(.N)*[{a|b|rc}N][.postN][.devN][+<local version label>]
Expand Down Expand Up @@ -1001,7 +1011,14 @@ def serialize_pep440(
out.append(base)

if stage is not None:
out.append(stage)
alternative_stages = {
"alpha": "a",
"beta": "b",
"c": "rc",
"pre": "rc",
"preview": "rc",
}
out.append(alternative_stages.get(stage.lower(), stage.lower()))
if revision is None:
# PEP 440 does not allow omitting the revision, so assume 0.
out.append(0)
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_dunamai.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,11 @@ def check_re(
check_re("v0.1.0rc.4", "0.1.0", "rc", "4")
check_re("v0.1.0-beta", "0.1.0", "beta")

check_re("v0.1.0a2", "0.1.0", "a", "2")
check_re("v0.1.0-a-2", "0.1.0", "a", "2")
check_re("v0.1.0.a.2", "0.1.0", "a", "2")
check_re("v0.1.0_a_2", "0.1.0", "a", "2")

check_re("v0.1.0rc.4+specifier", "0.1.0", "rc", "4", tagged_metadata="specifier")

check_re("v1", "1")
Expand All @@ -571,6 +576,13 @@ def test__serialize_pep440():
== "0!1.2.3a4.post5.dev6+foo.bar"
)

assert serialize_pep440("1.2.3", stage="alpha", revision=4) == "1.2.3a4"
assert serialize_pep440("1.2.3", stage="ALphA", revision=4) == "1.2.3a4"
assert serialize_pep440("1.2.3", stage="beta", revision=4) == "1.2.3b4"
assert serialize_pep440("1.2.3", stage="c", revision=4) == "1.2.3rc4"
assert serialize_pep440("1.2.3", stage="pre", revision=4) == "1.2.3rc4"
assert serialize_pep440("1.2.3", stage="preview", revision=4) == "1.2.3rc4"

with pytest.raises(ValueError):
serialize_pep440("foo")

Expand Down

0 comments on commit 0f8c364

Please sign in to comment.