Skip to content

Commit 5a7ded3

Browse files
committed
adding support to ip addresses
1 parent 50617f9 commit 5a7ded3

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

deepdiff/helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def get_semvar_as_integer(version):
188188
only_complex_number = (complex,) + numpy_complex_numbers
189189
only_numbers = (int, float, complex, Decimal) + numpy_numbers
190190
datetimes = (datetime.datetime, datetime.date, datetime.timedelta, datetime.time, np_datetime64)
191-
ipranges = (ipaddress.IPv4Interface, ipaddress.IPv6Interface, ipaddress.IPv4Network, ipaddress.IPv6Network)
191+
ipranges = (ipaddress.IPv4Interface, ipaddress.IPv6Interface, ipaddress.IPv4Network, ipaddress.IPv6Network, ipaddress.IPv4Address, ipaddress.IPv6Address)
192192
uuids = (uuid.UUID, )
193193
times = (datetime.datetime, datetime.time,np_datetime64)
194194
numbers: Tuple = only_numbers + datetimes

deepdiff/serialization.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
pydantic_base_model_type,
3333
PydanticBaseModel,
3434
NotPresent,
35+
ipranges,
3536
)
3637
from deepdiff.model import DeltaResult
3738

@@ -114,6 +115,7 @@ class UnsupportedFormatErr(TypeError):
114115
'OrderedDict': collections.OrderedDict,
115116
'Pattern': re.Pattern,
116117
'iprange': str,
118+
'IPv4Address': str,
117119
}
118120

119121

@@ -612,6 +614,7 @@ def _serialize_tuple(value):
612614
tuple: _serialize_tuple,
613615
Mapping: dict,
614616
NotPresent: str,
617+
ipranges: str,
615618
}
616619

617620
if PydanticBaseModel is not pydantic_base_model_type:

tests/test_diff_other.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
PURGE_LEVEL_RANGE_MSG)
1313
from concurrent.futures.process import ProcessPoolExecutor
1414
from concurrent.futures import as_completed
15+
from ipaddress import IPv4Address
1516

1617
# Only the prep part of DeepHash. We don't need to test the actual hash function.
1718
DeepHashPrep = partial(DeepHash, apply_hash=False)
@@ -200,3 +201,9 @@ def test_multi_processing3_deephash(self):
200201
for future in as_completed(futures, timeout=10):
201202
assert not future._exception
202203
assert expected_result == future._result
204+
205+
def test_ip_address_diff(self):
206+
ip1 = IPv4Address("128.0.0.1")
207+
ip2 = IPv4Address("128.0.0.2")
208+
diff = DeepDiff(ip1, ip2)
209+
assert {'values_changed': {'root': {'new_value': IPv4Address('128.0.0.2'), 'old_value': IPv4Address('128.0.0.1')}}} == diff

tests/test_serialization.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
from ipaddress import IPv4Address
23
import os
34
import json
45
import sys
@@ -9,6 +10,7 @@
910
from pickle import UnpicklingError
1011
from decimal import Decimal
1112
from collections import Counter
13+
from pydantic import BaseModel, IPvAnyAddress
1214
from deepdiff import DeepDiff
1315
from deepdiff.helper import pypy3, py_current_version, np_ndarray, Opcode, SetOrdered
1416
from deepdiff.serialization import (
@@ -25,6 +27,11 @@
2527
t2 = {1: 1, 2: 2, 3: 3, 4: {"a": "hello", "b": "world\n\n\nEnd"}}
2628

2729

30+
class SampleSchema(BaseModel):
31+
works: bool = False
32+
ips: list[IPvAnyAddress]
33+
34+
2835
class SomeStats(NamedTuple):
2936
counter: Optional[Counter]
3037
context_aware_counter: Optional[Counter] = None
@@ -115,6 +122,16 @@ def test_to_dict_at_different_verbose_level(self, verbose_level, expected):
115122
ddiff = DeepDiff(t1, t2, verbose_level=verbose_level)
116123
assert expected == ddiff.to_dict()
117124

125+
def test_serialize_pydantic_model(self):
126+
obj = SampleSchema(
127+
works=True,
128+
ips=["128.0.0.1"]
129+
)
130+
serialized = json_dumps(obj)
131+
obj_again = json_loads(serialized)
132+
assert {'works': True, 'ips': ['128.0.0.1']} == obj_again
133+
assert {'works': True, 'ips': [IPv4Address('128.0.0.1')]} == obj.model_dump()
134+
118135

119136
@pytest.mark.skipif(pypy3, reason='clevercsv is not supported in pypy3')
120137
class TestLoadContet:

0 commit comments

Comments
 (0)