Skip to content

Commit 4f34fe2

Browse files
committed
adding mypy.ini but there are still way too many mypy errors
1 parent 661c3b9 commit 4f34fe2

File tree

8 files changed

+69
-63
lines changed

8 files changed

+69
-63
lines changed

deepdiff/__init__.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
logging.basicConfig(format='%(asctime)s %(levelname)8s %(message)s')
88

99

10-
from .diff import DeepDiff
11-
from .search import DeepSearch, grep
12-
from .deephash import DeepHash
13-
from .delta import Delta
14-
from .path import extract, parse_path
10+
from .diff import DeepDiff as DeepDiff
11+
from .search import DeepSearch as DeepSearch, grep as grep
12+
from .deephash import DeepHash as DeepHash
13+
from .delta import Delta as Delta
14+
from .path import extract as extract, parse_path as parse_path

deepdiff/deephash.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def prepare_string_for_hashing(
107107
break
108108
except UnicodeDecodeError as er:
109109
err = er
110-
if not encoded:
111-
obj_decoded = obj.decode('utf-8', errors='ignore')
110+
if not encoded and err is not None:
111+
obj_decoded = obj.decode('utf-8', errors='ignore') # type: ignore
112112
start = max(err.start - 20, 0)
113113
start_prefix = ''
114114
if start > 0:
@@ -379,7 +379,7 @@ def _skip_this(self, obj, parent):
379379
skip = False
380380
break
381381
elif self.exclude_regex_paths and any(
382-
[exclude_regex_path.search(parent) for exclude_regex_path in self.exclude_regex_paths]):
382+
[exclude_regex_path.search(parent) for exclude_regex_path in self.exclude_regex_paths]): # type: ignore
383383
skip = True
384384
elif self.exclude_types_tuple and isinstance(obj, self.exclude_types_tuple):
385385
skip = True
@@ -540,7 +540,7 @@ def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET):
540540
elif isinstance(obj, datetime.date):
541541
result = self._prep_date(obj)
542542

543-
elif isinstance(obj, numbers):
543+
elif isinstance(obj, numbers): # type: ignore
544544
result = self._prep_number(obj)
545545

546546
elif isinstance(obj, MutableMapping):
@@ -549,17 +549,17 @@ def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET):
549549
elif isinstance(obj, tuple):
550550
result, counts = self._prep_tuple(obj=obj, parent=parent, parents_ids=parents_ids)
551551

552-
elif (pandas and isinstance(obj, pandas.DataFrame)):
553-
def gen():
554-
yield ('dtype', obj.dtypes)
555-
yield ('index', obj.index)
556-
yield from obj.items() # which contains (column name, series tuples)
552+
elif (pandas and isinstance(obj, pandas.DataFrame)): # type: ignore
553+
def gen(): # type: ignore
554+
yield ('dtype', obj.dtypes) # type: ignore
555+
yield ('index', obj.index) # type: ignore
556+
yield from obj.items() # type: ignore # which contains (column name, series tuples)
557557
result, counts = self._prep_iterable(obj=gen(), parent=parent, parents_ids=parents_ids)
558-
elif (polars and isinstance(obj, polars.DataFrame)):
558+
elif (polars and isinstance(obj, polars.DataFrame)): # type: ignore
559559
def gen():
560-
yield from obj.columns
561-
yield from list(obj.schema.items())
562-
yield from obj.rows()
560+
yield from obj.columns # type: ignore
561+
yield from list(obj.schema.items()) # type: ignore
562+
yield from obj.rows() # type: ignore
563563
result, counts = self._prep_iterable(obj=gen(), parent=parent, parents_ids=parents_ids)
564564

565565
elif isinstance(obj, Iterable):

deepdiff/delta.py

+23-23
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def reset(self):
171171
self.post_process_paths_to_convert = dict_()
172172

