Extending attrs-generated `__eq__`
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 42/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- python
- Domain
- developer-experience
Research direction
Start at attrs' generated eq entry point and compare it with the attrs_init extension path shown in the issue. Use the provided X and magic_object example to define the expected behavior, including preserving generated field comparisons while allowing custom equality logic.
Written by the indexing model from the issue text.
Description
Hi, I fear that there is probably a super obvious answer to this question, but I somehow cannot wrap my head around it: is there any way how I can "extend" the attrs-generated __eq__ method, similar to how it's possible for __init__ by calling __attrs_init__? 🤔
Here just a dummy example to illustrate the situation. Right now, the only way I can get it work is by writing the boilerplate myself.
from attrs import define
magic_object = object()
"""Is equal to all objects."""
@define
class X:
here: int
are: float
many: str
fields: bool
def __eq__(self, other):
if other is magic_object:
return True
# Ideally, I want to call something like:
# return __attrs_eq__(other)
# Instead, I have this boilerplate code!
return (
super().__eq__(other)
and isinstance(other, X)
and self.here == other.here
and self.are == other.are
and self.many == other.many
and self.fields == other.fields
)
assert X(0, 0.0, "a", True) == X(0, 0.0, "a", True)
assert X(0, 0.0, "a", True) != X(0, 0.0, "a", False)
assert X(0, 0.0, "a", True) == magic_object
- Dominant language
- Python
- Stars
- 5.8k
- Forks
- 490
- Avg merge
- 3d 12h
- Merged PRs (30d)
- 3
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python-attrs/attrs
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
python-attrs/attrs#1627 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
python-attrs/attrs#1637 ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 20/100
python-attrs/attrs#1620 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
python-attrs/attrs#1596 · 2 comments · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
python-attrs/attrs#1549 · 3 comments ·
Maintainers usually reply within 1 day
All issues in python-attrs/attrs
Similar issues
-
P4: low tooling
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
jeffknupp/association#318 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
petercorke/robotics-toolbox-python#709 ·
Maintainers usually reply within 2 days
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
MakerYuichi/Aegis-pro#114 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
mpfaffenberger/code_puppy#985 · 2 comments ·
Maintainers usually reply within 1 day