test_deprecations.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. from contextlib import contextmanager
  2. from io import BytesIO
  3. from unittest import TestCase, mock
  4. import importlib.metadata
  5. import json
  6. import subprocess
  7. import sys
  8. import urllib.request
  9. import referencing.exceptions
  10. from jsonschema import FormatChecker, exceptions, protocols, validators
  11. class TestDeprecations(TestCase):
  12. def test_version(self):
  13. """
  14. As of v4.0.0, __version__ is deprecated in favor of importlib.metadata.
  15. """
  16. message = "Accessing jsonschema.__version__ is deprecated"
  17. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  18. from jsonschema import __version__
  19. self.assertEqual(__version__, importlib.metadata.version("jsonschema"))
  20. self.assertEqual(w.filename, __file__)
  21. def test_validators_ErrorTree(self):
  22. """
  23. As of v4.0.0, importing ErrorTree from jsonschema.validators is
  24. deprecated in favor of doing so from jsonschema.exceptions.
  25. """
  26. message = "Importing ErrorTree from jsonschema.validators is "
  27. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  28. from jsonschema.validators import ErrorTree
  29. self.assertEqual(ErrorTree, exceptions.ErrorTree)
  30. self.assertEqual(w.filename, __file__)
  31. def test_import_ErrorTree(self):
  32. """
  33. As of v4.18.0, importing ErrorTree from the package root is
  34. deprecated in favor of doing so from jsonschema.exceptions.
  35. """
  36. message = "Importing ErrorTree directly from the jsonschema package "
  37. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  38. from jsonschema import ErrorTree
  39. self.assertEqual(ErrorTree, exceptions.ErrorTree)
  40. self.assertEqual(w.filename, __file__)
  41. def test_ErrorTree_setitem(self):
  42. """
  43. As of v4.20.0, setting items on an ErrorTree is deprecated.
  44. """
  45. e = exceptions.ValidationError("some error", path=["foo"])
  46. tree = exceptions.ErrorTree()
  47. subtree = exceptions.ErrorTree(errors=[e])
  48. message = "ErrorTree.__setitem__ is "
  49. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  50. tree["foo"] = subtree
  51. self.assertEqual(tree["foo"], subtree)
  52. self.assertEqual(w.filename, __file__)
  53. def test_import_FormatError(self):
  54. """
  55. As of v4.18.0, importing FormatError from the package root is
  56. deprecated in favor of doing so from jsonschema.exceptions.
  57. """
  58. message = "Importing FormatError directly from the jsonschema package "
  59. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  60. from jsonschema import FormatError
  61. self.assertEqual(FormatError, exceptions.FormatError)
  62. self.assertEqual(w.filename, __file__)
  63. def test_import_Validator(self):
  64. """
  65. As of v4.19.0, importing Validator from the package root is
  66. deprecated in favor of doing so from jsonschema.protocols.
  67. """
  68. message = "Importing Validator directly from the jsonschema package "
  69. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  70. from jsonschema import Validator
  71. self.assertEqual(Validator, protocols.Validator)
  72. self.assertEqual(w.filename, __file__)
  73. def test_validators_validators(self):
  74. """
  75. As of v4.0.0, accessing jsonschema.validators.validators is
  76. deprecated.
  77. """
  78. message = "Accessing jsonschema.validators.validators is deprecated"
  79. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  80. value = validators.validators
  81. self.assertEqual(value, validators._VALIDATORS)
  82. self.assertEqual(w.filename, __file__)
  83. def test_validators_meta_schemas(self):
  84. """
  85. As of v4.0.0, accessing jsonschema.validators.meta_schemas is
  86. deprecated.
  87. """
  88. message = "Accessing jsonschema.validators.meta_schemas is deprecated"
  89. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  90. value = validators.meta_schemas
  91. self.assertEqual(value, validators._META_SCHEMAS)
  92. self.assertEqual(w.filename, __file__)
  93. def test_RefResolver_in_scope(self):
  94. """
  95. As of v4.0.0, RefResolver.in_scope is deprecated.
  96. """
  97. resolver = validators._RefResolver.from_schema({})
  98. message = "jsonschema.RefResolver.in_scope is deprecated "
  99. with self.assertWarnsRegex(DeprecationWarning, message) as w: # noqa: SIM117
  100. with resolver.in_scope("foo"):
  101. pass
  102. self.assertEqual(w.filename, __file__)
  103. def test_Validator_is_valid_two_arguments(self):
  104. """
  105. As of v4.0.0, calling is_valid with two arguments (to provide a
  106. different schema) is deprecated.
  107. """
  108. validator = validators.Draft7Validator({})
  109. message = "Passing a schema to Validator.is_valid is deprecated "
  110. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  111. result = validator.is_valid("foo", {"type": "number"})
  112. self.assertFalse(result)
  113. self.assertEqual(w.filename, __file__)
  114. def test_Validator_iter_errors_two_arguments(self):
  115. """
  116. As of v4.0.0, calling iter_errors with two arguments (to provide a
  117. different schema) is deprecated.
  118. """
  119. validator = validators.Draft7Validator({})
  120. message = "Passing a schema to Validator.iter_errors is deprecated "
  121. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  122. error, = validator.iter_errors("foo", {"type": "number"})
  123. self.assertEqual(error.validator, "type")
  124. self.assertEqual(w.filename, __file__)
  125. def test_Validator_resolver(self):
  126. """
  127. As of v4.18.0, accessing Validator.resolver is deprecated.
  128. """
  129. validator = validators.Draft7Validator({})
  130. message = "Accessing Draft7Validator.resolver is "
  131. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  132. self.assertIsInstance(validator.resolver, validators._RefResolver)
  133. self.assertEqual(w.filename, __file__)
  134. def test_RefResolver(self):
  135. """
  136. As of v4.18.0, RefResolver is fully deprecated.
  137. """
  138. message = "jsonschema.RefResolver is deprecated"
  139. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  140. from jsonschema import RefResolver
  141. self.assertEqual(w.filename, __file__)
  142. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  143. from jsonschema.validators import RefResolver # noqa: F401, F811
  144. self.assertEqual(w.filename, __file__)
  145. def test_RefResolutionError(self):
  146. """
  147. As of v4.18.0, RefResolutionError is deprecated in favor of directly
  148. catching errors from the referencing library.
  149. """
  150. message = "jsonschema.exceptions.RefResolutionError is deprecated"
  151. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  152. from jsonschema import RefResolutionError
  153. self.assertEqual(RefResolutionError, exceptions._RefResolutionError)
  154. self.assertEqual(w.filename, __file__)
  155. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  156. from jsonschema.exceptions import RefResolutionError
  157. self.assertEqual(RefResolutionError, exceptions._RefResolutionError)
  158. self.assertEqual(w.filename, __file__)
  159. def test_catching_Unresolvable_directly(self):
  160. """
  161. This behavior is the intended behavior (i.e. it's not deprecated), but
  162. given we do "tricksy" things in the iterim to wrap exceptions in a
  163. multiple inheritance subclass, we need to be extra sure it works and
  164. stays working.
  165. """
  166. validator = validators.Draft202012Validator({"$ref": "urn:nothing"})
  167. with self.assertRaises(referencing.exceptions.Unresolvable) as e:
  168. validator.validate(12)
  169. expected = referencing.exceptions.Unresolvable(ref="urn:nothing")
  170. self.assertEqual(
  171. (e.exception, str(e.exception)),
  172. (expected, "Unresolvable: urn:nothing"),
  173. )
  174. def test_catching_Unresolvable_via_RefResolutionError(self):
  175. """
  176. Until RefResolutionError is removed, it is still possible to catch
  177. exceptions from reference resolution using it, even though they may
  178. have been raised by referencing.
  179. """
  180. with self.assertWarns(DeprecationWarning):
  181. from jsonschema import RefResolutionError
  182. validator = validators.Draft202012Validator({"$ref": "urn:nothing"})
  183. with self.assertRaises(referencing.exceptions.Unresolvable) as u:
  184. validator.validate(12)
  185. with self.assertRaises(RefResolutionError) as e:
  186. validator.validate(12)
  187. self.assertEqual(
  188. (e.exception, str(e.exception)),
  189. (u.exception, "Unresolvable: urn:nothing"),
  190. )
  191. def test_WrappedReferencingError_hashability(self):
  192. """
  193. Ensure the wrapped referencing errors are hashable when possible.
  194. """
  195. with self.assertWarns(DeprecationWarning):
  196. from jsonschema import RefResolutionError
  197. validator = validators.Draft202012Validator({"$ref": "urn:nothing"})
  198. with self.assertRaises(referencing.exceptions.Unresolvable) as u:
  199. validator.validate(12)
  200. with self.assertRaises(RefResolutionError) as e:
  201. validator.validate(12)
  202. self.assertIn(e.exception, {u.exception})
  203. self.assertIn(u.exception, {e.exception})
  204. def test_Validator_subclassing(self):
  205. """
  206. As of v4.12.0, subclassing a validator class produces an explicit
  207. deprecation warning.
  208. This was never intended to be public API (and some comments over the
  209. years in issues said so, but obviously that's not a great way to make
  210. sure it's followed).
  211. A future version will explicitly raise an error.
  212. """
  213. message = "Subclassing validator classes is "
  214. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  215. class Subclass(validators.Draft202012Validator):
  216. pass
  217. self.assertEqual(w.filename, __file__)
  218. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  219. class AnotherSubclass(validators.create(meta_schema={})):
  220. pass
  221. def test_FormatChecker_cls_checks(self):
  222. """
  223. As of v4.14.0, FormatChecker.cls_checks is deprecated without
  224. replacement.
  225. """
  226. self.addCleanup(FormatChecker.checkers.pop, "boom", None)
  227. message = "FormatChecker.cls_checks "
  228. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  229. FormatChecker.cls_checks("boom")
  230. self.assertEqual(w.filename, __file__)
  231. def test_draftN_format_checker(self):
  232. """
  233. As of v4.16.0, accessing jsonschema.draftn_format_checker is deprecated
  234. in favor of Validator.FORMAT_CHECKER.
  235. """
  236. message = "Accessing jsonschema.draft202012_format_checker is "
  237. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  238. from jsonschema import draft202012_format_checker
  239. self.assertIs(
  240. draft202012_format_checker,
  241. validators.Draft202012Validator.FORMAT_CHECKER,
  242. )
  243. self.assertEqual(w.filename, __file__)
  244. message = "Accessing jsonschema.draft201909_format_checker is "
  245. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  246. from jsonschema import draft201909_format_checker
  247. self.assertIs(
  248. draft201909_format_checker,
  249. validators.Draft201909Validator.FORMAT_CHECKER,
  250. )
  251. self.assertEqual(w.filename, __file__)
  252. message = "Accessing jsonschema.draft7_format_checker is "
  253. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  254. from jsonschema import draft7_format_checker
  255. self.assertIs(
  256. draft7_format_checker,
  257. validators.Draft7Validator.FORMAT_CHECKER,
  258. )
  259. self.assertEqual(w.filename, __file__)
  260. message = "Accessing jsonschema.draft6_format_checker is "
  261. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  262. from jsonschema import draft6_format_checker
  263. self.assertIs(
  264. draft6_format_checker,
  265. validators.Draft6Validator.FORMAT_CHECKER,
  266. )
  267. self.assertEqual(w.filename, __file__)
  268. message = "Accessing jsonschema.draft4_format_checker is "
  269. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  270. from jsonschema import draft4_format_checker
  271. self.assertIs(
  272. draft4_format_checker,
  273. validators.Draft4Validator.FORMAT_CHECKER,
  274. )
  275. self.assertEqual(w.filename, __file__)
  276. message = "Accessing jsonschema.draft3_format_checker is "
  277. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  278. from jsonschema import draft3_format_checker
  279. self.assertIs(
  280. draft3_format_checker,
  281. validators.Draft3Validator.FORMAT_CHECKER,
  282. )
  283. self.assertEqual(w.filename, __file__)
  284. with self.assertRaises(ImportError):
  285. from jsonschema import draft1234_format_checker # noqa: F401
  286. def test_import_cli(self):
  287. """
  288. As of v4.17.0, importing jsonschema.cli is deprecated.
  289. """
  290. message = "The jsonschema CLI is deprecated and will be removed "
  291. with self.assertWarnsRegex(DeprecationWarning, message) as w:
  292. import jsonschema.cli
  293. importlib.reload(jsonschema.cli)
  294. self.assertEqual(w.filename, importlib.__file__)
  295. def test_cli(self):
  296. """
  297. As of v4.17.0, the jsonschema CLI is deprecated.
  298. """
  299. process = subprocess.run(
  300. [sys.executable, "-m", "jsonschema"],
  301. capture_output=True,
  302. check=True,
  303. )
  304. self.assertIn(b"The jsonschema CLI is deprecated ", process.stderr)
  305. def test_automatic_remote_retrieval(self):
  306. """
  307. Automatic retrieval of remote references is deprecated as of v4.18.0.
  308. """
  309. ref = "http://bar#/$defs/baz"
  310. schema = {"$defs": {"baz": {"type": "integer"}}}
  311. if "requests" in sys.modules: # pragma: no cover
  312. self.addCleanup(
  313. sys.modules.__setitem__, "requests", sys.modules["requests"],
  314. )
  315. sys.modules["requests"] = None
  316. @contextmanager
  317. def fake_urlopen(request):
  318. self.assertIsInstance(request, urllib.request.Request)
  319. self.assertEqual(request.full_url, "http://bar")
  320. # Ha ha urllib.request.Request "normalizes" header names and
  321. # Request.get_header does not also normalize them...
  322. (header, value), = request.header_items()
  323. self.assertEqual(header.lower(), "user-agent")
  324. self.assertEqual(
  325. value, "python-jsonschema (deprecated $ref resolution)",
  326. )
  327. yield BytesIO(json.dumps(schema).encode("utf8"))
  328. validator = validators.Draft202012Validator({"$ref": ref})
  329. message = "Automatically retrieving remote references "
  330. patch = mock.patch.object(urllib.request, "urlopen", new=fake_urlopen)
  331. with patch, self.assertWarnsRegex(DeprecationWarning, message):
  332. self.assertEqual(
  333. (validator.is_valid({}), validator.is_valid(37)),
  334. (False, True),
  335. )