173173
def __add__(self, other):
174-
if isinstance(other, numbers) and self._numpy_paths:
174+
if isinstance(other, numbers) and self._numpy_paths: # type: ignore
175175
raise DeltaNumpyOperatorOverrideError(DELTA_NUMPY_OPERATOR_OVERRIDE_MSG)
176176
if self.mutate:
177177
self.root = other
@@ -240,7 +240,7 @@ def _get_elem_and_compare_to_old_value(
240240
if action == GET:
241241
current_old_value = obj[elem]
242242
elif action == GETATTR:
243-
current_old_value = getattr(obj, elem)
243+
current_old_value = getattr(obj, elem) # type: ignore
244244
else:
245245
raise DeltaError(INVALID_ACTION_WHEN_CALLING_GET_ELEM.format(action))
246246
except (KeyError, IndexError, AttributeError, TypeError) as e:
@@ -261,7 +261,7 @@ def _get_elem_and_compare_to_old_value(
261261
else:
262262
obj[elem] = _forced_old_value
263263
elif action == GETATTR:
264-
setattr(obj, elem, _forced_old_value)
264+
setattr(obj, elem, _forced_old_value) # type: ignore
265265
return _forced_old_value
266266
current_old_value = not_found
267267
if isinstance(path_for_err_reporting, (list, tuple)):
@@ -289,7 +289,7 @@ def _simple_set_elem_value(self, obj, path_for_err_reporting, elem=None, value=N
289289
else:
290290
self._raise_or_log(ELEM_NOT_FOUND_TO_ADD_MSG.format(elem, path_for_err_reporting))
291291
elif action == GETATTR:
292-
setattr(obj, elem, value)
292+
setattr(obj, elem, value) # type: ignore
293293
else:
294294
raise DeltaError(INVALID_ACTION_WHEN_CALLING_SIMPLE_SET_ELEM.format(action))
295295
except (KeyError, IndexError, AttributeError, TypeError) as e:
@@ -457,8 +457,8 @@ def _do_item_added(self, items, sort=True, insert=False):
457457
continue # pragma: no cover. Due to cPython peephole optimizer, this line doesn't get covered. https://github.com/nedbat/coveragepy/issues/198
458458

459459
# Insert is only true for iterables, make sure it is a valid index.
460-
if(insert and elem < len(obj)):
461-
obj.insert(elem, None)
460+
if(insert and elem < len(obj)): # type: ignore
461+
obj.insert(elem, None) # type: ignore
462462

463463
self._set_new_value(parent, parent_to_obj_elem, parent_to_obj_action,
464464
obj, elements, path, elem, action, new_value)
@@ -482,7 +482,7 @@ def _do_post_process(self):
482482
def _do_pre_process(self):
483483
if self._numpy_paths and ('iterable_item_added' in self.diff or 'iterable_item_removed' in self.diff):
484484
preprocess_paths = dict_()
485-
for path, type_ in self._numpy_paths.items():
485+
for path, type_ in self._numpy_paths.items(): # type: ignore
486486
preprocess_paths[path] = {'old_type': np_ndarray, 'new_type': list}
487487
try:
488488
type_ = numpy_dtype_string_to_type(type_)
@@ -507,7 +507,7 @@ def _get_elements_and_details(self, path):
507507
parent_to_obj_elem, parent_to_obj_action = elements[-2]
508508
obj = self._get_elem_and_compare_to_old_value(
509509
obj=parent, path_for_err_reporting=path, expected_old_value=None,
510-
elem=parent_to_obj_elem, action=parent_to_obj_action, next_element=next2_element)
510+
elem=parent_to_obj_elem, action=parent_to_obj_action, next_element=next2_element) # type: ignore
511511
else:
512512
# parent = self
513513
# obj = self.root
@@ -516,7 +516,7 @@ def _get_elements_and_details(self, path):
516516
parent = parent_to_obj_elem = parent_to_obj_action = None
517517
obj = self
518518
# obj = self.get_nested_obj(obj=self, elements=elements[:-1])
519-
elem, action = elements[-1]
519+
elem, action = elements[-1] # type: ignore
520520
except Exception as e:
521521
self._raise_or_log(UNABLE_TO_GET_ITEM_MSG.format(path, e))
522522
return None
@@ -550,7 +550,7 @@ def _do_values_or_type_changed(self, changes, is_type_change=False, verify_chang
550550
else:
551551
new_value = new_type(current_old_value)
552552
except Exception as e:
553-
self._raise_or_log(TYPE_CHANGE_FAIL_MSG.format(obj[elem], value.get('new_type', 'unknown'), e))
553+
self._raise_or_log(TYPE_CHANGE_FAIL_MSG.format(obj[elem], value.get('new_type', 'unknown'), e)) # type: ignore
554554
continue
555555
else:
556556
new_value = value['new_value']
@@ -582,7 +582,7 @@ def _do_item_removed(self, items):
582582
current_old_value = not_found
583583
try:
584584
if action == GET:
585-
current_old_value = obj[elem]
585+
current_old_value = obj[elem] # type: ignore
586586
elif action == GETATTR:
587587
current_old_value = getattr(obj, elem)
588588
look_for_expected_old_value = current_old_value != expected_old_value
@@ -644,15 +644,15 @@ def _do_iterable_opcodes(self):
644644
transformed.extend(opcode.new_values)
645645
elif opcode.tag == 'equal':
646646
# Items are the same in both lists, so we add them to the result
647-
transformed.extend(obj[opcode.t1_from_index:opcode.t1_to_index])
647+
transformed.extend(obj[opcode.t1_from_index:opcode.t1_to_index]) # type: ignore
648648
if is_obj_tuple:
649-
obj = tuple(obj)
649+
obj = tuple(obj) # type: ignore
650650
# Making sure that the object is re-instated inside the parent especially if it was immutable
651651
# and we had to turn it into a mutable one. In such cases the object has a new id.
652652
self._simple_set_elem_value(obj=parent, path_for_err_reporting=path, elem=parent_to_obj_elem,
653653
value=obj, action=parent_to_obj_action)
654654
else:
655-
obj[:] = transformed
655+
obj[:] = transformed # type: ignore
656656

657657

658658

@@ -745,7 +745,7 @@ def _do_ignore_order(self):
745745
fixed_indexes = self.diff.get('iterable_items_added_at_indexes', dict_())
746746
remove_indexes = self.diff.get('iterable_items_removed_at_indexes', dict_())
747747
paths = SetOrdered(fixed_indexes.keys()) | SetOrdered(remove_indexes.keys())
748-
for path in paths:
748+
for path in paths: # type: ignore
749749
# In the case of ignore_order reports, we are pointing to the container object.
750750
# Thus we add a [0] to the elements so we can get the required objects and discard what we don't need.
751751
elem_and_details = self._get_elements_and_details("{}[0]".format(path))
@@ -1021,7 +1021,7 @@ def _from_flat_dicts(flat_dict_list):
10211021
result['_iterable_opcodes'][path_str] = []
10221022
result['_iterable_opcodes'][path_str].append(
10231023
Opcode(
1024-
tag=FLAT_DATA_ACTION_TO_OPCODE_TAG[action],
1024+
tag=FLAT_DATA_ACTION_TO_OPCODE_TAG[action], # type: ignore
10251025
t1_from_index=flat_dict.get('t1_from_index'),
10261026
t1_to_index=flat_dict.get('t1_to_index'),
10271027
t2_from_index=flat_dict.get('t2_from_index'),
@@ -1091,7 +1091,7 @@ def to_flat_dicts(self, include_action_in_path=False, report_type_changes=True)
10911091
"""
10921092
return [
10931093
i._asdict() for i in self.to_flat_rows(include_action_in_path=False, report_type_changes=True)
1094-
]
1094+
] # type: ignore
10951095

10961096
def to_flat_rows(self, include_action_in_path=False, report_type_changes=True) -> List[FlatDeltaRow]:
10971097
"""
@@ -1141,13 +1141,13 @@ def to_flat_rows(self, include_action_in_path=False, report_type_changes=True) -
11411141
for index, value in index_to_value.items():
11421142
path2 = path.copy()
11431143
if include_action_in_path:
1144-
path2.append((index, 'GET'))
1144+
path2.append((index, 'GET')) # type: ignore
11451145
else:
11461146
path2.append(index)
11471147
if report_type_changes:
1148-
row = FlatDeltaRow(path=path2, value=value, action=new_action, type=type(value))
1148+
row = FlatDeltaRow(path=path2, value=value, action=new_action, type=type(value)) # type: ignore
11491149
else:
1150-
row = FlatDeltaRow(path=path2, value=value, action=new_action)
1150+
row = FlatDeltaRow(path=path2, value=value, action=new_action) # type: ignore
11511151
result.append(row)
11521152
elif action in {'set_item_added', 'set_item_removed'}:
11531153
for path, values in info.items():
@@ -1167,15 +1167,15 @@ def to_flat_rows(self, include_action_in_path=False, report_type_changes=True) -
11671167
value = value[new_key]
11681168
elif isinstance(value, (list, tuple)) and len(value) == 1:
11691169
value = value[0]
1170-
path.append(0)
1170+
path.append(0) # type: ignore
11711171
action = 'iterable_item_added'
11721172
elif isinstance(value, set) and len(value) == 1:
11731173
value = value.pop()
11741174
action = 'set_item_added'
11751175
if report_type_changes:
1176-
row = FlatDeltaRow(path=path, value=value, action=action, type=type(value))
1176+
row = FlatDeltaRow(path=path, value=value, action=action, type=type(value)) # type: ignore
11771177
else:
1178-
row = FlatDeltaRow(path=path, value=value, action=action)
1178+
row = FlatDeltaRow(path=path, value=value, action=action) # type: ignore
11791179
result.append(row)
11801180
elif action in {
11811181
'dictionary_item_removed', 'iterable_item_added',

deepdiff/lfucache.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ def __init__(self, key, report_type, value, freq_node, pre, nxt):
2323
self.nxt = nxt # next CacheNode
2424

2525
def free_myself(self):
26-
if self.freq_node.cache_head == self.freq_node.cache_tail:
27-
self.freq_node.cache_head = self.freq_node.cache_tail = None
28-
elif self.freq_node.cache_head == self:
29-
self.nxt.pre = None
30-
self.freq_node.cache_head = self.nxt
31-
elif self.freq_node.cache_tail == self:
32-
self.pre.nxt = None
33-
self.freq_node.cache_tail = self.pre
26+
if self.freq_node.cache_head == self.freq_node.cache_tail: # type: ignore
27+
self.freq_node.cache_head = self.freq_node.cache_tail = None # type: ignore
28+
elif self.freq_node.cache_head == self: # type: ignore
29+
self.nxt.pre = None # type: ignore
30+
self.freq_node.cache_head = self.nxt # type: ignore
31+
elif self.freq_node.cache_tail == self: # type: ignore
32+
self.pre.nxt = None # type: ignore
33+
self.freq_node.cache_tail = self.pre # type: ignore
3434
else:
35-
self.pre.nxt = self.nxt
36-
self.nxt.pre = self.pre
35+
self.pre.nxt = self.nxt # type: ignore
36+
self.nxt.pre = self.pre # type: ignore
3737

3838
self.pre = None
3939
self.nxt = None
@@ -77,8 +77,8 @@ def pop_head_cache(self):
7777
return cache_head
7878
else:
7979
cache_head = self.cache_head
80-
self.cache_head.nxt.pre = None
81-
self.cache_head = self.cache_head.nxt
80+
self.cache_head.nxt.pre = None # type: ignore
81+
self.cache_head = self.cache_head.nxt # type: ignore
8282
return cache_head
8383

8484
def append_cache_to_tail(self, cache_node):
@@ -89,7 +89,7 @@ def append_cache_to_tail(self, cache_node):
8989
else:
9090
cache_node.pre = self.cache_tail
9191
cache_node.nxt = None
92-
self.cache_tail.nxt = cache_node
92+
self.cache_tail.nxt = cache_node # type: ignore
9393
self.cache_tail = cache_node
9494

9595
def insert_after_me(self, freq_node):
@@ -172,12 +172,12 @@ def move_forward(self, cache_node, freq_node):
172172

173173
def dump_cache(self):
174174
head_freq_node = self.freq_link_head
175-
self.cache.pop(head_freq_node.cache_head.key)
176-
head_freq_node.pop_head_cache()
175+
self.cache.pop(head_freq_node.cache_head.key) # type: ignore
176+
head_freq_node.pop_head_cache() # type: ignore
177177

178-
if head_freq_node.count_caches() == 0:
179-
self.freq_link_head = head_freq_node.nxt
180-
head_freq_node.remove()
178+
if head_freq_node.count_caches() == 0: # type: ignore
179+
self.freq_link_head = head_freq_node.nxt # type: ignore
180+
head_freq_node.remove() # type: ignore
181181

182182
def create_cache_node(self, key, report_type, value):
183183
cache_node = CacheNode(

docs/faq.rst

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ Or use the tree view so you can use path(output_format='list'):
149149

150150

151151
Q: Why my datetimes are reported in UTC?
152+
----------------------------------------
152153

153154
**Answer**
154155

mypy.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[mypy]
2+
warn_unused_ignores = False

requirements-dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ pytest-benchmark==5.1.0
1919
pandas==2.2.3
2020
polars==1.21.0
2121
setuptools==75.8.0
22+
types-setuptools==75.8.0

tests/test_operators.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def _l2_distance(self, c1, c2):
3131
(c1["x"] - c2["x"]) ** 2 + (c1["y"] - c2["y"]) ** 2
3232
)
3333

34-
def give_up_diffing(self, level, diff_instance):
34+
def give_up_diffing(self, level, diff_instance) -> bool:
3535
l2_distance = self._l2_distance(level.t1, level.t2)
3636
if l2_distance > self.distance_threshold:
3737
diff_instance.custom_report_result('distance_too_far', level, {
@@ -77,7 +77,7 @@ def _l2_distance(self, c1, c2):
7777
(c1["x"] - c2["x"]) ** 2 + (c1["y"] - c2["y"]) ** 2
7878
)
7979

80-
def give_up_diffing(self, level, diff_instance):
80+
def give_up_diffing(self, level, diff_instance) -> bool:
8181
l2_distance = self._l2_distance(level.t1, level.t2)
8282
if l2_distance > self.distance_threshold:
8383
diff_instance.custom_report_result('distance_too_far', level, {
@@ -122,7 +122,7 @@ class ExpectChangeOperator(BaseOperator):
122122
def __init__(self, regex_paths):
123123
super().__init__(regex_paths)
124124

125-
def give_up_diffing(self, level, diff_instance):
125+
def give_up_diffing(self, level, diff_instance) -> bool:
126126
if level.t1 == level.t2:
127127
diff_instance.custom_report_result('unexpected:still', level, {
128128
"old": level.t1,
@@ -154,9 +154,10 @@ def __repr__(self):
154154

155155
class ListMatchOperator(BaseOperator):
156156

157-
def give_up_diffing(self, level, diff_instance):
157+
def give_up_diffing(self, level, diff_instance) -> bool:
158158
if set(level.t1.dict['list']) == set(level.t2.dict['list']):
159159
return True
160+
return False
160161

161162
ddiff = DeepDiff(custom1, custom2, custom_operators=[
162163
ListMatchOperator(types=[CustomClass])
@@ -260,6 +261,7 @@ def __init__(self, tolerance, types):
260261
def match(self, level) -> bool:
261262
if type(level.t1) in self.types:
262263
return True
264+
return False
263265

264266
def give_up_diffing(self, level, diff_instance) -> bool:
265267
relative = abs(abs(level.t1 - level.t2) / level.t1)

0 commit comments

Comments
 (0)