Skip to content

Commit 89ab170

Browse files
committed
fixes #445 because of trying to round the date
1 parent 6abf249 commit 89ab170

10 files changed

+78
-12
lines changed

CITATION.cff

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cff-version: 1.2.0
2+
message: "If you use this software, please cite it as below."
3+
authors:
4+
- family-names: "Dehpour"
5+
given-names: "Sep"
6+
orcid: "https://orcid.org/0009-0009-5828-4345"
7+
title: "DeepDiff"
8+
version: 6.7.1
9+
date-released: 2024
10+
url: "https://github.com/seperman/deepdiff"

README.md

-10
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,6 @@ Or to see a more user friendly version, please run: `pytest --cov=deepdiff --cov
101101

102102
Thank you!
103103

104-
# Citing
105-
106-
How to cite this library (APA style):
107-
108-
Dehpour, S. (2023). DeepDiff (Version 6.7.1) [Software]. Available from https://github.com/seperman/deepdiff.
109-
110-
How to cite this library (Chicago style):
111-
112-
Dehpour, Sep. 2023. DeepDiff (version 6.7.1).
113-
114104
# Authors
115105

116106
Please take a look at the [AUTHORS](AUTHORS.md) file.

deepdiff/deephash.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
import inspect
33
import logging
4+
import datetime
45
from collections.abc import Iterable, MutableMapping
56
from collections import defaultdict
67
from hashlib import sha1, sha256
@@ -455,6 +456,10 @@ def _prep_datetime(self, obj):
455456
obj = datetime_normalize(self.truncate_datetime, obj)
456457
return KEY_TO_VAL_STR.format(type_, obj)
457458

459+
def _prep_date(self, obj):
460+
type_ = 'datetime' # yes still datetime but it doesn't need normalization
461+
return KEY_TO_VAL_STR.format(type_, obj)
462+
458463
def _prep_tuple(self, obj, parent, parents_ids):
459464
# Checking to see if it has _fields. Which probably means it is a named
460465
# tuple.
@@ -505,6 +510,9 @@ def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET):
505510
elif isinstance(obj, times):
506511
result = self._prep_datetime(obj)
507512

513+
elif isinstance(obj, datetime.date):
514+
result = self._prep_date(obj)
515+
508516
elif isinstance(obj, numbers):
509517
result = self._prep_number(obj)
510518

deepdiff/diff.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,7 @@ def _diff_numbers(self, level, local_tree=None, report_type_change=True):
13601360
self._report_result('values_changed', level, local_tree=local_tree)
13611361
else:
13621362
# Bernhard10: I use string formatting for comparison, to be consistent with usecases where
1363-
# data is read from files that were previousely written from python and
1363+
# data is read from files that were previously written from python and
13641364
# to be consistent with on-screen representation of numbers.
13651365
# Other options would be abs(t1-t2)<10**-self.significant_digits
13661366
# or math.is_close (python3.5+)

deepdiff/helper.py

+1
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ def number_to_string(number, significant_digits, number_format_notation="f"):
418418
)
419419
)
420420
else:
421+
# import pytest; pytest.set_trace()
421422
number = round(number=number, ndigits=significant_digits)
422423

423424
if significant_digits == 0:

deepdiff/serialization.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,12 @@ def _serialize_decimal(value):
537537
return float(value)
538538

539539

540+
def _serialize_tuple(value):
541+
if hasattr(value, '_asdict'): # namedtuple
542+
return value._asdict()
543+
return value
544+
545+
540546
JSON_CONVERTOR = {
541547
decimal.Decimal: _serialize_decimal,
542548
ordered_set.OrderedSet: list,
@@ -548,7 +554,8 @@ def _serialize_decimal(value):
548554
np_float32: float,
549555
np_float64: float,
550556
np_int32: int,
551-
np_int64: int
557+
np_int64: int,
558+
tuple: _serialize_tuple,
552559
}
553560

554561
if PydanticBaseModel:

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ exclude = ./data,./src,.svn,CVS,.bzr,.hg,.git,__pycache__
1515

1616
[bumpversion:file:README.md]
1717

18+
[bumpversion:file:CITATION.cff]
19+
1820
[bumpversion:file:docs/index.rst]
1921

2022
[bumpversion:file:docs/conf.py]

tests/test_hash.py

+21
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@ def test_datetime(self):
8686
b_hash = DeepHash(b)
8787
assert a_hash[a] == b_hash[b]
8888

89+
def test_date1(self):
90+
date = datetime.date(2024, 2, 1)
91+
date_hash = DeepHash(date)
92+
assert 'd90e95901f85ca09b2536d3cb81a49747c3a4fb14906d6fa0d492713ebb4309c' == date_hash[date]
93+
94+
def test_date2(self):
95+
item = {'due_date': datetime.date(2024, 2, 1)}
96+
97+
result = DeepHash(
98+
item,
99+
significant_digits=12,
100+
number_format_notation='f',
101+
ignore_numeric_type_changes=True,
102+
ignore_type_in_groups=[{int, float, complex, datetime.datetime, datetime.date, datetime.timedelta, datetime.time}],
103+
ignore_type_subclasses=False,
104+
ignore_encoding_errors=False,
105+
ignore_repetition=True,
106+
number_to_string_func=number_to_string,
107+
)
108+
assert 'e0d7ec984a0eda44ceb1e3c595f9b805530d715c779483e63a72c67cbce68615' == result[item]
109+
89110
def test_datetime_truncate(self):
90111
a = datetime.datetime(2020, 5, 17, 22, 15, 34, 913070)
91112
b = datetime.datetime(2020, 5, 17, 22, 15, 39, 296583)

tests/test_ignore_order.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
import re
3+
import datetime
34
from unittest import mock
45
from deepdiff.helper import number_to_string, CannotCompare
56
from deepdiff import DeepDiff
@@ -976,6 +977,16 @@ def test_math_epsilon_when_ignore_order_in_nested_list(self):
976977
expected = {'values_changed': {'root[0]': {'new_value': {'x': 0.0011}, 'old_value': {'x': 0.001}}, 'root[1]': {'new_value': {'y': 2}, 'old_value': {'y': 2.00002}}}}
977978
assert expected == diff
978979

980+
def test_datetime_and_ignore_order(self):
981+
diff = DeepDiff(
982+
[{'due_date': datetime.date(2024, 2, 1)}],
983+
[{'due_date': datetime.date(2024, 2, 2)}],
984+
ignore_order=True,
985+
ignore_numeric_type_changes=True
986+
)
987+
assert {} != diff
988+
989+
979990

980991
class TestCompareFuncIgnoreOrder:
981992

tests/test_serialization.py

+16
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import sys
55
import pytest
66
import datetime
7+
from typing import NamedTuple, Optional
78
from pickle import UnpicklingError
89
from decimal import Decimal
10+
from collections import Counter
911
from deepdiff import DeepDiff
1012
from deepdiff.helper import pypy3
1113
from deepdiff.serialization import (
@@ -23,6 +25,19 @@
2325
t2 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": "world\n\n\nEnd"}}
2426

2527

28+
class SomeStats(NamedTuple):
29+
counter: Optional[Counter]
30+
context_aware_counter: Optional[Counter] = None
31+
min_int: Optional[int] = 0
32+
max_int: Optional[int] = 0
33+
34+
35+
field_stats1 = SomeStats(
36+
counter=Counter(["a", "a", "b"]),
37+
max_int=10
38+
)
39+
40+
2641
class TestSerialization:
2742
"""Tests for Serializations."""
2843

@@ -323,6 +338,7 @@ def test_pretty_form_method(self, expected, verbose_level):
323338
(5, {1, 2, 10}, set),
324339
(6, datetime.datetime(2023, 10, 11), datetime.datetime.fromisoformat),
325340
(7, datetime.datetime.utcnow(), datetime.datetime.fromisoformat),
341+
(8, field_stats1, lambda x: SomeStats(**x)),
326342
])
327343
def test_json_dumps_and_loads(self, test_num, value, func_to_convert_back):
328344
serialized = json_dumps(value)

0 commit comments

Comments
 (0)