-
Notifications
You must be signed in to change notification settings - Fork 11
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
Allow expr_matches to better handle presence of extra data #93
Conversation
oqpy/base.py
Outdated
@@ -328,7 +330,7 @@ def expr_matches(a: Any, b: Any) -> bool: | |||
if a.keys() != b.keys(): | |||
return False | |||
return all(expr_matches(va, b[k]) for k, va in a.items()) | |||
if hasattr(a, "__dict__"): | |||
if hasattr(a, "__dict__") and type(a).__module__.startswith("oqpy"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean that oqpy
classes shouldn't be subclassed outside of these modules (vaguely what sealed
means in some other languages)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, good point I hadn't considered that. Passing in a trivial subclass probably shouldn't break things.
Thinking about it more, we only need to avoid using a == b
test when it's an instance of OQPyExpression
since it's subtypes of that class which have __eq__
overridden, so I've changed this clause to look for that instead.
I've added a test for the case of using a trivial subclass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also have added a method to allow subclasses to control how matching works if required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, thanks for the update
I am creating
oqpy
variables from some other representation, and I wanted to attach some extra data to the oqpy variables to indicate their provenance. When I did this, I got failures fromexpr_matches
since it's method of traversing the__dict__
recursively is not applicable to my data, which has reference cycles.I've modified
expr_matches
so that it short circuits on object identity, and so that it only descends intoobj.__dict__
for objects from the oqpy package, rather than any externally produced data.