Python 2.7 LooseVersion version comparison unexpectedly fails
I have written some tests for LooseVersion from which some seem to be failing unexpectedly.
Code:
from distutils.version import LooseVersion
failed_tests =
# the tests at hand: the bool at the end is the expected result
# 3 < 3.0: False
# 3 == 3.0: True
# 3 >= 3.0: True
# 3.0 <= 3 : True
# 3.0 == 3 : True
# 3.0 > 3 : False
# must invert the boolean logic with not for the tests in the middle...
if (LooseVersion('3') < LooseVersion('3.0')):
failed_tests.append(('3 < 3.0', False))
if not (LooseVersion('3') == LooseVersion('3.0')):
failed_tests.append(('3 == 3.0', True))
if not (LooseVersion('3') >= LooseVersion('3.0')):
failed_tests.append(('3 >= 3.0', True))
if not (LooseVersion('3.0') <= LooseVersion('3')):
failed_tests.append(('3.0 <= 3', True))
if not (LooseVersion('3.0') == LooseVersion('3')):
failed_tests.append(('3.0 == 3', True))
if (LooseVersion('3.0') > LooseVersion('3')):
failed_tests.append(('3.0 > 3', False))
if failed_tests:
print
print 'Failed tests: ' + str(len(failed_tests))
for test_string, expected_result in failed_tests:
print test_string + ': ' + str(expected_result)
print
raise ValueError('Some tests failed!')
The above tests should not be failing, but they do:
12:43:59 [INF][ print]: Failed tests: 6
12:43:59 [INF][ print]: 3 < 3.0: False
12:43:59 [INF][ print]: 3 == 3.0: True
12:43:59 [INF][ print]: 3 >= 3.0: True
12:43:59 [INF][ print]: 3.0 <= 3: True
12:43:59 [INF][ print]: 3.0 == 3: True
12:43:59 [INF][ print]: 3.0 > 3: False
QUESTION:
Why is this happening and how do you fix this? Is this a bug? Are my tests bugged?
I really stared at the code for some time, but I cannot find the reason.
See https://github.com/python/cpython/blob/master/Lib/distutils/version.py for how the regex etc. looks like.
EDIT:
StrictVersion fails with:
13:03:26 [ERR][ python]: File "C:UsersKawuAppDataRoamingMySQLWorkbenchmodulesjpa_export_plugin_grt.py", line 267, in export_jpa_annotated_classes
13:03:26 [ERR][ python]: if (StrictVersion('3') < StrictVersion('3.0')):
13:03:26 [ERR][ python]: File "C:Program FilesMySQLMySQL Workbench 8.0 CEPythonLibdistutilsversion.py", line 40, in __init__
13:03:26 [ERR][ python]: self.parse(vstring)
13:03:26 [ERR][ python]: File "C:Program FilesMySQLMySQL Workbench 8.0 CEPythonLibdistutilsversion.py", line 107, in parse
13:03:26 [ERR][ python]: raise ValueError, "invalid version number '%s'" % vstring
13:03:26 [ERR][ python]: ValueError: invalid version number '3'
I any case, I don't know why a simple number/digit wouldn't qualify as a version number...... (Java 8, Java 9, ...)
EDIT #2:
According to How do I compare version numbers in Python? StrictVersion considers simple numbers as invalid, so I cannot go with StrictVersion anyway.
As from the code docs:
.
.
.
1.0.4a3
1.0.4b1
1.0.4
The following are examples of invalid version numbers:
1 <==
2.7.2.2
1.3.a4
1.3pl1
1.3c4
The rationale for this version numbering system will be explained
in the distutils documentation.
python python-2.7 version
add a comment |
I have written some tests for LooseVersion from which some seem to be failing unexpectedly.
Code:
from distutils.version import LooseVersion
failed_tests =
# the tests at hand: the bool at the end is the expected result
# 3 < 3.0: False
# 3 == 3.0: True
# 3 >= 3.0: True
# 3.0 <= 3 : True
# 3.0 == 3 : True
# 3.0 > 3 : False
# must invert the boolean logic with not for the tests in the middle...
if (LooseVersion('3') < LooseVersion('3.0')):
failed_tests.append(('3 < 3.0', False))
if not (LooseVersion('3') == LooseVersion('3.0')):
failed_tests.append(('3 == 3.0', True))
if not (LooseVersion('3') >= LooseVersion('3.0')):
failed_tests.append(('3 >= 3.0', True))
if not (LooseVersion('3.0') <= LooseVersion('3')):
failed_tests.append(('3.0 <= 3', True))
if not (LooseVersion('3.0') == LooseVersion('3')):
failed_tests.append(('3.0 == 3', True))
if (LooseVersion('3.0') > LooseVersion('3')):
failed_tests.append(('3.0 > 3', False))
if failed_tests:
print
print 'Failed tests: ' + str(len(failed_tests))
for test_string, expected_result in failed_tests:
print test_string + ': ' + str(expected_result)
print
raise ValueError('Some tests failed!')
The above tests should not be failing, but they do:
12:43:59 [INF][ print]: Failed tests: 6
12:43:59 [INF][ print]: 3 < 3.0: False
12:43:59 [INF][ print]: 3 == 3.0: True
12:43:59 [INF][ print]: 3 >= 3.0: True
12:43:59 [INF][ print]: 3.0 <= 3: True
12:43:59 [INF][ print]: 3.0 == 3: True
12:43:59 [INF][ print]: 3.0 > 3: False
QUESTION:
Why is this happening and how do you fix this? Is this a bug? Are my tests bugged?
I really stared at the code for some time, but I cannot find the reason.
See https://github.com/python/cpython/blob/master/Lib/distutils/version.py for how the regex etc. looks like.
EDIT:
StrictVersion fails with:
13:03:26 [ERR][ python]: File "C:UsersKawuAppDataRoamingMySQLWorkbenchmodulesjpa_export_plugin_grt.py", line 267, in export_jpa_annotated_classes
13:03:26 [ERR][ python]: if (StrictVersion('3') < StrictVersion('3.0')):
13:03:26 [ERR][ python]: File "C:Program FilesMySQLMySQL Workbench 8.0 CEPythonLibdistutilsversion.py", line 40, in __init__
13:03:26 [ERR][ python]: self.parse(vstring)
13:03:26 [ERR][ python]: File "C:Program FilesMySQLMySQL Workbench 8.0 CEPythonLibdistutilsversion.py", line 107, in parse
13:03:26 [ERR][ python]: raise ValueError, "invalid version number '%s'" % vstring
13:03:26 [ERR][ python]: ValueError: invalid version number '3'
I any case, I don't know why a simple number/digit wouldn't qualify as a version number...... (Java 8, Java 9, ...)
EDIT #2:
According to How do I compare version numbers in Python? StrictVersion considers simple numbers as invalid, so I cannot go with StrictVersion anyway.
As from the code docs:
.
.
.
1.0.4a3
1.0.4b1
1.0.4
The following are examples of invalid version numbers:
1 <==
2.7.2.2
1.3.a4
1.3pl1
1.3c4
The rationale for this version numbering system will be explained
in the distutils documentation.
python python-2.7 version
add a comment |
I have written some tests for LooseVersion from which some seem to be failing unexpectedly.
Code:
from distutils.version import LooseVersion
failed_tests =
# the tests at hand: the bool at the end is the expected result
# 3 < 3.0: False
# 3 == 3.0: True
# 3 >= 3.0: True
# 3.0 <= 3 : True
# 3.0 == 3 : True
# 3.0 > 3 : False
# must invert the boolean logic with not for the tests in the middle...
if (LooseVersion('3') < LooseVersion('3.0')):
failed_tests.append(('3 < 3.0', False))
if not (LooseVersion('3') == LooseVersion('3.0')):
failed_tests.append(('3 == 3.0', True))
if not (LooseVersion('3') >= LooseVersion('3.0')):
failed_tests.append(('3 >= 3.0', True))
if not (LooseVersion('3.0') <= LooseVersion('3')):
failed_tests.append(('3.0 <= 3', True))
if not (LooseVersion('3.0') == LooseVersion('3')):
failed_tests.append(('3.0 == 3', True))
if (LooseVersion('3.0') > LooseVersion('3')):
failed_tests.append(('3.0 > 3', False))
if failed_tests:
print
print 'Failed tests: ' + str(len(failed_tests))
for test_string, expected_result in failed_tests:
print test_string + ': ' + str(expected_result)
print
raise ValueError('Some tests failed!')
The above tests should not be failing, but they do:
12:43:59 [INF][ print]: Failed tests: 6
12:43:59 [INF][ print]: 3 < 3.0: False
12:43:59 [INF][ print]: 3 == 3.0: True
12:43:59 [INF][ print]: 3 >= 3.0: True
12:43:59 [INF][ print]: 3.0 <= 3: True
12:43:59 [INF][ print]: 3.0 == 3: True
12:43:59 [INF][ print]: 3.0 > 3: False
QUESTION:
Why is this happening and how do you fix this? Is this a bug? Are my tests bugged?
I really stared at the code for some time, but I cannot find the reason.
See https://github.com/python/cpython/blob/master/Lib/distutils/version.py for how the regex etc. looks like.
EDIT:
StrictVersion fails with:
13:03:26 [ERR][ python]: File "C:UsersKawuAppDataRoamingMySQLWorkbenchmodulesjpa_export_plugin_grt.py", line 267, in export_jpa_annotated_classes
13:03:26 [ERR][ python]: if (StrictVersion('3') < StrictVersion('3.0')):
13:03:26 [ERR][ python]: File "C:Program FilesMySQLMySQL Workbench 8.0 CEPythonLibdistutilsversion.py", line 40, in __init__
13:03:26 [ERR][ python]: self.parse(vstring)
13:03:26 [ERR][ python]: File "C:Program FilesMySQLMySQL Workbench 8.0 CEPythonLibdistutilsversion.py", line 107, in parse
13:03:26 [ERR][ python]: raise ValueError, "invalid version number '%s'" % vstring
13:03:26 [ERR][ python]: ValueError: invalid version number '3'
I any case, I don't know why a simple number/digit wouldn't qualify as a version number...... (Java 8, Java 9, ...)
EDIT #2:
According to How do I compare version numbers in Python? StrictVersion considers simple numbers as invalid, so I cannot go with StrictVersion anyway.
As from the code docs:
.
.
.
1.0.4a3
1.0.4b1
1.0.4
The following are examples of invalid version numbers:
1 <==
2.7.2.2
1.3.a4
1.3pl1
1.3c4
The rationale for this version numbering system will be explained
in the distutils documentation.
python python-2.7 version
I have written some tests for LooseVersion from which some seem to be failing unexpectedly.
Code:
from distutils.version import LooseVersion
failed_tests =
# the tests at hand: the bool at the end is the expected result
# 3 < 3.0: False
# 3 == 3.0: True
# 3 >= 3.0: True
# 3.0 <= 3 : True
# 3.0 == 3 : True
# 3.0 > 3 : False
# must invert the boolean logic with not for the tests in the middle...
if (LooseVersion('3') < LooseVersion('3.0')):
failed_tests.append(('3 < 3.0', False))
if not (LooseVersion('3') == LooseVersion('3.0')):
failed_tests.append(('3 == 3.0', True))
if not (LooseVersion('3') >= LooseVersion('3.0')):
failed_tests.append(('3 >= 3.0', True))
if not (LooseVersion('3.0') <= LooseVersion('3')):
failed_tests.append(('3.0 <= 3', True))
if not (LooseVersion('3.0') == LooseVersion('3')):
failed_tests.append(('3.0 == 3', True))
if (LooseVersion('3.0') > LooseVersion('3')):
failed_tests.append(('3.0 > 3', False))
if failed_tests:
print
print 'Failed tests: ' + str(len(failed_tests))
for test_string, expected_result in failed_tests:
print test_string + ': ' + str(expected_result)
print
raise ValueError('Some tests failed!')
The above tests should not be failing, but they do:
12:43:59 [INF][ print]: Failed tests: 6
12:43:59 [INF][ print]: 3 < 3.0: False
12:43:59 [INF][ print]: 3 == 3.0: True
12:43:59 [INF][ print]: 3 >= 3.0: True
12:43:59 [INF][ print]: 3.0 <= 3: True
12:43:59 [INF][ print]: 3.0 == 3: True
12:43:59 [INF][ print]: 3.0 > 3: False
QUESTION:
Why is this happening and how do you fix this? Is this a bug? Are my tests bugged?
I really stared at the code for some time, but I cannot find the reason.
See https://github.com/python/cpython/blob/master/Lib/distutils/version.py for how the regex etc. looks like.
EDIT:
StrictVersion fails with:
13:03:26 [ERR][ python]: File "C:UsersKawuAppDataRoamingMySQLWorkbenchmodulesjpa_export_plugin_grt.py", line 267, in export_jpa_annotated_classes
13:03:26 [ERR][ python]: if (StrictVersion('3') < StrictVersion('3.0')):
13:03:26 [ERR][ python]: File "C:Program FilesMySQLMySQL Workbench 8.0 CEPythonLibdistutilsversion.py", line 40, in __init__
13:03:26 [ERR][ python]: self.parse(vstring)
13:03:26 [ERR][ python]: File "C:Program FilesMySQLMySQL Workbench 8.0 CEPythonLibdistutilsversion.py", line 107, in parse
13:03:26 [ERR][ python]: raise ValueError, "invalid version number '%s'" % vstring
13:03:26 [ERR][ python]: ValueError: invalid version number '3'
I any case, I don't know why a simple number/digit wouldn't qualify as a version number...... (Java 8, Java 9, ...)
EDIT #2:
According to How do I compare version numbers in Python? StrictVersion considers simple numbers as invalid, so I cannot go with StrictVersion anyway.
As from the code docs:
.
.
.
1.0.4a3
1.0.4b1
1.0.4
The following are examples of invalid version numbers:
1 <==
2.7.2.2
1.3.a4
1.3pl1
1.3c4
The rationale for this version numbering system will be explained
in the distutils documentation.
python python-2.7 version
python python-2.7 version
edited Nov 23 '18 at 12:17
Kawu
asked Nov 23 '18 at 12:02
KawuKawu
6,86525104171
6,86525104171
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446383%2fpython-2-7-looseversion-version-comparison-unexpectedly-fails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446383%2fpython-2-7-looseversion-version-comparison-unexpectedly-fails%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown