Skip to content

Commit 269fa49

Browse files
authored
Merge pull request #152 from seperman/dev
fixing True vs 1 for dictionary keys #150
2 parents 9caed55 + 1089055 commit 269fa49

10 files changed

+66
-14
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ Authors:
2020
- Brian Maissy (brianmaissy) for fixing classes which inherit from classes with slots didn't have all of their slots compared
2121
- Juan Soler (Soleronline) for adding ignore_type_number
2222
- mthaddon for adding timedelta diffing support
23+
- Necrophagos for Hashing of the number 1 vs. True

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# DeepDiff v 4.0.6
1+
# DeepDiff v 4.0.7
22

33
<!-- ![Downloads](https://img.shields.io/pypi/dm/deepdiff.svg?style=flat) -->
44
![Python Versions](https://img.shields.io/pypi/pyversions/deepdiff.svg?style=flat)
@@ -417,7 +417,7 @@ And then running
417417

418418
# ChangeLog
419419

420-
420+
- v4-0-7: Hashing of the number 1 vs. True
421421
- v4-0-6: found a tiny bug in Python formatting of numbers in scientific notation. Added a workaround.
422422
- v4-0-5: Fixing number diffing. Adding number_format_notation and number_to_string_func.
423423
- v4-0-4: Adding ignore_string_case and ignore_type_subclasses
@@ -503,3 +503,4 @@ Thank you!
503503
- Brian Maissy (brianmaissy) for fixing classes which inherit from classes with slots didn't have all of their slots compared
504504
- Juan Soler (Soleronline) for adding ignore_type_number
505505
- mthaddon for adding timedelta diffing support
506+
- Necrophagos for Hashing of the number 1 vs. True

deepdiff/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""This module offers the DeepDiff, DeepSearch, grep and DeepHash classes."""
22
# flake8: noqa
3-
__version__ = '4.0.6'
3+
__version__ = '4.0.7'
44
import logging
55

66
if __name__ == '__main__':

deepdiff/deephash.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from collections.abc import Iterable, MutableMapping
55
from collections import defaultdict
66
from hashlib import sha1, sha256
7-
7+
from enum import Enum
88
from deepdiff.helper import (strings, numbers, unprocessed, not_hashed, add_to_frozen_set,
99
convert_item_or_items_into_set_else_none, get_doc,
1010
convert_item_or_items_into_compiled_regexes_else_none,
@@ -27,6 +27,11 @@
2727
INDEX_VS_ATTRIBUTE = ('[%s]', '.%s')
2828

2929

30+
class BoolObj(Enum):
31+
TRUE = 1
32+
FALSE = 0
33+
34+
3035
def prepare_string_for_hashing(obj, ignore_string_type_changes=False, ignore_string_case=False):
3136
"""
3237
Clean type conversions
@@ -259,6 +264,9 @@ def _prep_iterable(self, obj, parent, parents_ids=EMPTY_FROZENSET):
259264

260265
return result
261266

267+
def _prep_bool(self, obj):
268+
return BoolObj.TRUE if obj else BoolObj.FALSE
269+
262270
def _prep_number(self, obj):
263271
type_ = "number" if self.ignore_numeric_type_changes else obj.__class__.__name__
264272
if self.significant_digits is not None:
@@ -282,15 +290,19 @@ def _prep_tuple(self, obj, parent, parents_ids):
282290
def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET):
283291
"""The main diff method"""
284292

293+
if isinstance(obj, bool):
294+
obj = self._prep_bool(obj)
295+
result = None
296+
else:
297+
result = not_hashed
298+
285299
try:
286300
result = self[obj]
287301
except (TypeError, KeyError):
288302
pass
289303
else:
290304
return result
291305

292-
result = not_hashed
293-
294306
if self._skip_this(obj, parent):
295307
return
296308

@@ -314,6 +326,8 @@ def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET):
314326
elif isinstance(obj, Iterable):
315327
result = self._prep_iterable(obj=obj, parent=parent, parents_ids=parents_ids)
316328

