-
Notifications
You must be signed in to change notification settings - Fork 250
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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, | ||
|
@@ -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": | ||
"""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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be better to use a kwargs dict or a |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it intentional that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What validation are you thinking of? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
ret._version = version | ||
ret._set_key() | ||
Comment on lines
+262
to
+264
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let’s make these keyword-only, I don’t think using those as positional makes sense.