Repository navigation
Description
Situation
The min_ver and max_ver functions have currently this signature:
def max_ver(ver1, ver2):
# ...
def min_ver(ver1, ver2):
# ...In other words, these functions allow to compare exactly two versions. However, there may be situations, where you have a list of versions. This use case could not be done with the current implementation and needs a cumbersome for-loop.
As we have comparison operators, there is no need for this functions anymore. Of course, this works only for VersionInfo types, not strings. If someone still wants to compare strings, we could document a replacement.
Action Items:
- add deprecation warning for both functions
- document replacement; maybe something like:
max(map(semver.VersionInfo.parse, ['1.1.0', '1.2.0', '2.1.0', '0.5.10', '0.4.99']))
Old issue:
Suggested Solution
To allow to compare more than two versions, it would be useful to change the above signature like this:
def max_ver(ver1, ver2, *others):
# ...
def min_ver(ver1, ver2, *others):
# ...This would resemble the builtin functions min and max. The above functions could be used like this:
from semver import max_ver
m = max_ver("3.0.0", "2.0.0", "1.0.0", "4.0.0")
print(m)
# => "1.0.0"Benefits:
- No incompatible changes to API; it's still possible to use two arguments
- Only minimal changes in the implementation
Alternative Solution
The proposed solution may has some drawbacks:
- it doesn't allow to pass an iterable (like the builtin functions
minandmax). - could hit a performance bottleneck
If you don't like the proposed solution, we could:
- leave the current implementation of
min_verandmax_veruntouched, or, - allow iterables in
min_verandmax_ver(would introduce incompatible API changes), - introduce additional functions with the suffix
_iterwhich allows to optimize for iterables. The signature could be:
def max_ver_iter(iterable, *, default=None, key=None):
"""Returns the biggest version from an iterable
:param iterable: an iterable with version strings
:param default: specifies an object to return if the provided iterable is empty.
:param key: specifies a one-argument ordering function like that used for list.sort()
:raises: ValueError (If the iterable is empty and default is not provided)
"""Questions
- Does it make sense?
- Should we raise something if the
*otherstuple contains an invalid version? - Should we ignore invalid versions? Or provide a flag (something like
raise_if_invalid) which can be set? - Anything else?
Metadata
Metadata
Assignees
Labels
Type
Fields
SIze
Activity
ppkt commentedon Oct 7, 2019
@tomschr it's a quite interesting idea. I like it :)
In my opinion both solutions looks good but personally I prefer the first one - but I wonder if it's possible to use single dispatch here.
EDIT:
Unfortunately, it seems that it's a new feature in Python 3.4+ (it could be installed via pip but it's an external dependency):
https://www.python.org/dev/peps/pep-0443/
https://pypi.org/project/singledispatch/
tomschr commentedon Oct 7, 2019
@ppkt
Thank you. :)
Ahh, right, you probably mean the one from functool.singledispatch, right?
Yes, that could also be a solution. Not sure if testing would make it a bit more complicated (which wouldn't be an argument against it).
tomschr commentedon Oct 7, 2019
Yes, you're right. I found it too when I scrolled down a bit. Argh.
I've looked into the implementation and it doesn't look trivial.
ppkt commentedon Oct 7, 2019
@tomschr Sorry, I wasn't clear - I was talking about your "Suggested solution" - i.e. adding
*otherargument :)I also think that it would be great to have an ability to provide a iterable instead of list of versions and I thought that it could be possible with feature called "singledispatch" but, unfortunately, as long as we support Python 2.7 it's not possible without an external package
tomschr commentedon Oct 7, 2019
@ppkt No problem. :-)
Yes, that's unfortunate.
Theoretically, if we don't want to rely on an external package, we could copy the missing feature and apply it for Python 2.7 only. I know, it would be an ugly solution and only for such a small function would be probably "mad". 😆
By the way, do we have any plans when we are "sunsetting" the support for Python 2.7? Next year, when it's officially not supported anymore?
I know, it's a different issue, but I haven't found any information about how we want to deal with that. Maybe we should open a new issue and document that somewhere (in the README, our documentation etc.)?
ppkt commentedon Oct 7, 2019
@tomschr I would prefer to avoid including external library in our code ;)
Regarding sunsetting Py2.7 - I have no idea. Since semver is quite simple library I think we should keep support for older Python versions as long as it's possible but we should definitely think about moving forward. Maybe releasing version 3.x with dropped support for older versions and refactored to use modern features used in language but keep support for bugfixes in 2.x branch?
tomschr commentedon Oct 7, 2019
@ppkt
Mee too! Just playing devils advocate here. 😉
I've opened issue #161 for this to clarify. Funny, we had the same idea. 😉 👍
tomschr commentedon Oct 7, 2019
@ppkt By the way, I maybe found a workaround for this "singledispatch". As we can't use
functools.singledispatch(not available for Python 2.7), we could circumvent it. 😉The
pkgutil.simplegenericis (as the name implies) a simple generic "singledispatch". It is available for Python 2.7 and beyond. Strangely, it's not documented (neither for 2.7, nor for 3.x).Here is a short demonstration:
Would that be an option?
Once we remove support for Python 2.7, we can change it to
functools.singledispatchif needed.ppkt commentedon Oct 7, 2019
@tomschr neat :) It seems that it's some kind of backport of
singledispatch:https://github.com/python/cpython/blob/2.7/Lib/pkgutil.py#L28
https://github.com/python/cpython/blob/3.4/Lib/pkgutil.py#L3
tomschr commentedon Oct 13, 2019
After reading the issue again, I believe we should (if at all) implement the changes in 3.0.0. That makes it easier for us and we can use the
functools.singledispatchfunction (if we want to use it).Could we mark this feature for 3.0.0? Maybe with a GH milestone?
s-celles commentedon Oct 14, 2019
What about tagging?
s-celles commentedon Oct 14, 2019
I must admit I have never use GH milestone previously. What are the pros and cons compared to tagging issues?
tomschr commentedon Oct 14, 2019
@scls19fr
Good question! 👍 After thinking about again, we have three options:
are a lightweight way to associate an issue with a name. You can filter issues.
It's another way to structure and associate your issue. An issue now "belongs" to a milestone. If you assign your issues to a specific release, it shows you a percentage value about to what degree the release is ready. Milestones can be closed and reopened like issues.
It's a bit like a Trello board (if you know that Web site) and a way for project managers to get an overview. You can sort your issues into, for example, TODOs (open issues), Doing (issues that are being worked on), and Done (merged/closed issues) columns. You can have more than one board (for example, a board for a 2.9.x release, another board for the 3.x.y release).
I've sorted them from easy to more advanced. Labels are the easiest way, projects the most advanced, and milestones are in the middle.
All options are a good method to structure your issues. I guess, it's also a matter of taste.
You can continue to use labels (as you already did that). If we find out this is not enough, we can try milestones. 😉
tlaferriere commentedon Jun 9, 2020
Have you tried just using the builtin
max()function? It works quite well. I believe this is possible due to the implementation of__lt__and__gt__.tomschr commentedon Jun 10, 2020
Ahh, thanks for the hint, Thomas! 👍 Even better! I haven't looked at this issue for some time.
You are right, with the help of the
__lt__and__gt__operators, it makes comparisons easier. I guess, there is no need for themax_verandmin_verfunctions anymore.[-]Idea: Extend min_ver/max_ver to allow additional use case[/-][+]Deprecate min_ver/max_ver and use builtin max()/min() functions[/+]tlaferriere commentedon Jun 11, 2020
Actually, I was about to open a PR to deprecate these functions, but as I was editing the docs to reflect this, I realised that if we do that we will lose a use case: when you want to compare two strings (or dict, tuple) directly without having to call
semver.parse.I wonder if it would be acceptable to lose this functionality for the sake of cleaner code 🤔
tlaferriere commentedon Jun 12, 2020
@tomschr maybe we could simply change these functions to use the builtins behind the scenes?
I don't want to go about deprecating things and then someone comes up and says that the new workflow is unacceptable and that we need to change it back.
tomschr commentedon Jun 12, 2020
Do you mean to use
max(ormin) insidemax_ver/min_ver? Yes, why not. Do you have an idea?Sure. 👍 Maybe we should combine it with documentation?
tlaferriere commentedon Jun 13, 2020
I think using map like you suggested with
*argswould be relatively efficient to implement an arbitrary number of arguments for this function. It would be backwards compatible too 😉.With some type checking we can pretty easily implement converting from strings, tuples and dictionaries.
tomschr commentedon Jun 13, 2020
Sorry, it was probably too late and I was too tired when I wrote my last comment. My bad, sorry.
After rethinking it a bit, I need to correct and amend my comment. Maybe we need to clarify what we want to do with the
max_ver/min_verfunctions in general. We started by declaring both functions as deprecated. That's a good start. 👍If I'm not mistaken, we have the following options:
mapandmax).Solution 1 would be obviously the least destructive and most compatible. It would also mean we could focus more on documentation.
Solution 2 is something in the middle. On the other hand, why should we change a deprecated function and add functionality before we remove it? As we don't change the function signature, we have only two arguments. Changing the implementation doesn't add much value.
Solution 3 is also similar to solution 2, but changes the function signature. I'm a bit concerned about adding functionality to an already deprecated function.
So all in all,
max_ver/min_veris mostly used for old code.After thinking about all the options above, weight the pros and cons... I'm leaning towards the first solution. Maybe we should leave the function "as is", but focus more on improving the documentation.
As we declared both functions as deprecated, we shouldn't add additional functionality to it. Especially when we favor the builtins
max/minas a better solution for new code.So to summarize it, my idea would be:
@deprecateddecorator intact)... deprecated::directive as shown informat_version, for example.max_ver/min_verand the replacement.max_ver/min_ver.max/minin combination with a VersionInfo object.map.max/minworks best when all versions are of the same type.What do you think? Would that be a meaningful approach?
tlaferriere commentedon Jun 13, 2020
Actually the PR (#264 ) to deprecate these functions hasn't been merged yet, so these functions aren't deprecated yet 😉.
As such, the current state of these functions makes them not very useful. Improving could have potential to make them more useful, but if we want to stick to the bare essentials and keep the fluff at a minimum, then maybe the best way to go is to deprecate these functions and make the documentation super clear about how you can use the
VersionInfothanks to it's magic functions.If I am feeling the vibe of this project right, I think we want to keep it simple, yet powerful.
As such, these are the only two public module functions left that have not been deprecated. I think this might be a sign that we should focus more on having a powerful class that is well documented than having a bunch of utility functions that are probably little used shortcuts for use cases that we can fulfill simply by knowing what
VersionInfois really capable of 💪.Definitely 😃
tomschr commentedon Jun 13, 2020
Haha, yes, that's true. But almost. 😉
That's also my feeling. Or maybe someone else comes up that it indeed fulfill a certain need.
I agree with you. Probably having a good and powerful VersionInfo class does make things easier. A lot of of these comparisons cannot be done with pure version strings, for example.
There was some concern in another issue that some shortcuts (like
semver.compare) are shorter to write. That could may be the case. Not sure how to handle this case yet.👍