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

Update Model.__eq__ logic #355

Merged

Conversation

macisamuele
Copy link
Collaborator

Model equality check was implemented by ensuring that two objects of the same class and with the same "dict" representation are considered the same.

As far as this definition is valid we might do it a bit better.
Currently, isinstance and issubclass checks do consider equivalent model types generated by different bravado_core.spec.Spec instances if they are created out of the same specs (same url and content).
So what can happen is that

instance1 = spec1.definitions['model1'](...)
instance2 = spec2.definitions['model1']model1(...)

assert isinstance(instance1, instance2.__class__)
assert isinstance(instance2, instance1.__class__)
assert instance1._as_dict() == instance2._as_dict()
assert instance1 == instance2  # This would fail with the currently released bravado-core and succeed after this PR

The added test, test_model_equality_if_model_class_generated_by_different_Spec_object, will ensure that regression will be catched if the behaviour will change and it currently fails if the changes in bravado_core/model.py are reverted

@coveralls
Copy link

coveralls commented Oct 22, 2019

Coverage Status

Coverage decreased (-0.1%) to 98.895% when pulling 820ed6c on macisamuele:maci-update-equality-condition-of-models into d92cab7 on Yelp:master.

# Double check is needed to properly deal with polymorphic models
# Example: Dog is a discriminated version of Animal.
# assert isinstance(dog, Animal) is True
# assert isinstance(animal, Dog) is False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how this comment relates to the check you're doing, or the issue you're fixing with this PR. How does this help with two different Model classes (which are for the same model)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was initially implementing the check as if not isinstance(other, self.__class__): return False but I was noticing that in case of polymorphic models (definitions that take advantage of polymorphism) we are not short-circuiting the evaluation at the "type" level.

But the more I read it, the more I think that we could be fine on not short-circuiting the evaluation as different types would have different value of the discriminator field and so the subsequent check would catch such cases.

Said so, I'm going to simplify this so we remove eventual ambiguity.

@macisamuele macisamuele force-pushed the maci-update-equality-condition-of-models branch from 84ad71e to 820ed6c Compare October 23, 2019 09:50
@macisamuele macisamuele merged commit b7e52cb into Yelp:master Oct 23, 2019
@macisamuele macisamuele deleted the maci-update-equality-condition-of-models branch October 23, 2019 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants