From f1b2b2222c8832a5a01f328afb46c9f898e67a1e Mon Sep 17 00:00:00 2001 From: Fridolin Pokorny Date: Wed, 23 Oct 2019 15:11:08 +0200 Subject: [PATCH] Introduce major, minor and micro aliases to Version --- packaging/version.py | 15 +++++++++++++++ tests/test_version.py | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packaging/version.py b/packaging/version.py index b1aff655..f39a2a12 100644 --- a/packaging/version.py +++ b/packaging/version.py @@ -401,6 +401,21 @@ def is_devrelease(self): # type: () -> bool return self.dev is not None + @property + def major(self): + # type: () -> int + return self.release[0] if len(self.release) >= 1 else 0 + + @property + def minor(self): + # type: () -> int + return self.release[1] if len(self.release) >= 2 else 0 + + @property + def micro(self): + # type: () -> int + return self.release[2] if len(self.release) >= 3 else 0 + def _parse_letter_version( letter, # type: str diff --git a/tests/test_version.py b/tests/test_version.py index 4e8079dc..67e18e9f 100644 --- a/tests/test_version.py +++ b/tests/test_version.py @@ -758,6 +758,18 @@ def test_compare_legacyversion_version(self): result = sorted([Version("0"), LegacyVersion("1")]) assert result == [LegacyVersion("1"), Version("0")] + def test_major_version(self): + assert Version("2.1.0").major == 2 + + def test_minor_version(self): + assert Version("2.1.0").minor == 1 + assert Version("2").minor == 0 + + def test_micro_version(self): + assert Version("2.1.3").micro == 3 + assert Version("2.1").micro == 0 + assert Version("2").micro == 0 + LEGACY_VERSIONS = ["foobar", "a cat is fine too", "lolwut", "1-0", "2.0-a1"]