From 2ceef645596d53ea5c4b6d14114e726f1829d912 Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Fri, 20 Feb 2026 16:42:08 +0300 Subject: [PATCH 1/3] Fix incorrect self-comparison for InfinityType and NegativeInfinityType InfinityType.__le__ unconditionally returns False and __gt__ unconditionally returns True, which gives wrong results for self-comparison: Infinity <= Infinity returns False (should be True) and Infinity > Infinity returns True (should be False). The same issue exists for NegativeInfinityType where __lt__ always returns True and __ge__ always returns False, making NegativeInfinity < NegativeInfinity return True and NegativeInfinity >= NegativeInfinity return False. Fix by checking isinstance for these four operators, consistent with how __eq__ is already implemented. --- src/packaging/_structures.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/packaging/_structures.py b/src/packaging/_structures.py index 225e2eee0..74b759807 100644 --- a/src/packaging/_structures.py +++ b/src/packaging/_structures.py @@ -19,13 +19,13 @@ def __lt__(self, other: object) -> bool: return False def __le__(self, other: object) -> bool: - return False + return isinstance(other, self.__class__) def __eq__(self, other: object) -> bool: return isinstance(other, self.__class__) def __gt__(self, other: object) -> bool: - return True + return not isinstance(other, self.__class__) def __ge__(self, other: object) -> bool: return True @@ -48,7 +48,7 @@ def __hash__(self) -> int: return hash(repr(self)) def __lt__(self, other: object) -> bool: - return True + return not isinstance(other, self.__class__) def __le__(self, other: object) -> bool: return True @@ -60,7 +60,7 @@ def __gt__(self, other: object) -> bool: return False def __ge__(self, other: object) -> bool: - return False + return isinstance(other, self.__class__) def __neg__(self: object) -> InfinityType: return Infinity From 0b2c301530d1a0cffa4674c357c46696b238c096 Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Fri, 20 Feb 2026 18:15:59 +0300 Subject: [PATCH 2/3] Add self-comparison tests for Infinity types --- tests/test_structures.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_structures.py b/tests/test_structures.py index d85c96215..6b67d1897 100644 --- a/tests/test_structures.py +++ b/tests/test_structures.py @@ -49,10 +49,24 @@ def test_infinity_equal() -> None: assert Infinity == Infinity +def test_infinity_self_comparison() -> None: + assert not Infinity < Infinity + assert Infinity <= Infinity + assert not Infinity > Infinity + assert Infinity >= Infinity + + def test_negative_infinity_equal() -> None: assert NegativeInfinity == NegativeInfinity +def test_negative_infinity_self_comparison() -> None: + assert not NegativeInfinity < NegativeInfinity + assert NegativeInfinity <= NegativeInfinity + assert not NegativeInfinity > NegativeInfinity + assert NegativeInfinity >= NegativeInfinity + + def test_negate_infinity() -> None: assert isinstance(-Infinity, NegativeInfinity.__class__) From b090b040d3b620a502709dc4bbad8772f21b7e54 Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Sat, 21 Feb 2026 21:40:09 +0300 Subject: [PATCH 3/3] Use identity checks instead of isinstance for singleton comparison Since Infinity and NegativeInfinity are singletons, 'other is self' is both cheaper and more correct than isinstance checks. --- src/packaging/_structures.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/packaging/_structures.py b/src/packaging/_structures.py index 74b759807..74ca8f0c0 100644 --- a/src/packaging/_structures.py +++ b/src/packaging/_structures.py @@ -19,13 +19,13 @@ def __lt__(self, other: object) -> bool: return False def __le__(self, other: object) -> bool: - return isinstance(other, self.__class__) + return other is self def __eq__(self, other: object) -> bool: - return isinstance(other, self.__class__) + return other is self def __gt__(self, other: object) -> bool: - return not isinstance(other, self.__class__) + return other is not self def __ge__(self, other: object) -> bool: return True @@ -48,19 +48,19 @@ def __hash__(self) -> int: return hash(repr(self)) def __lt__(self, other: object) -> bool: - return not isinstance(other, self.__class__) + return other is not self def __le__(self, other: object) -> bool: return True def __eq__(self, other: object) -> bool: - return isinstance(other, self.__class__) + return other is self def __gt__(self, other: object) -> bool: return False def __ge__(self, other: object) -> bool: - return isinstance(other, self.__class__) + return other is self def __neg__(self: object) -> InfinityType: return Infinity