Skip to content

Commit 2443aa9

Browse files
committed
Special-case single field comparisons in ordering methods
1 parent 6663637 commit 2443aa9

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Lib/dataclasses.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,12 +1180,14 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
11801180
if order:
11811181
# Create and set the ordering methods.
11821182
flds = [f for f in field_list if f.compare]
1183-
if len(flds) == 1:
1184-
self_expr = f"self.{flds[0].name}"
1185-
other_expr = f"other.{flds[0].name}"
1186-
else:
1187-
self_expr = _tuple_str('self', flds)
1188-
other_expr = _tuple_str('other', flds)
1183+
match flds:
1184+
# Special-case single field comparisons. See GH-144191.
1185+
case [single_fld]:
1186+
self_expr = f'self.{single_fld.name}'
1187+
other_expr = f'other.{single_fld.name}'
1188+
case _:
1189+
self_expr = _tuple_str('self', flds)
1190+
other_expr = _tuple_str('other', flds)
11891191
for name, op in [('__lt__', '<'),
11901192
('__le__', '<='),
11911193
('__gt__', '>'),

0 commit comments

Comments
 (0)