From b2292921a1de9ef19d21d4274809aa71962e8a93 Mon Sep 17 00:00:00 2001 From: Vineet Kumar Date: Sun, 25 Jan 2026 21:00:04 +0530 Subject: [PATCH 1/4] Enhance ordering methods in dataclasses for single field cases --- Lib/dataclasses.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 730ced7299865e..3b3e985d7dd848 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -1180,21 +1180,25 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, if order: # Create and set the ordering methods. flds = [f for f in field_list if f.compare] - self_tuple = _tuple_str('self', flds) - other_tuple = _tuple_str('other', flds) + if len(flds) == 1: + self_expr = f"self.{flds[0].name}" + other_expr = f"other.{flds[0].name}" + else: + self_expr = _tuple_str('self', flds) + other_expr = _tuple_str('other', flds) for name, op in [('__lt__', '<'), ('__le__', '<='), ('__gt__', '>'), ('__ge__', '>='), ]: # Create a comparison function. If the fields in the object are - # named 'x' and 'y', then self_tuple is the string - # '(self.x,self.y)' and other_tuple is the string + # named 'x' and 'y', then self_expr is the string + # '(self.x,self.y)' and other_expr is the string # '(other.x,other.y)'. func_builder.add_fn(name, ('self', 'other'), [ ' if other.__class__ is self.__class__:', - f' return {self_tuple}{op}{other_tuple}', + f' return {self_expr}{op}{other_expr}', ' return NotImplemented'], overwrite_error='Consider using functools.total_ordering') From 2443aa95ecde0f2a65a7cbbcc0fa52ea2bf619fb Mon Sep 17 00:00:00 2001 From: Vineet Kumar Date: Mon, 26 Jan 2026 20:18:56 +0530 Subject: [PATCH 2/4] Special-case single field comparisons in ordering methods --- Lib/dataclasses.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 3b3e985d7dd848..5a7c7678e5f2dd 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -1180,12 +1180,14 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, if order: # Create and set the ordering methods. flds = [f for f in field_list if f.compare] - if len(flds) == 1: - self_expr = f"self.{flds[0].name}" - other_expr = f"other.{flds[0].name}" - else: - self_expr = _tuple_str('self', flds) - other_expr = _tuple_str('other', flds) + match flds: + # Special-case single field comparisons. See GH-144191. + case [single_fld]: + self_expr = f'self.{single_fld.name}' + other_expr = f'other.{single_fld.name}' + case _: + self_expr = _tuple_str('self', flds) + other_expr = _tuple_str('other', flds) for name, op in [('__lt__', '<'), ('__le__', '<='), ('__gt__', '>'), From 9ec5dc36a3a980c266de6a02ccf2647d8a74c4e1 Mon Sep 17 00:00:00 2001 From: Vineet Kumar Date: Mon, 26 Jan 2026 20:19:04 +0530 Subject: [PATCH 3/4] Add tests for ordering with dataclass fields, including compare=False --- Lib/test/test_dataclasses/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Lib/test/test_dataclasses/__init__.py b/Lib/test/test_dataclasses/__init__.py index 3b335429b98500..cd4435c5b647a9 100644 --- a/Lib/test/test_dataclasses/__init__.py +++ b/Lib/test/test_dataclasses/__init__.py @@ -406,6 +406,16 @@ class C: self.assertGreaterEqual(C(1), C(0)) self.assertGreaterEqual(C(1), C(1)) + @dataclass(order=True) + class C2: + x: int + y: int = field(compare=False) + + self.assertLess(C2(0, 10), C2(1, -5)) + self.assertLessEqual(C2(1, 10), C2(1, -5)) + self.assertGreater(C2(2, -1), C2(1, 999)) + self.assertGreaterEqual(C2(1, 10), C2(1, -5)) + def test_simple_compare(self): # Ensure that order=False is the default. @dataclass From 6bf700231dee47a23b0d1d998d4e0651c0e2c65d Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 26 Jan 2026 14:51:23 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-01-26-14-51-22.gh-issue-144191.jHXZrc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-01-26-14-51-22.gh-issue-144191.jHXZrc.rst diff --git a/Misc/NEWS.d/next/Library/2026-01-26-14-51-22.gh-issue-144191.jHXZrc.rst b/Misc/NEWS.d/next/Library/2026-01-26-14-51-22.gh-issue-144191.jHXZrc.rst new file mode 100644 index 00000000000000..17224ec35d5ad9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-01-26-14-51-22.gh-issue-144191.jHXZrc.rst @@ -0,0 +1 @@ +Dataclass ordering methods were adjusted to simplify comparisons for classes with a single comparison field, aligning the generated behaviour more closely with hand-written comparison logic.