@@ -486,6 +486,8 @@ def eq_generator(cls, funcname="__eq__"):
486486 # fmt: off
487487 code = (
488488 f"def { funcname } (self, other):\n "
489+ f" if self is other:\n "
490+ f" return True\n "
489491 f" return (\n "
490492 f" { instance_comparison } \n "
491493 f" ) if { class_comparison } else NotImplemented\n "
@@ -497,20 +499,36 @@ def eq_generator(cls, funcname="__eq__"):
497499
498500
499501def get_order_generator (cls , funcname , * , operator ):
502+ class_comparison = "self.__class__ is other.__class__"
500503 field_names = [
501504 name
502505 for name , attrib in get_fields (cls ).items ()
503506 if attrib .compare
504507 ]
505508
506- self_tuple = ", " .join (f"self.{ name } " for name in field_names )
507- other_tuple = self_tuple .replace ("self." , "other." )
509+ # Equal objects should be False for gt/lt comparisons
510+ eq_return = "True" if "=" in operator else "False"
511+
512+ instance_comparisons = [
513+ (
514+ f" if self.{ name } != other.{ name } :\n "
515+ f" return self.{ name } { operator } other.{ name } \n "
516+ )
517+ for name in field_names
518+ ]
519+ instance_comparisons .append (
520+ f" return { eq_return } "
521+ )
522+
523+ instance_comparison = "" .join (instance_comparisons )
508524
509525 # fmt: off
510526 code = (
511527 f"def { funcname } (self, other):\n "
512- f" if self.__class__ is other.__class__:\n "
513- f" return ({ self_tuple } ) { operator } ({ other_tuple } )\n "
528+ f" if self is other:\n "
529+ f" return { eq_return } \n "
530+ f" if { class_comparison } :\n "
531+ f"{ instance_comparison } \n "
514532 f" return NotImplemented\n "
515533 )
516534 # fmt: on
@@ -1057,7 +1075,7 @@ def __init__(
10571075
10581076 def __init_subclass__ (cls , frozen = False , ignore_annotations = False ):
10591077 # Subclasses of Field can be created as if they are dataclasses
1060- field_methods = {_field_init_maker , repr_maker , eq_maker }
1078+ field_methods = {_field_init_maker , repr_maker , eq_maker , replace_maker }
10611079 if frozen or _UNDER_TESTING :
10621080 field_methods |= {frozen_setattr_maker , frozen_delattr_maker }
10631081
0 commit comments