Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up __eq__ #1310

Merged
merged 6 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/1310.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Speed up the generated `__eq__` methods significantly by generating a chain of attribute comparisons instead of constructing and comparing tuples.
This change arguably makes the behavior more correct,
but changes it if an attribute compares equal by identity but not value, like `float('nan')`.
5 changes: 4 additions & 1 deletion docs/comparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ For that, *attrs* writes `__eq__` and `__ne__` methods for you.

Additionally, if you pass `order=True`, *attrs* will also create a complete set of ordering methods: `__le__`, `__lt__`, `__ge__`, and `__gt__`.

Both for equality and order, *attrs* will:
For equality, *attrs* will generate a statement comparing the types of both instances,
and then comparing each attribute in turn using `==`.

For order, *attrs* will:

- Check if the types of the instances you're comparing are equal,
- if so, create a tuple of all field values for each instance,
Expand Down
14 changes: 7 additions & 7 deletions src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -1864,20 +1864,20 @@ def _make_eq(cls, attrs):
globs = {}
if attrs:
lines.append(" return (")
others = [" ) == ("]
for a in attrs:
if a.eq_key:
cmp_name = f"_{a.name}_key"
# Add the key function to the global namespace
# of the evaluated function.
globs[cmp_name] = a.eq_key
lines.append(f" {cmp_name}(self.{a.name}),")
others.append(f" {cmp_name}(other.{a.name}),")
lines.append(
f" {cmp_name}(self.{a.name}) == {cmp_name}(other.{a.name})"
)
else:
lines.append(f" self.{a.name},")
others.append(f" other.{a.name},")

lines += [*others, " )"]
lines.append(f" self.{a.name} == other.{a.name}")
if a is not attrs[-1]:
lines[-1] = f"{lines[-1]} and"
lines.append(" )")
else:
lines.append(" return True")

Expand Down
2 changes: 1 addition & 1 deletion tests/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _create_hyp_nested_strategy(draw, simple_class_strategy):
bare_attrs = st.builds(attr.ib, default=st.none())
int_attrs = st.integers().map(lambda i: attr.ib(default=i))
str_attrs = st.text().map(lambda s: attr.ib(default=s))
float_attrs = st.floats().map(lambda f: attr.ib(default=f))
float_attrs = st.floats(allow_nan=False).map(lambda f: attr.ib(default=f))
dict_attrs = st.dictionaries(keys=st.text(), values=st.integers()).map(
lambda d: attr.ib(default=d)
)
Expand Down