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

Add caching to value_equality_values decorator for auto generated methods. #6275

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions cirq-core/cirq/value/value_equality_attr.py
Expand Up @@ -17,7 +17,7 @@

from typing_extensions import Protocol

from cirq import protocols
from cirq import protocols, _compat


class _SupportsValueEquality(Protocol):
Expand Down Expand Up @@ -221,13 +221,23 @@ class return the existing class' type.
)
else:
setattr(cls, '_value_equality_values_cls_', lambda self: cls)
setattr(cls, '__hash__', None if unhashable else _value_equality_hash)
setattr(cls, '_value_equality_values_', _compat.cached_method(values_getter))
tanujkhattar marked this conversation as resolved.
Show resolved Hide resolved
setattr(cls, '__hash__', None if unhashable else _compat.cached_method(_value_equality_hash))
setattr(cls, '__eq__', _value_equality_eq)
setattr(cls, '__ne__', _value_equality_ne)

if approximate:
if not hasattr(cls, '_value_equality_approximate_values_'):
setattr(cls, '_value_equality_approximate_values_', values_getter)
setattr(
cls, '_value_equality_approximate_values_', _compat.cached_method(values_getter)
tanujkhattar marked this conversation as resolved.
Show resolved Hide resolved
)
else:
approx_values_getter = getattr(cls, '_value_equality_approximate_values_')
setattr(
cls,
'_value_equality_approximate_values_',
_compat.cached_method(approx_values_getter),
)
setattr(cls, '_approx_eq_', _value_equality_approx_eq)

return cls
Expand Down