From 13fbd37cd3078b1722a19f901ac6cca2bde151f7 Mon Sep 17 00:00:00 2001 From: Sep Dehpour Date: Fri, 12 Jul 2019 15:13:07 -0700 Subject: [PATCH 1/4] fixing True vs 1 for dictionary keys #150 --- deepdiff/deephash.py | 20 +++++++++++++++++--- tests/test_diff_text.py | 23 ++++++++++++++++++++++- tests/test_hash.py | 18 ++++++++++++++++-- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/deepdiff/deephash.py b/deepdiff/deephash.py index 3cb3bed4..ad544a17 100644 --- a/deepdiff/deephash.py +++ b/deepdiff/deephash.py @@ -4,7 +4,7 @@ from collections.abc import Iterable, MutableMapping from collections import defaultdict from hashlib import sha1, sha256 - +from enum import Enum from deepdiff.helper import (strings, numbers, unprocessed, not_hashed, add_to_frozen_set, convert_item_or_items_into_set_else_none, get_doc, convert_item_or_items_into_compiled_regexes_else_none, @@ -27,6 +27,11 @@ INDEX_VS_ATTRIBUTE = ('[%s]', '.%s') +class BoolObj(Enum): + TRUE = 1 + FALSE = 0 + + def prepare_string_for_hashing(obj, ignore_string_type_changes=False, ignore_string_case=False): """ Clean type conversions @@ -259,6 +264,9 @@ def _prep_iterable(self, obj, parent, parents_ids=EMPTY_FROZENSET): return result + def _prep_bool(self, obj): + return BoolObj.TRUE if obj else BoolObj.FALSE + def _prep_number(self, obj): type_ = "number" if self.ignore_numeric_type_changes else obj.__class__.__name__ if self.significant_digits is not None: @@ -282,6 +290,12 @@ def _prep_tuple(self, obj, parent, parents_ids): def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET): """The main diff method""" + if isinstance(obj, bool): + obj = self._prep_bool(obj) + result = None + else: + result = not_hashed + try: result = self[obj] except (TypeError, KeyError): @@ -289,8 +303,6 @@ def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET): else: return result - result = not_hashed - if self._skip_this(obj, parent): return @@ -314,6 +326,8 @@ def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET): elif isinstance(obj, Iterable): result = self._prep_iterable(obj=obj, parent=parent, parents_ids=parents_ids) + elif obj in {BoolObj.TRUE, BoolObj.FALSE}: + result = 'bool:true' if obj is BoolObj.TRUE else 'bool:false' else: result = self._prep_obj(obj=obj, parent=parent, parents_ids=parents_ids) diff --git a/tests/test_diff_text.py b/tests/test_diff_text.py index 06e7acc2..a9374033 100755 --- a/tests/test_diff_text.py +++ b/tests/test_diff_text.py @@ -1,6 +1,5 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import math import datetime import pytest import logging @@ -1702,3 +1701,25 @@ def test_diff_when_hash_fails(self, mock_DeepHash, mock_logger): t2 = {"blah": {4}, 2: 1337} DeepDiff(t1, t2, ignore_order=True) assert mock_logger.warning.called + + def test_bool_vs_number(self): + t1 = { + "A List": [ + { + "Value One": True, + "Value Two": 1 + } + ], + } + + t2 = { + "A List": [ + { + "Value Two": 1, + "Value One": True + } + ], + } + + ddiff = DeepDiff(t1, t2, ignore_order=True) + assert ddiff == {} diff --git a/tests/test_hash.py b/tests/test_hash.py index 808fad59..7c3a3c6d 100755 --- a/tests/test_hash.py +++ b/tests/test_hash.py @@ -3,9 +3,8 @@ import re import pytest import logging -import math from deepdiff import DeepHash -from deepdiff.deephash import prepare_string_for_hashing, unprocessed +from deepdiff.deephash import prepare_string_for_hashing, unprocessed, BoolObj from deepdiff.helper import pypy3, get_id, number_to_string from collections import namedtuple from functools import partial @@ -97,6 +96,21 @@ def test_sha1_hash_not_sensitive_to_bytecode_vs_unicode(self): class TestDeepHashPrep: """DeepHashPrep Tests covering object serialization.""" + def test_prep_bool_vs_num1(self): + assert {BoolObj.TRUE: 'bool:true'} == DeepHashPrep(True) + assert {1: 'int:1'} == DeepHashPrep(1) + + def test_prep_bool_vs_num2(self): + item1 = { + "Value One": True, + "Value Two": 1, + } + item2 = { + "Value Two": 1, + "Value One": True, + } + assert DeepHashPrep(item1)[item1] == DeepHashPrep(item2)[item2] + def test_prep_str(self): obj = "a" expected_result = {obj: prep_str(obj)} From 9908cf212965bc01c5d47c54433942c42d06f6fc Mon Sep 17 00:00:00 2001 From: Sep Dehpour Date: Fri, 12 Jul 2019 15:18:23 -0700 Subject: [PATCH 2/4] adding changelog --- README.md | 2 +- docs/index.rst | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 796d4835..ff262290 100644 --- a/README.md +++ b/README.md @@ -417,7 +417,7 @@ And then running # ChangeLog - +- v4-0-7: Hashing of the number 1 vs. True - v4-0-6: found a tiny bug in Python formatting of numbers in scientific notation. Added a workaround. - v4-0-5: Fixing number diffing. Adding number_format_notation and number_to_string_func. - v4-0-4: Adding ignore_string_case and ignore_type_subclasses diff --git a/docs/index.rst b/docs/index.rst index 9f5ab7b9..8127e6dd 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -281,6 +281,7 @@ Indices and tables Changelog ========= +- v4-0-7: Hashing of the number 1 vs. True - v4-0-6: found a tiny bug in Python formatting of numbers in scientific notation. Added a workaround. - v4-0-5: Fixing number diffing. Adding number_format_notation and number_to_string_func. - v4-0-4: Adding ignore_string_case and ignore_type_subclasses From 7b63c0b51374fabc472c6c1c7671ef13410cf264 Mon Sep 17 00:00:00 2001 From: Sep Dehpour Date: Fri, 12 Jul 2019 15:18:32 -0700 Subject: [PATCH 3/4] =?UTF-8?q?Bump=20version:=204.0.6=20=E2=86=92=204.0.7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- deepdiff/__init__.py | 2 +- docs/conf.py | 4 ++-- docs/index.rst | 2 +- setup.cfg | 2 +- setup.py | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ff262290..7e0983a4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# DeepDiff v 4.0.6 +# DeepDiff v 4.0.7 ![Python Versions](https://img.shields.io/pypi/pyversions/deepdiff.svg?style=flat) diff --git a/deepdiff/__init__.py b/deepdiff/__init__.py index 03707b99..00e4223d 100644 --- a/deepdiff/__init__.py +++ b/deepdiff/__init__.py @@ -1,6 +1,6 @@ """This module offers the DeepDiff, DeepSearch, grep and DeepHash classes.""" # flake8: noqa -__version__ = '4.0.6' +__version__ = '4.0.7' import logging if __name__ == '__main__': diff --git a/docs/conf.py b/docs/conf.py index 3639e8af..c4395784 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -60,9 +60,9 @@ # built documents. # # The short X.Y version. -version = '4.0.6' +version = '4.0.7' # The full version, including alpha/beta/rc tags. -release = '4.0.6' +release = '4.0.7' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/index.rst b/docs/index.rst index 8127e6dd..89e5bd92 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -4,7 +4,7 @@ contain the root `toctree` directive. -DeepDiff 4.0.6 documentation! +DeepDiff 4.0.7 documentation! ============================= **DeepDiff: Deep Difference of dictionaries, iterables, strings and other objects. It will recursively look for all the changes.** diff --git a/setup.cfg b/setup.cfg index 08269c06..52b5f92b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 4.0.6 +current_version = 4.0.7 commit = True tag = True tag_name = {new_version} diff --git a/setup.py b/setup.py index b3ed8079..d8215786 100755 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ if os.environ.get('USER', '') == 'vagrant': del os.link -version = '4.0.6' +version = '4.0.7' def get_reqs(filename): From 1089055fdd6fcdc24e39e16076e6480220cc6852 Mon Sep 17 00:00:00 2001 From: Sep Dehpour Date: Fri, 12 Jul 2019 15:23:21 -0700 Subject: [PATCH 4/4] adding author --- AUTHORS | 1 + README.md | 1 + 2 files changed, 2 insertions(+) diff --git a/AUTHORS b/AUTHORS index e598a258..cc85a583 100644 --- a/AUTHORS +++ b/AUTHORS @@ -20,3 +20,4 @@ Authors: - Brian Maissy (brianmaissy) for fixing classes which inherit from classes with slots didn't have all of their slots compared - Juan Soler (Soleronline) for adding ignore_type_number - mthaddon for adding timedelta diffing support +- Necrophagos for Hashing of the number 1 vs. True diff --git a/README.md b/README.md index 7e0983a4..1eb9bd20 100644 --- a/README.md +++ b/README.md @@ -503,3 +503,4 @@ Thank you! - Brian Maissy (brianmaissy) for fixing classes which inherit from classes with slots didn't have all of their slots compared - Juan Soler (Soleronline) for adding ignore_type_number - mthaddon for adding timedelta diffing support +- Necrophagos for Hashing of the number 1 vs. True