Skip to content

Commit

Permalink
Update check_object_deepcopy
Browse files Browse the repository at this point in the history
- To report more than `<generator object ...>`
- To properly exclude `str` and `unicode` on Python3 and Python2
  respectively.
- To exclude `bool` and `bytes` which are also immutable
- To log types of the offending objects in case they need to be special
  cased in the future.
  • Loading branch information
terencehonles committed Oct 19, 2021
1 parent e6a31dd commit 9f1280b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import simplejson as json
import yaml
from mock import Mock
from six import binary_type
from six import iteritems
from six import iterkeys
from six import text_type
from six.moves.urllib import parse as urlparse
from six.moves.urllib.request import pathname2url
from six.moves.urllib.request import url2pathname
Expand Down Expand Up @@ -302,12 +304,15 @@ def check_object_deepcopy(obj):
)
if id(getattr(obj, attr_name, None)) == id(getattr(obj_copy, attr_name, None))
}
assert not any(
# This is an `any` check, but it will save the offending values and allow pytest to report them
immutable_types = (type(None), bool, binary_type, text_type, tuple)
assert not {
attr_name: (attr_value, type(attr_value))
for attr_name, attr_value in iteritems(attributes_with_same_id)
# If `attr_name: attr_value` is in attributes_with_same_id then attr_value id did not change
# after deepcopy. As immutable types do not create new instances for deepcopy we need to ensure
# that all the occurrences of "same-id" are related to immutable types.
not isinstance(attr_value, (type(None), str, tuple))
for attr_name, attr_value in iteritems(attributes_with_same_id)
)
if not isinstance(attr_value, immutable_types)
}

return obj_copy

0 comments on commit 9f1280b

Please sign in to comment.