-
Notifications
You must be signed in to change notification settings - Fork 96
Allow shorter version for parsing #359
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 shorter version for parsing #359
Conversation
Thanks @OidaTiftla for your contribution! And sorry for the delay. It looks good. My only concern is the repetition of the content between the class variables If feasible, could be combine these class variables somehow? |
I looked a bit closer and I see the following issues:
Apart from the technical issue, there is a fundamental question. Should we deviate from the semver spec? Should we allow parsing of incomplete version strings? Have you seen this? IF we want to go this route, we should get rid of the two class variables and define a placeholder for the patch part like this: _REGEX = (
r"""
^
(?P<major>0|[1-9]\d*)
(?:
\.
(?P<minor>0|[1-9]\d*)
(?:
\.
(?P<patch>0|[1-9]\d*)
){}
)
(?:-(?P<prerelease>
(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)
(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*
))?
(?:\+(?P<build>
[0-9a-zA-Z-]+
(?:\.[0-9a-zA-Z-]+)*
))?
$
"""
) In the def parse((cls, version: String, optional_patch: bool = False) -> "Version":
regex = re.compile(cls._REGEX.format("?" if optional_patch else ""), re.VERBOSE)
match = regex.match(version)
if match is None:
raise ValueError(f"{version} is not valid SemVer string")
matched_version_parts: Dict[str, Any] = match.groupdict()
if not matched_version_parts['minor']:
matched_version_parts['minor'] = 0
if not matched_version_parts['patch']:
matched_version_parts['patch'] = 0
return cls(**matched_version_parts) This is just a rough draft. I'm not sure if this would be useful as we will loose the |
- Fix variable naming (snake_case and clearer name) - Add missing doc string - Fix DRY principle Co-authored-by: Tom Schraitle <tomschr@users.noreply.github.com>
Everything okay. It's open source, so don't stress.
Yes, patch and minor is optional.
I fully support this 👍, the format string solution is a very nice idea.
Ah thanks, I'll fix this.
Ok, I'll add it.
I was reading though the docs but didn't found that part before trying to adjust the code to fit my needs. So thanks for the link. What does not make me happy about this workaround is, that I have to do the parsing by my own and cannot use the library for that part. So I would copy the regex from the source code of python_semver. My opinion to the fundamental question: If the method is used with the default parameters it will still throw an error if it is not a valid semver. So I think it would be okay, but that's something you or you with the other maintainers must decide if you want that optional parameter to deviate from the spec. Users would not need the workaround from above and can still benefit from that library. For me it would be totally fine, if you close this PR and I use my fork in my current project.
I've fixed the DRY violation a bit differently, you may have a look at it and report if this would keep the If you do not want to deviate from the semver spec it's also ok for me, you can simply close this PR. I will then stick to my fork for my current project, that's also fine. 😄 |
If you decide to continue this PR, I can also add some tests for the optional parsing feature. |
@OidaTiftla Thanks for your contribution! 👍
Haha, yes, you're right.
Thanks. I would even go further and be explicit. In my example, I wasn't and that could be improved with a named placeholder.
You're welcome. 🙂
Not sure if I understand it correctly, but why copying the regex? Wouldn't that be something for creating a subclass from
Thanks for your thoughts. 👍 I think, I would be okay if a) the default behavior isn't changed, b) it's clearly documented in the docstring, and c) all tests are green. You already did a and b. Look at c, there are some overly lone lines, but nothing serious. What I would like to avoid is to blow up the library to an "all-purpose-parse-whatever-shitty-version-string-you have". 😉 There is
I will come up with an additional review.
I'm happily integrate your change. Just a few minor things that I will come up in a few seconds. |
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.
@OidaTiftla Just some minor things I would like to see. If you agree, I can integrate it.
Many thanks!
Thanks for your feedback @tomschr! 👍
Yes you're right, a derived class would have access to it, but I was not explicit enough, sorry for that. My statement of copy the regex will not solve the problem, to parse version strings with optional patch and minor version parts. One must copy the regex from python_semver and adjust it. Hope I could clarify my thought a bit.
Ok. I'll look into it.
Yes, I'm totally on your site. And you as maintainer should always ask for the necessity of changes and keep the library clean 👍 |
Co-authored-by: Tom Schraitle <tomschr@users.noreply.github.com>
Co-authored-by: Tom Schraitle <tomschr@users.noreply.github.com>
@tomschr can you please trigger the workflows again? So I can see if I could fix everything. Thanks in advance. Locally in my environment there were not more errors when running And are the added tests enough or should there be more unit tests added? |
Ahh, right! Thanks for the clarification!
Done. All green! 😉
That's fine. Just in case: you can use
They are fine. 👍 I realized, I haven't looked at some parts in the documentation/changelog. I would like to add some small additions. If that's done, I'll merge them. Promised! 🙂 |
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.
@OidaTiftla Just some minor additions. Would be great if you could include them. Thanks!
Co-authored-by: Tom Schraitle <tomschr@users.noreply.github.com>
@tomschr thanks for your feedback.
Ah thank you for this, this would help 👍
It was my fault to read the |
@OidaTiftla Many thanks for your contributions! 👍
No problem. 🙂 If you have further ideas or suggestions I would happily discuss it with you. 🙂 There are also some open issues that would need some love and dedication. Also I'm working on getting out a 3.0.0 release. Not sure when this will be ready. |
For my personal use I added the possibility to parse a version string that is shorter than
major.minor.patch
, for example13.5
.When creating a semver version object using the constructor it is possible to only pass a major version and the other parameters are filled with a default value. So why not give this possibility to the parsing function.
To keep backwards compatibility and the requirement from semver that normal version strings need at least three version numbers, the parsing of shorter version strings must be enabled with a parameter. Without that parameter the old functionality will be executed.
I do not know if this is something you would like to integrate, if not simply close this pull request. Otherwise give me a short feedback, then I will update the documentation and the test suite.