Skip to content

Commit 72027f6

Browse files
committed
inline now that loop is streamlined
1 parent 1eecf2a commit 72027f6

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

Lib/dataclasses.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -433,15 +433,6 @@ def _tuple_str(obj_name, fields):
433433
return f'({",".join([f"{obj_name}.{f.name}" for f in fields])},)'
434434

435435

436-
def _tuple_compare_expand(op, fields):
437-
for f in fields:
438-
yield f' if self.{f.name} != other.{f.name}:'
439-
# ? use "op[0]" here since gated by "!=", probably not worth it
440-
yield f' return self.{f.name} {op} other.{f.name}'
441-
# the instances are equal here, return constant
442-
yield f' return {op.endswith("=")}'
443-
444-
445436
class _FuncBuilder:
446437
def __init__(self, globals):
447438
self.names = []
@@ -1149,14 +1140,21 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
11491140
# if self.y != other.y:
11501141
# return self.y {op} other.y
11511142
# return {op.endswith("=")}
1152-
1153-
# __eq__ has this self guard, add here for consistency
1154-
self_guard = [' if self is other:', ' return True'] if op.endswith("=") else []
1143+
return_when_equal = f' return {op.endswith("=")}'
11551144
func_builder.add_fn(name,
11561145
('self', 'other'),
1157-
[*self_guard,
1146+
[ ' if self is other:',
1147+
# __eq__ has this self guard, add here for consistency
1148+
return_when_equal,
11581149
' if other.__class__ is self.__class__:',
1159-
*_tuple_compare_expand(op, flds),
1150+
*(
1151+
f' if self.{f.name} != other.{f.name}:\n'
1152+
# ? use "op[0]" here since gated by "!=", probably not worth confusion
1153+
f' return self.{f.name} {op} other.{f.name}'
1154+
for f in flds
1155+
),
1156+
# the instances are equal here, return constant
1157+
return_when_equal,
11601158
' return NotImplemented'],
11611159
overwrite_error='Consider using functools.total_ordering')
11621160

0 commit comments

Comments
 (0)