Skip to content

Commit cbbbea7

Browse files
committed
fixup! WIP
1 parent e2db11f commit cbbbea7

File tree

2 files changed

+31
-37
lines changed

2 files changed

+31
-37
lines changed

src/attr/_make.py

+29-34
Original file line numberDiff line numberDiff line change
@@ -556,29 +556,27 @@ def slots_setstate(self, state):
556556

557557
def add_repr(self, ns):
558558
if "__repr__" in self._cls.__dict__:
559-
meth = getattr(self._cls, "__repr__")
560-
else:
561-
meth = self._add_method_dunders(_make_repr(self._attrs, ns=ns))
559+
return self
562560

563-
self._cls_dict["__repr__"] = meth
561+
self._cls_dict["__repr__"] = self._add_method_dunders(
562+
_make_repr(self._attrs, ns=ns)
563+
)
564564
return self
565565

566566
def add_str(self):
567567
if "__str__" in self._cls.__dict__:
568-
meth = getattr(self._cls, "__str__")
569-
else:
570-
repr = self._cls_dict.get("__repr__")
571-
if repr is None:
572-
raise ValueError(
573-
"__str__ can only be generated if a __repr__ exists."
574-
)
568+
return self
575569

576-
def __str__(self):
577-
return self.__repr__()
570+
repr = self._cls_dict.get("__repr__")
571+
if repr is None:
572+
raise ValueError(
573+
"__str__ can only be generated if a __repr__ exists."
574+
)
578575

579-
meth = self._add_method_dunders(__str__)
576+
def __str__(self):
577+
return self.__repr__()
580578

581-
self._cls_dict["__str__"] = meth
579+
self._cls_dict["__str__"] = self._add_method_dunders(__str__)
582580
return self
583581

584582
def make_unhashable(self):
@@ -587,28 +585,27 @@ def make_unhashable(self):
587585

588586
def add_hash(self):
589587
if "__hash__" in self._cls.__dict__:
590-
meth = getattr(self._cls, "__hash__")
591-
else:
592-
meth = self._add_method_dunders(_make_hash(self._attrs))
588+
return self
593589

594-
self._cls_dict["__hash__"] = meth
590+
self._cls_dict["__hash__"] = self._add_method_dunders(
591+
_make_hash(self._attrs)
592+
)
595593
return self
596594

597595
def add_init(self):
598596
if "__init__" in self._cls.__dict__:
599-
meth = getattr(self._cls, "__init__")
600-
else:
601-
meth = self._add_method_dunders(
602-
_make_init(
603-
self._attrs,
604-
self._has_post_init,
605-
self._frozen,
606-
self._slots,
607-
self._super_attr_map,
608-
)
597+
return self
598+
599+
self._cls_dict["__init__"] = self._add_method_dunders(
600+
_make_init(
601+
self._attrs,
602+
self._has_post_init,
603+
self._frozen,
604+
self._slots,
605+
self._super_attr_map,
609606
)
607+
)
610608

611-
self._cls_dict["__init__"] = meth
612609
return self
613610

614611
def add_cmp(self):
@@ -618,11 +615,9 @@ def add_cmp(self):
618615
method_name = meth.__name__
619616

620617
if method_name in self._cls.__dict__:
621-
meth = getattr(self._cls, method_name)
622-
else:
623-
meth = self._add_method_dunders(meth)
618+
continue
624619

625-
cd[method_name] = meth
620+
cd[method_name] = self._add_method_dunders(meth)
626621

627622
return self
628623

tests/test_make.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1276,8 +1276,7 @@ class C2(C):
12761276
"__init__",
12771277
"__hash__",
12781278
"__repr__",
1279-
"__str__",
1280-
"__eq__",
1279+
"__str__" "__eq__",
12811280
"__ne__",
12821281
"__lt__",
12831282
"__le__",
@@ -1307,7 +1306,7 @@ class C(object):
13071306
# doesn't work
13081307
sentinel = getattr(C, method_name)
13091308

1310-
C = attr.s(C, hash=True, str=True)
1309+
C = attr.s(C, hash=True, str=(method_name == "__str__"))
13111310

13121311
assert sentinel == getattr(C, method_name)
13131312

0 commit comments

Comments
 (0)