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

Allow modifying versions #698

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/packaging/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def __ne__(self, other: object) -> bool:
:meta hide-value:
"""

_SENTINEL: Any = object()


class Version(_BaseVersion):
"""This class abstracts handling of a project's versions.
Expand Down Expand Up @@ -210,7 +212,9 @@ def __init__(self, version: str) -> None:
dev=_parse_letter_version(match.group("dev_l"), match.group("dev_n")),
local=_parse_local_version(match.group("local")),
)
self._set_key()

def _set_key(self) -> None:
# Generate a key which will be used for sorting
self._key = _cmpkey(
self._version.epoch,
Expand All @@ -221,6 +225,45 @@ def __init__(self, version: str) -> None:
self._version.local,
)

def replace(
self,
release: Tuple[int, ...] = _SENTINEL,
pre: Optional[Tuple[str, int]] = _SENTINEL,
post: Optional[int] = _SENTINEL,
dev: Optional[int] = _SENTINEL,
local: Optional[str] = _SENTINEL,
) -> "Version":
Comment on lines +228 to +235
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def replace(
self,
release: Tuple[int, ...] = _SENTINEL,
pre: Optional[Tuple[str, int]] = _SENTINEL,
post: Optional[int] = _SENTINEL,
dev: Optional[int] = _SENTINEL,
local: Optional[str] = _SENTINEL,
) -> "Version":
def replace(
self,
*,
release: Tuple[int, ...] = _SENTINEL,
pre: Optional[Tuple[str, int]] = _SENTINEL,
post: Optional[int] = _SENTINEL,
dev: Optional[int] = _SENTINEL,
local: Optional[str] = _SENTINEL,
) -> "Version":

Let’s make these keyword-only, I don’t think using those as positional makes sense.

"""Return a new Version object with the given version parts replaced.

>>> Version("1.0a1").replace(pre="b2")
<Version('1.0b2')>
>>> Version("1.0").replace(dev="0")
<Version('1.0.dev0')>
>>> Version("1.0").replace(local="foo")
<Version('1.0+foo')>
"""

version = self._version
if release is not _SENTINEL:
version = version._replace(release=release)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to use a kwargs dict or a partial here instead of repeatedly recreating the version tuple?

if pre is not _SENTINEL:
version = version._replace(pre=pre)
if post is not _SENTINEL:
version = version._replace(
post=("post", post) if post is not None else None
)
if dev is not _SENTINEL:
version = version._replace(dev=("dev", dev) if dev is not None else None)
if local is not _SENTINEL:
version = version._replace(
local=_parse_local_version(local) if local is not None else None
)

ret = Version.__new__(Version)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that replace would bypass validation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What validation are you thinking of?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation that happens when the version string is parsed: https://github.com/hauntsaninja/packaging/blob/1fbd2d4ba66dfa6908c59d10ace94524c295cd55/src/packaging/version.py#L200-L202. For example, you could set pre='abc', but that's not a valid pre-release, and it would raise an error if the version was stringified and reparsed.

ret._version = version
ret._set_key()
Comment on lines +262 to +264
Copy link
Member

@uranusjr uranusjr Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC pathlib (?) has a pattern like

new = self.__new__(Version)
new._set_version(version)

This would probably be a bit less awkward.

return ret

def __repr__(self) -> str:
"""A representation of the Version that shows all internal state.

Expand Down