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

Version tuple parsing #273

Merged
merged 8 commits into from
Oct 15, 2021
Merged

Version tuple parsing #273

merged 8 commits into from
Oct 15, 2021

Conversation

snk4tr
Copy link
Contributor

@snk4tr snk4tr commented Oct 13, 2021

Closes #272

Proposed Changes

  • New version parsing function inspired by a popular semver parsing library
  • Corresponding tests
  • Changes in usage of parsed versions: now if version of a library is not a valid semver version (as it was in my case with gudhi), the execution does not fail with an exception, warning is provided instead.

@snk4tr snk4tr added the bug Something isn't working label Oct 13, 2021
@snk4tr snk4tr requested review from zakajd and denproc October 13, 2021 16:49
@snk4tr snk4tr self-assigned this Oct 13, 2021
Signed-off-by: Sergey Kastryulin <[email protected]>
Signed-off-by: Sergey Kastryulin <[email protected]>
Signed-off-by: Sergey Kastryulin <[email protected]>
@codecov
Copy link

codecov bot commented Oct 13, 2021

Codecov Report

Merging #273 (88e92e1) into master (66c6a5a) will decrease coverage by 0.03%.
The diff coverage is 95.12%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #273      +/-   ##
==========================================
- Coverage   93.28%   93.25%   -0.04%     
==========================================
  Files          33       33              
  Lines        2264     2283      +19     
==========================================
+ Hits         2112     2129      +17     
- Misses        152      154       +2     
Flag Coverage Δ
unittests 93.25% <95.12%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
piq/utils/common.py 92.85% <87.50%> (-3.70%) ⬇️
piq/fsim.py 100.00% <100.00%> (ø)
piq/gs.py 90.10% <100.00%> (+0.22%) ⬆️
piq/msid.py 77.19% <100.00%> (+0.13%) ⬆️
piq/srsim.py 99.00% <100.00%> (+<0.01%) ⬆️
piq/utils/__init__.py 100.00% <100.00%> (ø)
piq/vsi.py 100.00% <100.00%> (ø)

@snk4tr
Copy link
Contributor Author

snk4tr commented Oct 13, 2021

Ready for review.

Copy link
Collaborator

@zakajd zakajd left a comment

Choose a reason for hiding this comment

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

Thanks for PR, left some comments, let's discuss and then merge.

piq/srsim.py Outdated Show resolved Hide resolved
piq/utils/common.py Outdated Show resolved Hide resolved
Comment on lines +101 to +102
if isinstance(version, bytes):
version = version.decode("UTF-8")
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the use case for that? Have you ever met library whose version is encoded in raw bytes instead of string?
IMO this is redundant, but if there are such cases with raw bytes — let's keep it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Byte string is an option. Whether this option is used depends of a library's authors.
This behaviour is processed by the python-semver library, which is the reason why I decided to include it as well. I do not consider an additional sanity check to be redundant.

match = _REGEX.match(version)
if match is None:
warnings.warn(f"{version} is not a valid SemVer string")
return tuple()
Copy link
Collaborator

@zakajd zakajd Oct 14, 2021

Choose a reason for hiding this comment

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

Why return empty tuple instead of explicit None?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Practical reason: mypy won't let you do that because Optional[Tuple[int, ...]] is later used in the >= operation. The linter cannot see that the check that verifies that the value is not None is done and reports that the >= operator for (possibly) None value makes no sense — more on that in this action report.

Conceptual reason: functions that return different types and number of arguments depending on internal logic are not good because they force independent (outside) code to have a specific behavior to encounter for that. Yes, I know that in the case of an empty tuple, we add a specific check anyway, but if we didn't, it at least wouldn't crash.

Personally, I don't see the conceptual reason to be too big of a problem, but then the practical reason comes into play 😄

Comment on lines +7 to +26
_REGEX = re.compile(
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-]+)*
))?
$
""",
re.VERBOSE,
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍🏼

Copy link
Collaborator

@denproc denproc left a comment

Choose a reason for hiding this comment

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

Great regex expression! Let's unify the conditions to check that @zakajd mentioned (None vs tuple().

Signed-off-by: Sergey Kastryulin <[email protected]>
Signed-off-by: Sergey Kastryulin <[email protected]>
Signed-off-by: Sergey Kastryulin <[email protected]>
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Collaborator

@zakajd zakajd left a comment

Choose a reason for hiding this comment

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

Ok, looks good to me.
I also like the idea of returning empty tuple more than returning None 😆

@snk4tr
Copy link
Contributor Author

snk4tr commented Oct 15, 2021

@denproc done, please confirm

@snk4tr snk4tr requested a review from denproc October 15, 2021 07:57
Copy link
Collaborator

@denproc denproc left a comment

Choose a reason for hiding this comment

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

LGTM

@denproc denproc merged commit 147ec75 into master Oct 15, 2021
@denproc denproc deleted the fix/version-parsing branch April 30, 2023 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Version tuple parsing
3 participants