Skip to content

Commit c092940

Browse files
committedMar 30, 2025·
fix ignored KeyError exceptions raised in diff handlers
1 parent f351f25 commit c092940

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed
 

‎nested_diff/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,15 @@ def diff(self, a, b):
154154
if a is b:
155155
return True, {'U': a} if self.op_u else {}
156156

157+
differ = self.default_differ
158+
157159
if a.__class__ is b.__class__:
158160
try:
159-
return self._differs[a.__class__](self, a, b)
161+
differ = self._differs[a.__class__]
160162
except KeyError:
161163
pass
162164

163-
return self.default_differ(self, a, b)
165+
return differ(self, a, b)
164166

165167
def set_handler(self, handler):
166168
"""Set handler.

‎tests/test_handlers.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from nested_diff import Differ, handlers
24

35

@@ -25,6 +27,15 @@ def diff(self, differ, a, b):
2527
assert got == expected
2628

2729

30+
def test_diff_handler_exceptions_propagated():
31+
class IntHandler(handlers.IntHandler):
32+
def diff(*args): # noqa: ARG002
33+
raise KeyError
34+
35+
with pytest.raises(KeyError):
36+
Differ(handlers=[IntHandler()]).diff(1, 2)
37+
38+
2839
def test_scalar_handler_diff_equal():
2940
a = 0
3041
b = 0

0 commit comments

Comments
 (0)
Please sign in to comment.