Skip to content

Commit 15d7cb9

Browse files
committed
Move warnings to user context
Currently the warning location points inside the jsonschema code and it takes an additional effort to find where the actual code that triggers it is located.
1 parent 405f735 commit 15d7cb9

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

jsonschema/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def __getattr__(name):
4646
"removed in a future release. Use importlib.metadata directly "
4747
"to query for jsonschema's version.",
4848
DeprecationWarning,
49+
stacklevel=2,
4950
)
5051

5152
try:

jsonschema/tests/test_deprecations.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def test_version(self):
1212
with self.assertWarns(DeprecationWarning) as w:
1313
from jsonschema import __version__ # noqa
1414

15+
self.assertEqual(w.filename, __file__)
1516
self.assertTrue(
1617
str(w.warning).startswith(
1718
"Accessing jsonschema.__version__ is deprecated",
@@ -27,6 +28,7 @@ def test_validators_ErrorTree(self):
2728
with self.assertWarns(DeprecationWarning) as w:
2829
from jsonschema.validators import ErrorTree # noqa
2930

31+
self.assertEqual(w.filename, __file__)
3032
self.assertTrue(
3133
str(w.warning).startswith(
3234
"Importing ErrorTree from jsonschema.validators is deprecated",
@@ -43,6 +45,7 @@ def test_validators_validators(self):
4345
value = validators.validators
4446
self.assertEqual(value, validators._VALIDATORS)
4547

48+
self.assertEqual(w.filename, __file__)
4649
self.assertTrue(
4750
str(w.warning).startswith(
4851
"Accessing jsonschema.validators.validators is deprecated",
@@ -59,6 +62,7 @@ def test_validators_meta_schemas(self):
5962
value = validators.meta_schemas
6063
self.assertEqual(value, validators._META_SCHEMAS)
6164

65+
self.assertEqual(w.filename, __file__)
6266
self.assertTrue(
6367
str(w.warning).startswith(
6468
"Accessing jsonschema.validators.meta_schemas is deprecated",
@@ -75,6 +79,7 @@ def test_RefResolver_in_scope(self):
7579
with resolver.in_scope("foo"):
7680
pass
7781

82+
self.assertEqual(w.filename, __file__)
7883
self.assertTrue(
7984
str(w.warning).startswith(
8085
"jsonschema.RefResolver.in_scope is deprecated ",
@@ -92,6 +97,7 @@ def test_Validator_is_valid_two_arguments(self):
9297
result = validator.is_valid("foo", {"type": "number"})
9398

9499
self.assertFalse(result)
100+
self.assertEqual(w.filename, __file__)
95101
self.assertTrue(
96102
str(w.warning).startswith(
97103
"Passing a schema to Validator.is_valid is deprecated ",
@@ -109,6 +115,7 @@ def test_Validator_iter_errors_two_arguments(self):
109115
error, = validator.iter_errors("foo", {"type": "number"})
110116

111117
self.assertEqual(error.validator, "type")
118+
self.assertEqual(w.filename, __file__)
112119
self.assertTrue(
113120
str(w.warning).startswith(
114121
"Passing a schema to Validator.iter_errors is deprecated ",

jsonschema/validators.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def __getattr__(name):
3333
"Importing ErrorTree from jsonschema.validators is deprecated. "
3434
"Instead import it from jsonschema.exceptions.",
3535
DeprecationWarning,
36+
stacklevel=2,
3637
)
3738
from jsonschema.exceptions import ErrorTree
3839
return ErrorTree
@@ -41,13 +42,15 @@ def __getattr__(name):
4142
"Accessing jsonschema.validators.validators is deprecated. "
4243
"Use jsonschema.validators.validator_for with a given schema.",
4344
DeprecationWarning,
45+
stacklevel=2,
4446
)
4547
return _VALIDATORS
4648
elif name == "meta_schemas":
4749
warnings.warn(
4850
"Accessing jsonschema.validators.meta_schemas is deprecated. "
4951
"Use jsonschema.validators.validator_for with a given schema.",
5052
DeprecationWarning,
53+
stacklevel=2,
5154
)
5255
return _META_SCHEMAS
5356
raise AttributeError(f"module {__name__} has no attribute {name}")
@@ -193,6 +196,7 @@ def iter_errors(self, instance, _schema=None):
193196
"iter_errors(...) instead."
194197
),
195198
DeprecationWarning,
199+
stacklevel=2,
196200
)
197201
else:
198202
_schema = self.schema
@@ -262,6 +266,7 @@ def is_valid(self, instance, _schema=None):
262266
"instead."
263267
),
264268
DeprecationWarning,
269+
stacklevel=2,
265270
)
266271
self = self.evolve(schema=_schema)
267272

@@ -727,6 +732,7 @@ def in_scope(self, scope):
727732
"jsonschema.RefResolver.in_scope is deprecated and will be "
728733
"removed in a future release.",
729734
DeprecationWarning,
735+
stacklevel=3,
730736
)
731737
self.push_scope(scope)
732738
try:

0 commit comments

Comments
 (0)