329+
elif obj in {BoolObj.TRUE, BoolObj.FALSE}:
330+
result = 'bool:true' if obj is BoolObj.TRUE else 'bool:false'
317331
else:
318332
result = self._prep_obj(obj=obj, parent=parent, parents_ids=parents_ids)
319333

docs/conf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@
6060
# built documents.
6161
#
6262
# The short X.Y version.
63-
version = '4.0.6'
63+
version = '4.0.7'
6464
# The full version, including alpha/beta/rc tags.
65-
release = '4.0.6'
65+
release = '4.0.7'
6666

6767
# The language for content autogenerated by Sphinx. Refer to documentation
6868
# for a list of supported languages.

docs/index.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
contain the root `toctree` directive.
55
66
7-
DeepDiff 4.0.6 documentation!
7+
DeepDiff 4.0.7 documentation!
88
=============================
99

1010
**DeepDiff: Deep Difference of dictionaries, iterables, strings and other objects. It will recursively look for all the changes.**
@@ -281,6 +281,7 @@ Indices and tables
281281
Changelog
282282
=========
283283

284+
- v4-0-7: Hashing of the number 1 vs. True
284285
- v4-0-6: found a tiny bug in Python formatting of numbers in scientific notation. Added a workaround.
285286
- v4-0-5: Fixing number diffing. Adding number_format_notation and number_to_string_func.
286287
- v4-0-4: Adding ignore_string_case and ignore_type_subclasses

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 4.0.6
2+
current_version = 4.0.7
33
commit = True
44
tag = True
55
tag_name = {new_version}

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
if os.environ.get('USER', '') == 'vagrant':
1111
del os.link
1212

13-
version = '4.0.6'
13+
version = '4.0.7'
1414

1515

1616
def get_reqs(filename):

tests/test_diff_text.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
import math
43
import datetime
54
import pytest
65
import logging
@@ -1702,3 +1701,25 @@ def test_diff_when_hash_fails(self, mock_DeepHash, mock_logger):
17021701
t2 = {"blah": {4}, 2: 1337}
17031702
DeepDiff(t1, t2, ignore_order=True)
17041703
assert mock_logger.warning.called
1704+
1705+
def test_bool_vs_number(self):
1706+
t1 = {
1707+
"A List": [
1708+
{
1709+
"Value One": True,
1710+
"Value Two": 1
1711+
}
1712+
],
1713+
}
1714+
1715+
t2 = {
1716+
"A List": [
1717+
{
1718+
"Value Two": 1,
1719+
"Value One": True
1720+
}
1721+
],
1722+
}
1723+
1724+
ddiff = DeepDiff(t1, t2, ignore_order=True)
1725+
assert ddiff == {}

tests/test_hash.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import re
44
import pytest
55
import logging
6-
import math
76
from deepdiff import DeepHash
8-
from deepdiff.deephash import prepare_string_for_hashing, unprocessed
7+
from deepdiff.deephash import prepare_string_for_hashing, unprocessed, BoolObj
98
from deepdiff.helper import pypy3, get_id, number_to_string
109
from collections import namedtuple
1110
from functools import partial
@@ -97,6 +96,21 @@ def test_sha1_hash_not_sensitive_to_bytecode_vs_unicode(self):
9796
class TestDeepHashPrep:
9897
"""DeepHashPrep Tests covering object serialization."""
9998

99+
def test_prep_bool_vs_num1(self):
100+
assert {BoolObj.TRUE: 'bool:true'} == DeepHashPrep(True)
101+
assert {1: 'int:1'} == DeepHashPrep(1)
102+
103+
def test_prep_bool_vs_num2(self):
104+
item1 = {
105+
"Value One": True,
106+
"Value Two": 1,
107+
}
108+
item2 = {
109+
"Value Two": 1,
110+
"Value One": True,
111+
}
112+
assert DeepHashPrep(item1)[item1] == DeepHashPrep(item2)[item2]
113+
100114
def test_prep_str(self):
101115
obj = "a"
102116
expected_result = {obj: prep_str(obj)}

0 commit comments

Comments
 (0)