diff --git a/pylint/__pkginfo__.py b/pylint/__pkginfo__.py index 8316089ec9b..0c773da26de 100644 --- a/pylint/__pkginfo__.py +++ b/pylint/__pkginfo__.py @@ -1,5 +1,6 @@ # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html # For details: https://github.com/PyCQA/pylint/blob/master/LICENSE +from typing import Tuple from pkg_resources import DistributionNotFound, get_distribution @@ -8,5 +9,27 @@ except DistributionNotFound: __version__ = "2.8.2+" -# Kept for compatibility reason, see https://github.com/PyCQA/pylint/issues/4399 -numversion = tuple(__version__.split(".")) + +def get_numversion_from_version(v: str) -> Tuple: + """Kept for compatibility reason + + See https://github.com/PyCQA/pylint/issues/4399 + https://github.com/PyCQA/pylint/issues/4420, + """ + v = v.replace("pylint-", "") + version = [] + for n in v.split(".")[0:3]: + try: + version.append(int(n)) + except ValueError: + num = "" + for c in n: + if c.isdigit(): + num += c + else: + break + version.append(int(num)) + return tuple(version) + + +numversion = get_numversion_from_version(__version__) diff --git a/tests/test_numversion.py b/tests/test_numversion.py new file mode 100644 index 00000000000..03d1faa1217 --- /dev/null +++ b/tests/test_numversion.py @@ -0,0 +1,23 @@ +# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html +# For details: https://github.com/PyCQA/pylint/blob/master/LICENSE + +import pytest + +from pylint.__pkginfo__ import get_numversion_from_version + + +@pytest.mark.parametrize( + "version,expected_numversion", + [ + ["2.8.1", (2, 8, 1)], + ["2.8.2+", (2, 8, 2)], + ["3.0.0a0", (3, 0, 0)], + ["3.0.0b1", (3, 0, 0)], + ["3.0.0rc1", (3, 0, 0)], + ["3.0.0dev-234324234234f23abc4", (3, 0, 0)], + ["pylint-2.4.7", (2, 4, 7)], + ["2.8.3.dev3+g28c093c2.d20210428", (2, 8, 3)], + ], +) +def test_numversion(version, expected_numversion): + assert get_numversion_from_version(version) == expected_numversion