Skip to content

Commit 0cf607d

Browse files
committed
fixes #405 where old versions of numpy are not supported
1 parent a95defa commit 0cf607d

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

deepdiff/diff.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,6 @@ def _compare_in_order(
655655
Default compare if `iterable_compare_func` is not provided.
656656
This will compare in sequence order.
657657
"""
658-
659658
if t1_from_index is None:
660659
return [((i, i), (x, y)) for i, (x, y) in enumerate(
661660
zip_longest(

deepdiff/helper.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,40 @@ class pydantic_base_model_type:
108108

109109
NUMERICS = frozenset(string.digits)
110110

111+
112+
def _int_or_zero(value):
113+
"""
114+
Tries to extract some number from a string.
115+
116+
12c becomes 12
117+
"""
118+
try:
119+
return int(value)
120+
except Exception:
121+
result = []
122+
for char in value:
123+
if char in NUMERICS:
124+
result.append(char)
125+
if result:
126+
return int(''.join(result))
127+
return 0
128+
129+
130+
def get_semvar_as_integer(version):
131+
"""
132+
Converts:
133+
134+
'1.23.5' to 1023005
135+
"""
136+
version = version.split('.')
137+
if len(version) > 3:
138+
version = version[:3]
139+
elif len(version) < 3:
140+
version.extend(['0'] * (3 - len(version)))
141+
142+
return sum([10**(i * 3) * _int_or_zero(v) for i, v in enumerate(reversed(version))])
143+
144+
111145
# we used to use OrderedDictPlus when dictionaries in Python were not ordered.
112146
dict_ = dict
113147

@@ -120,6 +154,10 @@ class pydantic_base_model_type:
120154

121155
pypy3 = py3 and hasattr(sys, "pypy_translation_info")
122156

157+
158+
if get_semvar_as_integer(np.__version__) < 1019000:
159+
sys.exit('The minimum required Numpy version is 1.19.0. Please upgrade your Numpy package.')
160+
123161
strings = (str, bytes) # which are both basestring
124162
unicode_type = str
125163
bytes_type = bytes
@@ -321,8 +359,8 @@ def type_in_type_group(item, type_group):
321359

322360
def type_is_subclass_of_type_group(item, type_group):
323361
return isinstance(item, type_group) \
324-
or (isinstance(item, type) and issubclass(item, type_group)) \
325-
or type_in_type_group(item, type_group)
362+
or (isinstance(item, type) and issubclass(item, type_group)) \
363+
or type_in_type_group(item, type_group)
326364

327365

328366
def get_doc(doc_filename):

tests/test_helper.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
not_found, OrderedSetPlus, diff_numpy_array, cartesian_product_numpy,
1111
get_truncate_datetime, datetime_normalize,
1212
detailed__dict__, ENUM_INCLUDE_KEYS, add_root_to_paths,
13+
get_semvar_as_integer,
1314
)
1415

1516

@@ -297,3 +298,14 @@ def test_detailed__dict__(self, obj, include_keys, expected):
297298
def test_add_root_to_paths(self, test_num, value, expected):
298299
result = add_root_to_paths(value)
299300
assert expected == result, f"test_add_root_to_paths #{test_num} failed."
301+
302+
@pytest.mark.parametrize('test_num, value, expected', [
303+
(1, '1.2.3', 1002003),
304+
(2, '1.22.3', 1022003),
305+
(3, '1.22.3c', 1022003),
306+
(4, '2.4', 2004000),
307+
(5, '1.19.0', 1019000),
308+
])
309+
def test_get_semvar_as_integer(self, test_num, value, expected):
310+
result = get_semvar_as_integer(value)
311+
assert expected == result, f"test_get_semvar_as_integer #{test_num} failed."

0 commit comments

Comments
 (0)