diff options
author | Julian Berman <Julian@GrayVines.com> | 2021-05-01 13:56:23 -0400 |
---|---|---|
committer | Julian Berman <Julian@GrayVines.com> | 2021-05-04 17:03:20 -0400 |
commit | d9f63840609176f0b7328f2088c3b2a4016526cd (patch) | |
tree | a32935dbb4f886487976560e9e6b940df23bb60b | |
parent | 83c652f2056c373873b7ef50f8312b66a8c619c7 (diff) | |
download | jsonschema-d9f63840609176f0b7328f2088c3b2a4016526cd.tar.gz |
Remove default types (in favor of the TypeChecker API).
This has been deprecated for ~2 years, so should be good to go.
Closes: #681.
-rw-r--r-- | jsonschema/tests/test_jsonschema_test_suite.py | 30 | ||||
-rw-r--r-- | jsonschema/tests/test_validators.py | 220 | ||||
-rw-r--r-- | jsonschema/validators.py | 149 |
3 files changed, 2 insertions, 397 deletions
diff --git a/jsonschema/tests/test_jsonschema_test_suite.py b/jsonschema/tests/test_jsonschema_test_suite.py index a3b8a0b..813182d 100644 --- a/jsonschema/tests/test_jsonschema_test_suite.py +++ b/jsonschema/tests/test_jsonschema_test_suite.py @@ -7,7 +7,6 @@ See https://github.com/json-schema-org/JSON-Schema-Test-Suite for details. """ import sys -import warnings from jsonschema import ( Draft3Validator, @@ -21,7 +20,6 @@ from jsonschema import ( ) from jsonschema.tests._helpers import bug from jsonschema.tests._suite import Suite -from jsonschema.validators import _DEPRECATED_DEFAULT_TYPES, create SUITE = Suite() DRAFT3 = SUITE.version(name="draft3") @@ -498,31 +496,3 @@ TestDraft7 = DRAFT7.to_unittest_testcase( )(test) ), ) - - -with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) - - TestDraft3LegacyTypeCheck = DRAFT3.to_unittest_testcase( - # Interestingly the any part couldn't really be done w/the old API. - ( - (test for test in each if test.schema != {"type": "any"}) - for each in DRAFT3.tests_of(name="type") - ), - name="TestDraft3LegacyTypeCheck", - Validator=create( - meta_schema=Draft3Validator.META_SCHEMA, - validators=Draft3Validator.VALIDATORS, - default_types=_DEPRECATED_DEFAULT_TYPES, - ), - ) - - TestDraft4LegacyTypeCheck = DRAFT4.to_unittest_testcase( - DRAFT4.tests_of(name="type"), - name="TestDraft4LegacyTypeCheck", - Validator=create( - meta_schema=Draft4Validator.META_SCHEMA, - validators=Draft4Validator.VALIDATORS, - default_types=_DEPRECATED_DEFAULT_TYPES, - ), - ) diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index 2f017b9..1fbcc3f 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -176,226 +176,6 @@ class TestCreateAndExtend(SynchronousTestCase): self.assertEqual(Derived.ID_OF(Derived.META_SCHEMA), correct_id) -class TestLegacyTypeChecking(SynchronousTestCase): - def test_create_default_types(self): - Validator = validators.create(meta_schema={}, validators=()) - self.assertEqual( - set(Validator.DEFAULT_TYPES), { - u"array", - u"boolean", - u"integer", - u"null", - u"number", - u"object", u"string", - }, - ) - self.flushWarnings() - - def test_extend(self): - Validator = validators.create(meta_schema={}, validators=()) - original = dict(Validator.VALIDATORS) - new = object() - - Extended = validators.extend( - Validator, - validators={u"new": new}, - ) - self.assertEqual( - ( - Extended.VALIDATORS, - Extended.META_SCHEMA, - Extended.TYPE_CHECKER, - Validator.VALIDATORS, - - Extended.DEFAULT_TYPES, - Extended({}).DEFAULT_TYPES, - self.flushWarnings()[0]["message"], - ), ( - dict(original, new=new), - Validator.META_SCHEMA, - Validator.TYPE_CHECKER, - original, - - Validator.DEFAULT_TYPES, - Validator.DEFAULT_TYPES, - self.flushWarnings()[0]["message"], - ), - ) - - def test_types_redefines_the_validators_type_checker(self): - schema = {"type": "string"} - self.assertFalse(validators.Draft7Validator(schema).is_valid(12)) - - validator = validators.Draft7Validator( - schema, - types={"string": (str, int)}, - ) - self.assertTrue(validator.is_valid(12)) - self.flushWarnings() - - def test_providing_default_types_warns(self): - self.assertWarns( - category=DeprecationWarning, - message=( - "The default_types argument is deprecated. " - "Use the type_checker argument instead." - ), - # https://tm.tl/9363 :'( - filename=sys.modules[self.assertWarns.__module__].__file__, - - f=validators.create, - meta_schema={}, - validators={}, - default_types={"foo": object}, - ) - - def test_cannot_ask_for_default_types_with_non_default_type_checker(self): - """ - We raise an error when you ask a validator with non-default - type checker for its DEFAULT_TYPES. - - The type checker argument is new, so no one but this library - itself should be trying to use it, and doing so while then - asking for DEFAULT_TYPES makes no sense (not to mention is - deprecated), since type checkers are not strictly about Python - type. - """ - Validator = validators.create( - meta_schema={}, - validators={}, - type_checker=TypeChecker(), - ) - with self.assertRaises(validators._DontDoThat) as e: - Validator.DEFAULT_TYPES - - self.assertIn( - "DEFAULT_TYPES cannot be used on Validators using TypeCheckers", - str(e.exception), - ) - with self.assertRaises(validators._DontDoThat): - Validator({}).DEFAULT_TYPES - - self.assertFalse(self.flushWarnings()) - - def test_providing_explicit_type_checker_does_not_warn(self): - Validator = validators.create( - meta_schema={}, - validators={}, - type_checker=TypeChecker(), - ) - self.assertFalse(self.flushWarnings()) - - Validator({}) - self.assertFalse(self.flushWarnings()) - - def test_providing_neither_does_not_warn(self): - Validator = validators.create(meta_schema={}, validators={}) - self.assertFalse(self.flushWarnings()) - - Validator({}) - self.assertFalse(self.flushWarnings()) - - def test_providing_default_types_with_type_checker_errors(self): - with self.assertRaises(TypeError) as e: - validators.create( - meta_schema={}, - validators={}, - default_types={"foo": object}, - type_checker=TypeChecker(), - ) - - self.assertIn( - "Do not specify default_types when providing a type checker", - str(e.exception), - ) - self.assertFalse(self.flushWarnings()) - - def test_extending_a_legacy_validator_with_a_type_checker_errors(self): - Validator = validators.create( - meta_schema={}, - validators={}, - default_types={u"array": list} - ) - with self.assertRaises(TypeError) as e: - validators.extend( - Validator, - validators={}, - type_checker=TypeChecker(), - ) - - self.assertIn( - ( - "Cannot extend a validator created with default_types " - "with a type_checker. Update the validator to use a " - "type_checker when created." - ), - str(e.exception), - ) - self.flushWarnings() - - def test_extending_a_legacy_validator_does_not_rewarn(self): - Validator = validators.create(meta_schema={}, default_types={}) - self.assertTrue(self.flushWarnings()) - - validators.extend(Validator) - self.assertFalse(self.flushWarnings()) - - def test_accessing_default_types_warns(self): - Validator = validators.create(meta_schema={}, validators={}) - self.assertFalse(self.flushWarnings()) - - self.assertWarns( - DeprecationWarning, - ( - "The DEFAULT_TYPES attribute is deprecated. " - "See the type checker attached to this validator instead." - ), - # https://tm.tl/9363 :'( - sys.modules[self.assertWarns.__module__].__file__, - - getattr, - Validator, - "DEFAULT_TYPES", - ) - - def test_accessing_default_types_on_the_instance_warns(self): - Validator = validators.create(meta_schema={}, validators={}) - self.assertFalse(self.flushWarnings()) - - self.assertWarns( - DeprecationWarning, - ( - "The DEFAULT_TYPES attribute is deprecated. " - "See the type checker attached to this validator instead." - ), - # https://tm.tl/9363 :'( - sys.modules[self.assertWarns.__module__].__file__, - - getattr, - Validator({}), - "DEFAULT_TYPES", - ) - - def test_providing_types_to_init_warns(self): - Validator = validators.create(meta_schema={}, validators={}) - self.assertFalse(self.flushWarnings()) - - self.assertWarns( - category=DeprecationWarning, - message=( - "The types argument is deprecated. " - "Provide a type_checker to jsonschema.validators.extend " - "instead." - ), - # https://tm.tl/9363 :'( - filename=sys.modules[self.assertWarns.__module__].__file__, - - f=Validator, - schema={}, - types={"bar": object}, - ) - - class TestIterErrors(TestCase): def setUp(self): self.validator = validators.Draft3Validator({}) diff --git a/jsonschema/validators.py b/jsonschema/validators.py index 5bd908f..70d46c2 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -8,7 +8,6 @@ from urllib.request import urlopen from warnings import warn import contextlib import json -import numbers from jsonschema import ( _legacy_validators, @@ -25,71 +24,10 @@ from jsonschema.exceptions import ErrorTree ErrorTree -class _DontDoThat(Exception): - """ - Raised when a Validators with non-default type checker is misused. - - Asking one for DEFAULT_TYPES doesn't make sense, since type checkers - exist for the unrepresentable cases where DEFAULT_TYPES can't - represent the type relationship. - """ - - def __str__(self): - return "DEFAULT_TYPES cannot be used on Validators using TypeCheckers" - - validators = {} meta_schemas = _utils.URIDict() -def _generate_legacy_type_checks(types=()): - """ - Generate newer-style type checks out of JSON-type-name-to-type mappings. - - Arguments: - - types (dict): - - A mapping of type names to their Python types - - Returns: - - A dictionary of definitions to pass to `TypeChecker` - """ - types = dict(types) - - def gen_type_check(pytypes): - pytypes = _utils.flatten(pytypes) - - def type_check(checker, instance): - if isinstance(instance, bool): - if bool not in pytypes: - return False - return isinstance(instance, pytypes) - - return type_check - - definitions = {} - for typename, pytypes in types.items(): - definitions[typename] = gen_type_check(pytypes) - - return definitions - - -_DEPRECATED_DEFAULT_TYPES = { - u"array": list, - u"boolean": bool, - u"integer": int, - u"null": type(None), - u"number": numbers.Number, - u"object": dict, - u"string": str, -} -_TYPE_CHECKER_FOR_DEPRECATED_DEFAULT_TYPES = _types.TypeChecker( - type_checkers=_generate_legacy_type_checks(_DEPRECATED_DEFAULT_TYPES), -) - - def validates(version): """ Register the decorated validator for a ``version`` of the specification. @@ -119,25 +57,6 @@ def validates(version): return _validates -def _DEFAULT_TYPES(self): - if self._CREATED_WITH_DEFAULT_TYPES is None: - raise _DontDoThat() - - warn( - ( - "The DEFAULT_TYPES attribute is deprecated. " - "See the type checker attached to this validator instead." - ), - DeprecationWarning, - stacklevel=2, - ) - return self._DEFAULT_TYPES - - -class _DefaultTypesDeprecatingMetaClass(type): - DEFAULT_TYPES = property(_DEFAULT_TYPES) - - def _id_of(schema): if schema is True or schema is False: return u"" @@ -148,8 +67,7 @@ def create( meta_schema, validators=(), version=None, - default_types=None, - type_checker=None, + type_checker=_types.draft7_type_checker, id_of=_id_of, ): """ @@ -189,16 +107,6 @@ def create( If unprovided, a `jsonschema.TypeChecker` will be created with a set of default types typical of JSON Schema drafts. - default_types (collections.abc.Mapping): - - .. deprecated:: 3.0.0 - - Please use the type_checker argument instead. - - If set, it provides mappings of JSON types to Python types - that will be converted to functions and redefined in this - object's `jsonschema.TypeChecker`. - id_of (collections.abc.Callable): A function that given a schema, returns its ID. @@ -208,66 +116,19 @@ def create( a new `jsonschema.IValidator` class """ - if default_types is not None: - if type_checker is not None: - raise TypeError( - "Do not specify default_types when providing a type checker.", - ) - _created_with_default_types = True - warn( - ( - "The default_types argument is deprecated. " - "Use the type_checker argument instead." - ), - DeprecationWarning, - stacklevel=2, - ) - type_checker = _types.TypeChecker( - type_checkers=_generate_legacy_type_checks(default_types), - ) - else: - default_types = _DEPRECATED_DEFAULT_TYPES - if type_checker is None: - _created_with_default_types = False - type_checker = _TYPE_CHECKER_FOR_DEPRECATED_DEFAULT_TYPES - elif type_checker is _TYPE_CHECKER_FOR_DEPRECATED_DEFAULT_TYPES: - _created_with_default_types = False - else: - _created_with_default_types = None - - class Validator(metaclass=_DefaultTypesDeprecatingMetaClass): + class Validator: VALIDATORS = dict(validators) META_SCHEMA = dict(meta_schema) TYPE_CHECKER = type_checker ID_OF = staticmethod(id_of) - DEFAULT_TYPES = property(_DEFAULT_TYPES) - _DEFAULT_TYPES = dict(default_types) - _CREATED_WITH_DEFAULT_TYPES = _created_with_default_types - def __init__( self, schema, - types=(), resolver=None, format_checker=None, ): - if types: - warn( - ( - "The types argument is deprecated. Provide " - "a type_checker to jsonschema.validators.extend " - "instead." - ), - DeprecationWarning, - stacklevel=2, - ) - - self.TYPE_CHECKER = self.TYPE_CHECKER.redefine_many( - _generate_legacy_type_checks(types), - ) - if resolver is None: resolver = RefResolver.from_schema(schema, id_of=id_of) @@ -414,12 +275,6 @@ def extend(validator, validators=(), version=None, type_checker=None): if type_checker is None: type_checker = validator.TYPE_CHECKER - elif validator._CREATED_WITH_DEFAULT_TYPES: - raise TypeError( - "Cannot extend a validator created with default_types " - "with a type_checker. Update the validator to use a " - "type_checker when created." - ) return create( meta_schema=validator.META_SCHEMA, validators=all_validators, |