Skip to content

Commit

Permalink
Move warnings to user context
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kojoley committed Jan 1, 2022
1 parent 405f735 commit 15d7cb9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions jsonschema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __getattr__(name):
"removed in a future release. Use importlib.metadata directly "
"to query for jsonschema's version.",
DeprecationWarning,
stacklevel=2,
)

try:
Expand Down
7 changes: 7 additions & 0 deletions jsonschema/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def test_version(self):
with self.assertWarns(DeprecationWarning) as w:
from jsonschema import __version__ # noqa

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

self.assertEqual(w.filename, __file__)
self.assertTrue(
str(w.warning).startswith(
"Importing ErrorTree from jsonschema.validators is deprecated",
Expand All @@ -43,6 +45,7 @@ def test_validators_validators(self):
value = validators.validators
self.assertEqual(value, validators._VALIDATORS)

self.assertEqual(w.filename, __file__)
self.assertTrue(
str(w.warning).startswith(
"Accessing jsonschema.validators.validators is deprecated",
Expand All @@ -59,6 +62,7 @@ def test_validators_meta_schemas(self):
value = validators.meta_schemas
self.assertEqual(value, validators._META_SCHEMAS)

self.assertEqual(w.filename, __file__)
self.assertTrue(
str(w.warning).startswith(
"Accessing jsonschema.validators.meta_schemas is deprecated",
Expand All @@ -75,6 +79,7 @@ def test_RefResolver_in_scope(self):
with resolver.in_scope("foo"):
pass

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

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

self.assertEqual(error.validator, "type")
self.assertEqual(w.filename, __file__)
self.assertTrue(
str(w.warning).startswith(
"Passing a schema to Validator.iter_errors is deprecated ",
Expand Down
6 changes: 6 additions & 0 deletions jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __getattr__(name):
"Importing ErrorTree from jsonschema.validators is deprecated. "
"Instead import it from jsonschema.exceptions.",
DeprecationWarning,
stacklevel=2,
)
from jsonschema.exceptions import ErrorTree
return ErrorTree
Expand All @@ -41,13 +42,15 @@ def __getattr__(name):
"Accessing jsonschema.validators.validators is deprecated. "
"Use jsonschema.validators.validator_for with a given schema.",
DeprecationWarning,
stacklevel=2,
)
return _VALIDATORS
elif name == "meta_schemas":
warnings.warn(
"Accessing jsonschema.validators.meta_schemas is deprecated. "
"Use jsonschema.validators.validator_for with a given schema.",
DeprecationWarning,
stacklevel=2,
)
return _META_SCHEMAS
raise AttributeError(f"module {__name__} has no attribute {name}")
Expand Down Expand Up @@ -193,6 +196,7 @@ def iter_errors(self, instance, _schema=None):
"iter_errors(...) instead."
),
DeprecationWarning,
stacklevel=2,
)
else:
_schema = self.schema
Expand Down Expand Up @@ -262,6 +266,7 @@ def is_valid(self, instance, _schema=None):
"instead."
),
DeprecationWarning,
stacklevel=2,
)
self = self.evolve(schema=_schema)

Expand Down Expand Up @@ -727,6 +732,7 @@ def in_scope(self, scope):
"jsonschema.RefResolver.in_scope is deprecated and will be "
"removed in a future release.",
DeprecationWarning,
stacklevel=3,
)
self.push_scope(scope)
try:
Expand Down

0 comments on commit 15d7cb9

Please sign in to comment.