Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30bec1a

Browse files
committed
Add checker instance to type checking functions
Improves composability, esp for custom types.
1 parent 34227e8 commit 30bec1a

3 files changed

Lines changed: 13 additions & 13 deletions

File tree

jsonschema/_types.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,41 @@
77
from jsonschema.exceptions import UndefinedTypeCheck
88

99

10-
def is_array(instance):
10+
def is_array(checker, instance):
1111
return isinstance(instance, list)
1212

1313

14-
def is_bool(instance):
14+
def is_bool(checker, instance):
1515
return isinstance(instance, bool)
1616

1717

18-
def is_integer(instance):
18+
def is_integer(checker, instance):
1919
# bool inherits from int, so ensure bools aren't reported as ints
2020
if isinstance(instance, bool):
2121
return False
2222
return isinstance(instance, int_types)
2323

2424

25-
def is_null(instance):
25+
def is_null(checker, instance):
2626
return instance is None
2727

2828

29-
def is_number(instance):
29+
def is_number(checker, instance):
3030
# bool inherits from int, so ensure bools aren't reported as ints
3131
if isinstance(instance, bool):
3232
return False
3333
return isinstance(instance, numbers.Number)
3434

3535

36-
def is_object(instance):
36+
def is_object(checker, instance):
3737
return isinstance(instance, dict)
3838

3939

40-
def is_string(instance):
40+
def is_string(checker, instance):
4141
return isinstance(instance, str_types)
4242

4343

44-
def is_any(instance):
44+
def is_any(checker, instance):
4545
return True
4646

4747

@@ -90,7 +90,7 @@ def is_type(self, instance, type):
9090
if type is unknown to this object.
9191
"""
9292
try:
93-
return self._type_checkers[type](instance)
93+
return self._type_checkers[type](self, instance)
9494
except KeyError:
9595
raise UndefinedTypeCheck
9696

jsonschema/tests/test_types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
from jsonschema.validators import Draft4Validator, extend
1212

1313

14-
def is_int_or_string_int(instance):
14+
def is_int_or_string_int(checker, instance):
1515
if Draft4Validator.TYPE_CHECKER.is_type(instance, "integer"):
1616
return True
1717

18-
if Draft4Validator.TYPE_CHECKER.is_type(instance, "string"):
18+
if checker.is_type(instance, "string"):
1919
try:
2020
int(instance)
2121
return True
@@ -32,7 +32,7 @@ def is_namedtuple(instance):
3232
return False
3333

3434

35-
def is_object_or_named_tuple(instance):
35+
def is_object_or_named_tuple(checker, instance):
3636
if Draft4Validator.TYPE_CHECKER.is_type(instance, "object"):
3737
return True
3838

jsonschema/validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def _generate_legacy_type_checks(types=()):
8383
def gen_type_check(pytypes):
8484
pytypes = _utils.flatten(pytypes)
8585

86-
def type_check(instance):
86+
def type_check(checker, instance):
8787
if isinstance(instance, bool):
8888
if bool not in pytypes:
8989
return False

0 commit comments

Comments
 (0)