From 11fc9d81139003e5510eff34fe6ad7bcf93683dc Mon Sep 17 00:00:00 2001 From: Kayokoi <113896798+wozhenbang2004@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:29:56 +0800 Subject: [PATCH 1/2] catalog: Fix comparisons with unrelated object types Return NotImplemented so Python can apply reflected comparisons and equality or ordering fallbacks. --- babel/messages/catalog.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py index 0e7417020..8ccb93cc8 100644 --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -209,21 +209,33 @@ def values_to_compare(obj): return _cmp(values_to_compare(self), values_to_compare(other)) def __gt__(self, other: object) -> bool: + if not isinstance(other, Message): + return NotImplemented return self.__cmp__(other) > 0 def __lt__(self, other: object) -> bool: + if not isinstance(other, Message): + return NotImplemented return self.__cmp__(other) < 0 def __ge__(self, other: object) -> bool: + if not isinstance(other, Message): + return NotImplemented return self.__cmp__(other) >= 0 def __le__(self, other: object) -> bool: + if not isinstance(other, Message): + return NotImplemented return self.__cmp__(other) <= 0 def __eq__(self, other: object) -> bool: + if not isinstance(other, Message): + return NotImplemented return self.__cmp__(other) == 0 def __ne__(self, other: object) -> bool: + if not isinstance(other, Message): + return NotImplemented return self.__cmp__(other) != 0 def is_identical(self, other: Message) -> bool: From bdd6d586bc78a219b474d07049ac8be733fb1879 Mon Sep 17 00:00:00 2001 From: Kayokoi <113896798+wozhenbang2004@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:31:53 +0800 Subject: [PATCH 2/2] catalog: Test comparison fallbacks for unrelated types Cover unrelated operands in both directions and Python reflected equality. --- tests/messages/test_catalog.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/messages/test_catalog.py b/tests/messages/test_catalog.py index 2eaa508cb..16a307ab6 100644 --- a/tests/messages/test_catalog.py +++ b/tests/messages/test_catalog.py @@ -12,6 +12,7 @@ import copy import datetime +import operator import pickle from io import StringIO @@ -22,6 +23,36 @@ from babel.util import FixedOffsetTimezone +@pytest.mark.parametrize('other', [None, 1, 'foo', object()]) +def test_message_equality_with_other_types(other): + message = catalog.Message('foo') + assert not (message == other) + assert not (other == message) + assert message != other + assert other != message + + +@pytest.mark.parametrize('other', [None, 1, 'foo', object()]) +@pytest.mark.parametrize('compare', [operator.lt, operator.le, operator.gt, operator.ge]) +def test_message_ordering_with_other_types(other, compare): + message = catalog.Message('foo') + with pytest.raises(TypeError): + compare(message, other) + with pytest.raises(TypeError): + compare(other, message) + + +def test_message_equality_uses_reflected_comparison(): + class EqualToMessage: + def __eq__(self, other): + return isinstance(other, catalog.Message) + + message = catalog.Message('foo') + other = EqualToMessage() + assert message == other + assert not (message != other) + + def test_message_python_format(): assert catalog.PYTHON_FORMAT.search('foo %d bar') assert catalog.PYTHON_FORMAT.search('foo %s bar')