Skip to content

Commit

Permalink
allow separator between dev and num, ie, "dev.23"
Browse files Browse the repository at this point in the history
According to the regular expression in Appendix B of the specification for
Version Specifiers:

https://packaging.python.org/en/latest/specifications/version-specifiers/#appendix-parsing-version-strings-with-regular-expressionsversion-specifiers/

... a separator character ('-', '_', or '.') is allowed between the word
"dev" and the optional number:

```
(?P<dev>                     # dev release
    [-_\.]?
    (?P<dev_l>dev)
    [-_\.]?
(?P<dev_n>[0-9]+)?
)?
```

Fixes RazerM#33
  • Loading branch information
pmolodo committed Jan 4, 2025
1 parent aebccb9 commit 2c43655
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
26 changes: 18 additions & 8 deletions src/parver/_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
post = sep? post_tag pre_post_num?
post_implicit = "-" int
post_tag = "post" / "rev" / "r"
dev = sep? "dev" pre_post_num?
pre_post_num = sep? int
dev = sep? "dev" int?
local = "+" local_part (sep local_part)*
local_part = alpha / int
sep = dot / "-" / "_"
Expand Down Expand Up @@ -175,20 +175,30 @@ def visit_post_implicit(
def visit_dev(self, node: Node, children: SemanticActionResults) -> segment.Dev:
num: Union[ImplicitZero, int] = IMPLICIT_ZERO
sep: Union[Separator, None, UnsetType] = UNSET
sep2: Union[Separator, None, UnsetType] = UNSET

for token in children:
if sep is UNSET:
if isinstance(token, Sep):
sep = token.value
else:
num = token
else:
if isinstance(token, Sep):
assert sep is UNSET
sep = token.value
elif isinstance(token, int):
# we should only get an int if there's no sep2 - if there is,
# we should get a tuple
assert sep2 is UNSET
sep2 = None
num = token
elif isinstance(token, tuple):
assert len(token) == 2
sep2 = token[0].value
num = token[1]
else:
raise AssertionError(f"unknown dev child token type: {token!r}")

# if there is a dev segment at all, the first sep is always known
if isinstance(sep, UnsetType):
sep = None

return segment.Dev(value=num, sep=sep)
return segment.Dev(value=num, sep=sep, sep2=sep2)

def visit_local(self, node: Node, children: SemanticActionResults) -> segment.Local:
return segment.Local("".join(str(getattr(c, "value", c)) for c in children))
Expand Down
1 change: 1 addition & 0 deletions src/parver/_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Post:
class Dev:
value: Union[ImplicitZero, int] = attr.ib()
sep: Optional[Separator] = attr.ib()
sep2: Union[Separator, UnsetType, None] = attr.ib()


@attr.s(slots=True)
Expand Down
22 changes: 22 additions & 0 deletions src/parver/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ class Version:
:param dev_sep: Specify an alternate separator before the development
release segment. The normal form is ``'.'``.
:param dev_sep2: Specify an alternate separator between the identifier and
number. The normal form is `None`.
:param post_tag: Specify alternate post release identifier `rev` or `r`.
May be `None` to signify an `implicit post release`_.
Expand Down Expand Up @@ -258,6 +261,10 @@ class Version:
The separator before the develepment release identifier.
.. attribute:: dev_sep2
The separator between the development release identifier and number.
.. attribute:: post_tag
If this :class:`Version` instance represents a post release, this
Expand Down Expand Up @@ -322,6 +329,9 @@ class Version:
dev_sep: Optional[Separator] = attr.ib(
default=UNSET, validator=validate_sep_or_unset
)
dev_sep2: Union[Separator, UnsetType, None] = attr.ib(
default=UNSET, validator=validate_sep_or_unset
)
post_tag: Optional[PostTag] = attr.ib(default=UNSET, validator=validate_post_tag)

epoch_implicit: bool = attr.ib(default=False, init=False)
Expand Down Expand Up @@ -429,10 +439,15 @@ def _validate_dev(self, set_: Callable[[str, Any], None]) -> None:
elif self.dev is None:
if self.dev_sep is not UNSET:
raise ValueError("Cannot set dev_sep without dev.")
if self.dev_sep2 is not UNSET:
raise ValueError("Cannot set dev_sep2 without dev.")

if self.dev_sep is UNSET:
set_("dev_sep", None if self.dev is None else ".")

if self.dev_sep2 is UNSET:
set_("dev_sep2", None)

@classmethod
def parse(cls, version: str, strict: bool = False) -> "Version":
"""
Expand Down Expand Up @@ -479,6 +494,7 @@ def parse(cls, version: str, strict: bool = False) -> "Version":
elif isinstance(s, segment.Dev):
kwargs["dev"] = s.value
kwargs["dev_sep"] = s.sep
kwargs["dev_sep2"] = s.sep2
elif isinstance(s, segment.Local):
kwargs["local"] = s.value
elif isinstance(s, segment.V):
Expand Down Expand Up @@ -534,6 +550,8 @@ def __str__(self) -> str:
if self.dev_sep is not None:
parts.append(self.dev_sep)
parts.append("dev")
if self.dev_sep2:
parts.append(self.dev_sep2)
if not self.dev_implicit:
parts.append(str(self.dev))

Expand Down Expand Up @@ -663,6 +681,7 @@ def _attrs_as_init(self) -> Dict[str, Any]:
if self.dev is None:
del d["dev"]
del d["dev_sep"]
del d["dev_sep2"]

return d

Expand All @@ -681,6 +700,7 @@ def replace(
post_sep1: Union[Separator, None, UnsetType] = UNSET,
post_sep2: Union[Separator, None, UnsetType] = UNSET,
dev_sep: Union[Separator, None, UnsetType] = UNSET,
dev_sep2: Union[Separator, None, UnsetType] = UNSET,
post_tag: Union[PostTag, None, UnsetType] = UNSET,
) -> "Version":
"""Return a new :class:`Version` instance with the same attributes,
Expand All @@ -702,6 +722,7 @@ def replace(
post_sep1=post_sep1,
post_sep2=post_sep2,
dev_sep=dev_sep,
dev_sep2=dev_sep2,
post_tag=post_tag,
)
kwargs = {k: v for k, v in kwargs.items() if v is not UNSET}
Expand All @@ -726,6 +747,7 @@ def replace(

if kwargs.get("dev", UNSET) is None:
d.pop("dev_sep", None)
d.pop("dev_sep2", None)

d.update(kwargs)
return Version(**d)
Expand Down
6 changes: 5 additions & 1 deletion tests/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ def dev(draw, strict=False):

blank = just("")

num_part = num_str
sep2 = separator(strict=strict, optional=True)
if strict:
sep2 = blank

num_part = sep2.map(lambda s: s + draw(num_str))
if not strict:
num_part = one_of(blank, num_part)

Expand Down

0 comments on commit 2c43655

Please sign in to comment.