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"]