Skip to content

Commit 211c548

Browse files
committed
Suppress the KeyError in unknown type exception tracebacks.
It's an implementation detail that this is looking things up in a dict.
1 parent 9a62047 commit 211c548

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

jsonschema/_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def is_type(self, instance, type):
8989
try:
9090
fn = self._type_checkers[type]
9191
except KeyError:
92-
raise UndefinedTypeCheck(type)
92+
raise UndefinedTypeCheck(type) from None
9393

9494
return fn(self, instance)
9595

jsonschema/tests/test_types.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from jsonschema import ValidationError, _validators
1212
from jsonschema._types import TypeChecker
1313
from jsonschema.exceptions import UndefinedTypeCheck, UnknownType
14-
from jsonschema.validators import Draft4Validator, extend
14+
from jsonschema.validators import Draft202012Validator, extend
1515

1616

1717
def equals_2(checker, instance):
@@ -23,7 +23,7 @@ def is_namedtuple(instance):
2323

2424

2525
def is_object_or_named_tuple(checker, instance):
26-
if Draft4Validator.TYPE_CHECKER.is_type(instance, "object"):
26+
if Draft202012Validator.TYPE_CHECKER.is_type(instance, "object"):
2727
return True
2828
return is_namedtuple(instance)
2929

@@ -40,11 +40,15 @@ def test_is_type(self):
4040
)
4141

4242
def test_is_unknown_type(self):
43-
with self.assertRaises(UndefinedTypeCheck) as context:
43+
with self.assertRaises(UndefinedTypeCheck) as e:
4444
TypeChecker().is_type(4, "foobar")
4545
self.assertIn(
4646
"'foobar' is unknown to this type checker",
47-
str(context.exception),
47+
str(e.exception),
48+
)
49+
self.assertTrue(
50+
e.exception.__suppress_context__,
51+
msg="Expected the internal KeyError to be hidden.",
4852
)
4953

5054
def test_checks_can_be_added_at_init(self):
@@ -114,8 +118,8 @@ def int_or_str_int(checker, instance):
114118
return True
115119

116120
CustomValidator = extend(
117-
Draft4Validator,
118-
type_checker=Draft4Validator.TYPE_CHECKER.redefine(
121+
Draft202012Validator,
122+
type_checker=Draft202012Validator.TYPE_CHECKER.redefine(
119123
"integer", int_or_str_int,
120124
),
121125
)
@@ -135,23 +139,29 @@ def test_object_can_be_extended(self):
135139

136140
Point = namedtuple("Point", ["x", "y"])
137141

138-
type_checker = Draft4Validator.TYPE_CHECKER.redefine(
142+
type_checker = Draft202012Validator.TYPE_CHECKER.redefine(
139143
"object", is_object_or_named_tuple,
140144
)
141145

142-
CustomValidator = extend(Draft4Validator, type_checker=type_checker)
146+
CustomValidator = extend(
147+
Draft202012Validator,
148+
type_checker=type_checker,
149+
)
143150
validator = CustomValidator(schema)
144151

145152
validator.validate(Point(x=4, y=5))
146153

147154
def test_object_extensions_require_custom_validators(self):
148155
schema = {"type": "object", "required": ["x"]}
149156

150-
type_checker = Draft4Validator.TYPE_CHECKER.redefine(
157+
type_checker = Draft202012Validator.TYPE_CHECKER.redefine(
151158
"object", is_object_or_named_tuple,
152159
)
153160

154-
CustomValidator = extend(Draft4Validator, type_checker=type_checker)
161+
CustomValidator = extend(
162+
Draft202012Validator,
163+
type_checker=type_checker,
164+
)
155165
validator = CustomValidator(schema)
156166

157167
Point = namedtuple("Point", ["x", "y"])
@@ -166,7 +176,7 @@ def test_object_extensions_can_handle_custom_validators(self):
166176
"properties": {"x": {"type": "integer"}},
167177
}
168178

169-
type_checker = Draft4Validator.TYPE_CHECKER.redefine(
179+
type_checker = Draft202012Validator.TYPE_CHECKER.redefine(
170180
"object", is_object_or_named_tuple,
171181
)
172182

@@ -181,7 +191,7 @@ def coerced(validator, value, instance, schema):
181191
properties = coerce_named_tuple(_validators.properties)
182192

183193
CustomValidator = extend(
184-
Draft4Validator,
194+
Draft202012Validator,
185195
type_checker=type_checker,
186196
validators={"required": required, "properties": properties},
187197
)
@@ -203,5 +213,5 @@ def coerced(validator, value, instance, schema):
203213

204214
def test_unknown_type(self):
205215
with self.assertRaises(UnknownType) as e:
206-
Draft4Validator({}).is_type(12, "some unknown type")
216+
Draft202012Validator({}).is_type(12, "some unknown type")
207217
self.assertIn("'some unknown type'", str(e.exception))

0 commit comments

Comments
 (0)