GeezerVison is showing github.com — fixing tests across python versions. · benthomasson/deepdiff@569baa4
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: deepdiff/anyset.py +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | from ordered_set import OrderedSet | 2 | 2 | from deepdiff.deephash import DeepHash | | 3 | +from deepdiff.helper import dict_ | 3 | 4 |
| 4 | 5 |
| 5 | 6 | class AnySet: | @@ -11,8 +12,8 @@ class AnySet: | 11 | 12 | """ | 12 | 13 | def __init__(self, items=None): | 13 | 14 | self._set = OrderedSet() | 14 | | - self._hashes = {} | 15 | | - self._hash_to_objects = {} | | 15 | + self._hashes = dict_() | | 16 | + self._hash_to_objects = dict_() | 16 | 17 | if items: | 17 | 18 | for item in items: | 18 | 19 | self.add(item) | |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/deephash.py +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | convert_item_or_items_into_compiled_regexes_else_none, | 10 | 10 | get_id, type_is_subclass_of_type_group, type_in_type_group, | 11 | 11 | number_to_string, datetime_normalize, KEY_TO_VAL_STR, short_repr, | 12 | | - get_truncate_datetime, ) | | 12 | + get_truncate_datetime, dict_) | 13 | 13 | from deepdiff.base import Base | 14 | 14 | logger = logging.getLogger(__name__) | 15 | 15 |
| | 22 | 22 | MURMUR_SEED = 1203 | 23 | 23 |
| 24 | 24 | RESERVED_DICT_KEYS = {UNPROCESSED_KEY} | 25 | | -EMPTY_FROZENSET = frozenset({}) | | 25 | +EMPTY_FROZENSET = frozenset() | 26 | 26 |
| 27 | 27 | INDEX_VS_ATTRIBUTE = ('[%s]', '.%s') | 28 | 28 |
| @@ -146,7 +146,7 @@ def __init__(self, | 146 | 146 | elif isinstance(hashes, DeepHash): | 147 | 147 | self.hashes = hashes.hashes | 148 | 148 | else: | 149 | | - self.hashes = {} | | 149 | + self.hashes = dict_() | 150 | 150 | exclude_types = set() if exclude_types is None else set(exclude_types) | 151 | 151 | self.exclude_types_tuple = tuple(exclude_types) # we need tuple for checking isinstance | 152 | 152 | self.ignore_repetition = ignore_repetition | @@ -256,7 +256,7 @@ def _get_objects_to_hashes_dict(self, extract_index=0): | 256 | 256 | or a dictionary of objects to the count of items that went to build them. | 257 | 257 | extract_index=0 for hashes and extract_index=1 for counts. | 258 | 258 | """ | 259 | | - result = {} | | 259 | + result = dict_() | 260 | 260 | for key, value in self.hashes.items(): | 261 | 261 | if key in RESERVED_DICT_KEYS: | 262 | 262 | result[key] = value | |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/delta.py +7-7Lines changed: 7 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | from deepdiff.helper import ( | 7 | 7 | DICT_IS_SORTED, MINIMUM_PY_DICT_TYPE_SORTED, strings, short_repr, numbers, | 8 | 8 | np_ndarray, np_array_factory, numpy_dtypes, | 9 | | - not_found, numpy_dtype_string_to_type) | | 9 | + not_found, numpy_dtype_string_to_type, dict_) | 10 | 10 | from deepdiff.path import _path_to_elements, _get_nested_obj, GET, GETATTR | 11 | 11 | from deepdiff.anyset import AnySet | 12 | 12 |
| @@ -140,7 +140,7 @@ def __repr__(self): | 140 | 140 | return "<Delta: {}>".format(short_repr(self.diff, max_length=100)) | 141 | 141 |
| 142 | 142 | def reset(self): | 143 | | - self.post_process_paths_to_convert = {} | | 143 | + self.post_process_paths_to_convert = dict_() | 144 | 144 |
| 145 | 145 | def __add__(self, other): | 146 | 146 | if isinstance(other, numbers) and self._numpy_paths: | @@ -332,7 +332,7 @@ def _do_post_process(self): | 332 | 332 |
| 333 | 333 | def _do_pre_process(self): | 334 | 334 | if self._numpy_paths and ('iterable_item_added' in self.diff or 'iterable_item_removed' in self.diff): | 335 | | - preprocess_paths = {} | | 335 | + preprocess_paths = dict_() | 336 | 336 | for path, type_ in self._numpy_paths.items(): | 337 | 337 | preprocess_paths[path] = {'old_type': np_ndarray, 'new_type': list} | 338 | 338 | try: | @@ -497,8 +497,8 @@ def _do_ignore_order(self): | 497 | 497 | } | 498 | 498 |
| 499 | 499 | """ | 500 | | - fixed_indexes = self.diff.get('iterable_items_added_at_indexes', {}) | 501 | | - remove_indexes = self.diff.get('iterable_items_removed_at_indexes', {}) | | 500 | + fixed_indexes = self.diff.get('iterable_items_added_at_indexes', dict_()) | | 501 | + remove_indexes = self.diff.get('iterable_items_removed_at_indexes', dict_()) | 502 | 502 | paths = set(fixed_indexes.keys()) | set(remove_indexes.keys()) | 503 | 503 | for path in paths: | 504 | 504 | # In the case of ignore_order reports, we are pointing to the container object. | @@ -509,8 +509,8 @@ def _do_ignore_order(self): | 509 | 509 | else: | 510 | 510 | continue # pragma: no cover. Due to cPython peephole optimizer, this line doesn't get covered. https://github.com/nedbat/coveragepy/issues/198 | 511 | 511 | # copying both these dictionaries since we don't want to mutate them. | 512 | | - fixed_indexes_per_path = fixed_indexes.get(path, {}).copy() | 513 | | - remove_indexes_per_path = remove_indexes.get(path, {}).copy() | | 512 | + fixed_indexes_per_path = fixed_indexes.get(path, dict_()).copy() | | 513 | + remove_indexes_per_path = remove_indexes.get(path, dict_()).copy() | 514 | 514 | fixed_indexes_values = AnySet(fixed_indexes_per_path.values()) | 515 | 515 |
| 516 | 516 | new_obj = [] | |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/diff.py +14-13Lines changed: 14 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | number_to_string, datetime_normalize, KEY_TO_VAL_STR, booleans, | 21 | 21 | np_ndarray, get_numpy_ndarray_rows, OrderedSetPlus, RepeatedTimer, | 22 | 22 | skipped, TEXT_VIEW, TREE_VIEW, DELTA_VIEW, | 23 | | - np, get_truncate_datetime) | | 23 | + np, get_truncate_datetime, dict_) | 24 | 24 | from deepdiff.serialization import SerializationMixin | 25 | 25 | from deepdiff.distance import DistanceMixin | 26 | 26 | from deepdiff.model import ( | @@ -133,6 +133,7 @@ def __init__(self, | 133 | 133 | _numpy_paths=None, | 134 | 134 | _original_type=None, | 135 | 135 | **kwargs): | | 136 | + super().__init__() | 136 | 137 | if kwargs: | 137 | 138 | raise ValueError(( | 138 | 139 | "The following parameter(s) are not valid: %s\n" | @@ -145,7 +146,7 @@ def __init__(self, | 145 | 146 | "_numpy_paths, _original_type, parameters and shared_parameters.") % ', '.join(kwargs.keys())) | 146 | 147 |
| 147 | 148 | if parameters: | 148 | | - self.__dict__ = deepcopy(parameters) | | 149 | + self.__dict__.update(deepcopy(parameters)) | 149 | 150 | else: | 150 | 151 | self.ignore_order = ignore_order | 151 | 152 | ignore_type_in_groups = ignore_type_in_groups or [] | @@ -220,8 +221,8 @@ def __init__(self, | 220 | 221 | MAX_PASS_LIMIT_REACHED: False, | 221 | 222 | MAX_DIFF_LIMIT_REACHED: False, | 222 | 223 | } | 223 | | - self.hashes = {} if hashes is None else hashes | 224 | | - self._numpy_paths = {} if _numpy_paths is None else _numpy_paths | | 224 | + self.hashes = dict_() if hashes is None else hashes | | 225 | + self._numpy_paths = dict_() if _numpy_paths is None else _numpy_paths | 225 | 226 | self.shared_parameters = { | 226 | 227 | 'hashes': self.hashes, | 227 | 228 | '_stats': self._stats, | @@ -323,7 +324,7 @@ def unmangle(attribute): | 323 | 324 |
| 324 | 325 | return {i: getattr(object, unmangle(i)) for i in all_slots} | 325 | 326 |
| 326 | | - def __diff_obj(self, level, parents_ids=frozenset({}), | | 327 | + def __diff_obj(self, level, parents_ids=frozenset(), | 327 | 328 | is_namedtuple=False): | 328 | 329 | """Difference of 2 objects""" | 329 | 330 | try: | @@ -376,7 +377,7 @@ def __get_clean_to_keys_mapping(self, keys, level): | 376 | 377 |
| 377 | 378 | TODO: needs also some key conversion for groups of types other than the built-in strings and numbers. | 378 | 379 | """ | 379 | | - result = {} | | 380 | + result = dict_() | 380 | 381 | for key in keys: | 381 | 382 | if self.ignore_string_type_changes and isinstance(key, bytes): | 382 | 383 | clean_key = key.decode('utf-8') | @@ -523,14 +524,14 @@ def __iterables_subscriptable(t1, t2): | 523 | 524 | except AttributeError: | 524 | 525 | return False | 525 | 526 |
| 526 | | - def __diff_iterable(self, level, parents_ids=frozenset({}), _original_type=None): | | 527 | + def __diff_iterable(self, level, parents_ids=frozenset(), _original_type=None): | 527 | 528 | """Difference of iterables""" | 528 | 529 | if self.ignore_order: | 529 | 530 | self.__diff_iterable_with_deephash(level, parents_ids, _original_type=_original_type) | 530 | 531 | else: | 531 | 532 | self.__diff_iterable_in_order(level, parents_ids, _original_type=_original_type) | 532 | 533 |
| 533 | | - def __diff_iterable_in_order(self, level, parents_ids=frozenset({}), _original_type=None): | | 534 | + def __diff_iterable_in_order(self, level, parents_ids=frozenset(), _original_type=None): | 534 | 535 | # We're handling both subscriptable and non-subscriptable iterables. Which one is it? | 535 | 536 | subscriptable = self.__iterables_subscriptable(level.t1, level.t2) | 536 | 537 | if subscriptable: | @@ -636,7 +637,7 @@ def __create_hashtable(self, level, t): | 636 | 637 | """Create hashtable of {item_hash: (indexes, item)}""" | 637 | 638 | obj = getattr(level, t) | 638 | 639 |
| 639 | | - local_hashes = {} | | 640 | + local_hashes = dict_() | 640 | 641 | for (i, item) in enumerate(obj): | 641 | 642 | try: | 642 | 643 | parent = "{}[{}]".format(level.path(), i) | @@ -745,7 +746,7 @@ def __get_most_in_common_pairs_in_iterables( | 745 | 746 | # It also includes a "max" key that is just the value of the biggest current distance in the | 746 | 747 | # most_in_common_pairs dictionary. | 747 | 748 | most_in_common_pairs = defaultdict(lambda: defaultdict(OrderedSetPlus)) | 748 | | - pairs = {} | | 749 | + pairs = dict_() | 749 | 750 |
| 750 | 751 | pre_calced_distances = None | 751 | 752 |
| @@ -848,7 +849,7 @@ def __diff_iterable_with_deephash(self, level, parents_ids, _original_type=None) | 848 | 849 | if not self._stats[MAX_PASS_LIMIT_REACHED]: | 849 | 850 | self._stats[MAX_PASS_LIMIT_REACHED] = True | 850 | 851 | logger.warning(MAX_PASSES_REACHED_MSG.format(self.max_passes)) | 851 | | - pairs = {} | | 852 | + pairs = dict_() | 852 | 853 |
| 853 | 854 | def get_other_pair(hash_value, in_t1=True): | 854 | 855 | """ | @@ -1014,7 +1015,7 @@ def __diff_datetimes(self, level): | 1014 | 1015 | if level.t1 != level.t2: | 1015 | 1016 | self.__report_result('values_changed', level) | 1016 | 1017 |
| 1017 | | - def __diff_numpy_array(self, level, parents_ids=frozenset({})): | | 1018 | + def __diff_numpy_array(self, level, parents_ids=frozenset()): | 1018 | 1019 | """Diff numpy arrays""" | 1019 | 1020 | if level.path() not in self._numpy_paths: | 1020 | 1021 | self._numpy_paths[level.path()] = get_type(level.t2).__name__ | @@ -1092,7 +1093,7 @@ def __count_diff(self): | 1092 | 1093 | return StopIteration | 1093 | 1094 | self._stats[DIFF_COUNT] += 1 | 1094 | 1095 |
| 1095 | | - def __diff(self, level, parents_ids=frozenset({}), _original_type=None): | | 1096 | + def __diff(self, level, parents_ids=frozenset(), _original_type=None): | 1096 | 1097 | """The main diff method""" | 1097 | 1098 | if self.__count_diff() is StopIteration: | 1098 | 1099 | return | |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/distance.py +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | from deepdiff.deephash import DeepHash | 3 | 3 | from deepdiff.helper import ( | 4 | 4 | DELTA_VIEW, numbers, strings, add_to_frozen_set, not_found, only_numbers, np, np_float64, time_to_seconds, | 5 | | - cartesian_product_numpy, np_ndarray, np_array_factory, get_homogeneous_numpy_compatible_type_of_seq) | | 5 | + cartesian_product_numpy, np_ndarray, np_array_factory, get_homogeneous_numpy_compatible_type_of_seq, dict_) | 6 | 6 | from collections.abc import Mapping, Iterable | 7 | 7 |
| 8 | 8 |
| @@ -79,7 +79,7 @@ def _precalculate_numpy_arrays_distance( | 79 | 79 | if isinstance(t2_hashtable[hashes_added[0]].item, (np_ndarray, list)): | 80 | 80 | return | 81 | 81 |
| 82 | | - pre_calced_distances = {} | | 82 | + pre_calced_distances = dict_() | 83 | 83 | added = [t2_hashtable[k].item for k in hashes_added] | 84 | 84 | removed = [t1_hashtable[k].item for k in hashes_removed] | 85 | 85 |
| @@ -121,10 +121,10 @@ def _get_item_length(item, parents_ids=frozenset([])): | 121 | 121 | for key, subitem in item.items(): | 122 | 122 | # dedupe the repetition report so the number of times items have shown up does not affect the distance. | 123 | 123 | if key in {'iterable_items_added_at_indexes', 'iterable_items_removed_at_indexes'}: | 124 | | - new_subitem = {} | | 124 | + new_subitem = dict_() | 125 | 125 | for path_, indexes_to_items in subitem.items(): | 126 | 126 | used_value_ids = set() | 127 | | - new_indexes_to_items = {} | | 127 | + new_indexes_to_items = dict_() | 128 | 128 | for k, v in indexes_to_items.items(): | 129 | 129 | v_id = id(v) | 130 | 130 | if v_id not in used_value_ids: | |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/helper.py +23-3Lines changed: 23 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | import time | 8 | 8 | from ast import literal_eval | 9 | 9 | from decimal import Decimal, localcontext | 10 | | -from collections import namedtuple | | 10 | +from collections import namedtuple, OrderedDict | 11 | 11 | from itertools import repeat | 12 | 12 | from ordered_set import OrderedSet | 13 | 13 | from threading import Timer | @@ -88,6 +88,25 @@ class np_type: | 88 | 88 | MINIMUM_PY_DICT_TYPE_SORTED = Decimal('3.6') | 89 | 89 | DICT_IS_SORTED = py_current_version >= MINIMUM_PY_DICT_TYPE_SORTED | 90 | 90 |
| | 91 | + | | 92 | +class OrderedDictPlus(OrderedDict): | | 93 | + | | 94 | + def __repr__(self): | | 95 | + return str(dict(self)) | | 96 | + | | 97 | + def copy(self): | | 98 | + result = OrderedDict() | | 99 | + for k, v in self.items(): | | 100 | + result[k] = v | | 101 | + return result | | 102 | + | | 103 | + | | 104 | +if DICT_IS_SORTED: | | 105 | + dict_ = dict | | 106 | +else: | | 107 | + dict_ = OrderedDictPlus | | 108 | + | | 109 | + | 91 | 110 | if py4: | 92 | 111 | logger.warning('Python 4 is not supported yet. Switching logic to Python 3.') # pragma: no cover | 93 | 112 | py3 = True # pragma: no cover | @@ -96,6 +115,7 @@ class np_type: | 96 | 115 | sys.exit('Python 2 is not supported anymore. The last version of DeepDiff that supported Py2 was 3.3.0') | 97 | 116 |
| 98 | 117 | pypy3 = py3 and hasattr(sys, "pypy_translation_info") | | 118 | +PY_3_5 = py_current_version == Decimal('3.5') | 99 | 119 |
| 100 | 120 | strings = (str, bytes) # which are both basestring | 101 | 121 | unicode_type = str | @@ -172,10 +192,10 @@ def __repr__(self): | 172 | 192 |
| 173 | 193 |
| 174 | 194 | # Disabling remapping from old to new keys since the mapping is deprecated. | 175 | | -RemapDict = dict | | 195 | +RemapDict = dict_ | 176 | 196 |
| 177 | 197 |
| 178 | | -# class RemapDict(dict): | | 198 | +# class RemapDict(dict_): | 179 | 199 | # """ | 180 | 200 | # DISABLED | 181 | 201 | # Remap Dictionary. | |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/lfucache.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | from collections import defaultdict | 8 | 8 | from ordered_set import OrderedSet | 9 | 9 | from threading import Lock | 10 | | -from deepdiff.helper import not_found | | 10 | +from deepdiff.helper import not_found, dict_ | 11 | 11 |
| 12 | 12 |
| 13 | 13 | class CacheNode: | @@ -113,7 +113,7 @@ def insert_before_me(self, freq_node): | 113 | 113 | class LFUCache: | 114 | 114 |
| 115 | 115 | def __init__(self, capacity): | 116 | | - self.cache = {} # {key: cache_node} | | 116 | + self.cache = dict_() # {key: cache_node} | 117 | 117 | if capacity <= 0: | 118 | 118 | raise ValueError('Capacity of LFUCache needs to be positive.') # pragma: no cover. | 119 | 119 | self.capacity = capacity | |
|