summaryrefslogtreecommitdiff
path: root/jsonschema
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2019-01-26 12:19:51 -0500
committerJulian Berman <Julian@GrayVines.com>2019-01-26 12:19:51 -0500
commitfc2d4f95de4d063ec74cc140412f698f4f11e78f (patch)
tree80356829a246758daa5a184a637d79f0ba3212c1 /jsonschema
parent548701130bce465e36d5ec505a824607310623a7 (diff)
downloadjsonschema-fc2d4f95de4d063ec74cc140412f698f4f11e78f.tar.gz
Intentionally break mixing the new and old type interfaces.
Diffstat (limited to 'jsonschema')
-rw-r--r--jsonschema/tests/test_validators.py39
-rw-r--r--jsonschema/validators.py11
2 files changed, 33 insertions, 17 deletions
diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py
index 4a25a28..5068627 100644
--- a/jsonschema/tests/test_validators.py
+++ b/jsonschema/tests/test_validators.py
@@ -165,22 +165,6 @@ class TestLegacyTypeCheckCreation(SynchronousTestCase):
self.assertEqual(set(Validator.DEFAULT_TYPES), expected_types)
- @unittest.skip("This logic is actually incorrect.")
- def test_default_types_update_type_checker(self):
- Validator = validators.create(
- meta_schema=self.meta_schema,
- validators=self.validators,
- default_types={u"array": list}
- )
-
- self.assertEqual(set(Validator.DEFAULT_TYPES), {u"array"})
- Extended = validators.extend(
- Validator,
- type_checker=Validator.TYPE_CHECKER.remove(u"array")
- )
-
- self.assertEqual(set(Extended.DEFAULT_TYPES), {})
-
def test_types_redefines_the_validators_type_checker(self):
schema = {"type": "string"}
self.assertFalse(validators.Draft7Validator(schema).is_valid(12))
@@ -243,6 +227,29 @@ class TestLegacyTypeCheckingDeprecation(SynchronousTestCase):
)
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())
diff --git a/jsonschema/validators.py b/jsonschema/validators.py
index 90c0f1b..264a949 100644
--- a/jsonschema/validators.py
+++ b/jsonschema/validators.py
@@ -200,6 +200,7 @@ def create(
raise TypeError(
"Do not specify default_types when providing a type checker.",
)
+ _created_with_default_types = True
warn(
(
"The default_types argument is deprecated. "
@@ -213,6 +214,7 @@ def create(
)
else:
default_types = _DEPRECATED_DEFAULT_TYPES
+ _created_with_default_types = False
if type_checker is None:
type_checker = _types.TypeChecker()
@@ -226,6 +228,7 @@ def create(
DEFAULT_TYPES = property(_DEFAULT_TYPES)
_DEFAULT_TYPES = dict(default_types)
+ _CREATED_WITH_DEFAULT_TYPES = _created_with_default_types
def __init__(
self,
@@ -393,8 +396,14 @@ def extend(validator, validators=(), version=None, type_checker=None):
all_validators = dict(validator.VALIDATORS)
all_validators.update(validators)
- if not type_checker:
+ 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."
+ )
# Set the default_types to None during class creation to avoid
# overwriting the type checker (and triggering the deprecation warning).