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.
| 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.
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.
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.
Is it intentional that replace would bypass validation?
There was a problem hiding this comment.
What validation are you thinking of?
There was a problem hiding this comment.
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? |
|
Unless there is an objection I might take up this shortly with a new PR. Specifically, working on improving the performance of I have some general ideas about enforcing validations of replacement input only as much as needed, but we'll see how it goes when I create a PR. |
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 replacingreleaseeither, thoughts? (E.g. when would it make sense to replace thereleasebut 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.