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

Ensure that spec is deep copiable #360

Merged

Conversation

macisamuele
Copy link
Collaborator

A bravado_core.spec.Spec instance is currently not deep copiable.
So running something like deepcopy(Spec.from_dict(<valid_specs>)) results on an unbounded recursion error.

The goal of this PR is to address this issue by defining the __deepcopy__ dunder method.

@macisamuele macisamuele force-pushed the maci-ensure-that-Spec-is-deep-copiable branch from 0ab2830 to 4caa156 Compare November 23, 2019 12:26
tests/spec/Spec/build_test.py Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Nov 24, 2019

Coverage Status

Coverage decreased (-0.5%) to 98.395% when pulling 7eea6a1 on macisamuele:maci-ensure-that-Spec-is-deep-copiable into aaf0df5 on Yelp:master.

return False

if id(self) == id(other):
return True
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it make sense to put the id check first?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👍

# all the attributes.
# NOTE: Few attributes have recursive references to Spec or do not define an equality method we're going to blacklist them
return all(
getattr(self, attr_name, None) == getattr(other, attr_name, None)
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems wrong to me - it allows the equality check to pass if one instance has the attribute set, while the other hasn't.

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'm going to update the return statement in the following way:

  • going to use a for loop instead of using all as it can simplify reading
  • going to use getattr(self, attr_name) != getattr(other, attr_name) check
  • going to catch AttributeError exception to ensure that differences in the defined attributes cause equality check to return False

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