Skip to content

Commit

Permalink
Merge pull request #152 from seperman/dev
Browse files Browse the repository at this point in the history
fixing True vs 1 for dictionary keys #150
  • Loading branch information
seperman authored Jul 12, 2019
2 parents 9caed55 + 1089055 commit 269fa49
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 14 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DeepDiff v 4.0.6
# DeepDiff v 4.0.7

<!-- ![Downloads](https://img.shields.io/pypi/dm/deepdiff.svg?style=flat) -->
![Python Versions](https://img.shields.io/pypi/pyversions/deepdiff.svg?style=flat)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion deepdiff/__init__.py
Original file line number Diff line number Diff line change
@@ -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__':
Expand Down
20 changes: 17 additions & 3 deletions deepdiff/deephash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -282,15 +290,19 @@ 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):
pass
else:
return result

result = not_hashed

if self._skip_this(obj, parent):
return

Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.**
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 4.0.6
current_version = 4.0.7
commit = True
tag = True
tag_name = {new_version}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
23 changes: 22 additions & 1 deletion tests/test_diff_text.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math
import datetime
import pytest
import logging
Expand Down Expand Up @@ -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 == {}
18 changes: 16 additions & 2 deletions tests/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)}
Expand Down

0 comments on commit 269fa49

Please sign in to comment.