Skip to content

Commit

Permalink
Models equality depends on self and other be instances of the same cl…
Browse files Browse the repository at this point in the history
…ass (isinstance instead of is)
  • Loading branch information
macisamuele committed Oct 22, 2019
1 parent d92cab7 commit 84ad71e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 5 additions & 2 deletions bravado_core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,11 @@ def __eq__(self, other):
Two model instances are equal if they have the same type and the same
properties and values (including additional properties).
"""
# Check same type as self
if type(self) is not type(other):
# 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
if not isinstance(other, self.__class__) or not isinstance(self, other.__class__):
return False

# Ignore any '_raw' keys
Expand Down
10 changes: 9 additions & 1 deletion tests/model/model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,20 @@ def test_model_issubclass_inherits_from(pet_type, cat_type):
assert issubclass(cat_type, pet_type)


def test_model_isinstance_model_class_generate_by_different_Spec_object(cat_swagger_spec, cat_type, cat_kwargs):
def test_model_isinstance_model_class_generated_by_different_Spec_object(cat_swagger_spec, cat_type, cat_kwargs):
cat = cat_type(**cat_kwargs)
new_cat_spec = Spec.from_dict(cat_swagger_spec.client_spec_dict)
assert isinstance(cat, new_cat_spec.definitions['Cat'])


def test_model_equality_if_model_class_generated_by_different_Spec_object(cat_swagger_spec, cat_type, cat_kwargs):
cat = cat_type(**cat_kwargs)
new_cat_spec = Spec.from_dict(cat_swagger_spec.client_spec_dict)
new_cat_type = new_cat_spec.definitions['Cat']
new_cat = new_cat_type(**cat_kwargs)
assert cat == new_cat


def test_model_deepcopy(user_type, user_kwargs):
user = user_type(**user_kwargs)
user_copy = deepcopy(user)
Expand Down

0 comments on commit 84ad71e

Please sign in to comment.