-
Notifications
You must be signed in to change notification settings - Fork 241
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?
Conversation
Fixes pypa#659 Let me know if something like this seems acceptable to you and I'll add tests and documentation. Some notes: - My use case was adding a local version to an existing version - I don't think it would make sense to allow replacing parts of the release, e.g. "major", since I don't really see a use case. Things like `v.replace(major=v.major+1)` are probably just mistakes. - This is why I don't allow replacing `epoch` either - `Version.__new__(Version)` is a little gross, as is the munging. The munging is designed to line up with the corresponding properties, so `v.replace(xyz=v.xyz)` always works.
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": |
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.
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.
ret = Version.__new__(Version) | ||
ret._version = version | ||
ret._set_key() |
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.
IIRC pathlib (?) has a pattern like
new = self.__new__(Version)
new._set_version(version)
This would probably be a bit less awkward.
The general approach looks okay to me (with Tzu-ping's comments). @pradyunsg ? FYI I converted this to a draft until there are tests and documentation. |
|
||
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 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?
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Is it intentional that replace
would bypass validation?
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.
What validation are you thinking of?
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.
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.
Same. I'm on board for this approach, assuming tests + docs + existing review comments are addressed. :) |
Hello. a = Version('1.2.3')
a.minor += 1 |
Where do you get this impression? |
Fixes #659
Let me know if something like this seems acceptable to you and I'll add tests and documentation.
Some notes:
v.replace(major=v.major+1)
are probably just mistakes since other parts don't get reset.epoch
. One could make an argument that we shouldn't allow replacingrelease
either, thoughts? (E.g. when would it make sense to replace therelease
but keep thepre
?)Version.__new__(Version)
is a little gross, as is the munging. The munging is designed to line up with the corresponding properties, sov.replace(xyz=v.xyz)
always works.