Skip to content

Commit f40063a

Browse files
Stranger6667Zac-HD
authored andcommitted
Improve performance for conditional keywords
1 parent 70ec36a commit f40063a

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

3+
#### 0.18.2 - 2020-11-22
34
- Remove internal caching due to hash collisions (#71)
5+
- Improve performance for conditional keywords
46

57
#### 0.18.1 - 2020-11-21
68
- Canonicalise `anyOf` special cases when all subschemas have only the `type` keyword

src/hypothesis_jsonschema/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
The only public API is `from_schema`; check the docstring for details.
44
"""
55

6-
__version__ = "0.18.1"
6+
__version__ = "0.18.2"
77
__all__ = ["from_schema"]
88

99
from ._from_schema import from_schema

src/hypothesis_jsonschema/_canonicalise.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,12 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]:
243243
then = schema.pop("then", schema)
244244
else_ = schema.pop("else", schema)
245245
if if_ is not None and (then is not schema or else_ is not schema):
246-
schema = {
247-
"anyOf": [
246+
if then not in (if_, TRUTHY) or else_ != TRUTHY:
247+
alternatives = [
248248
{"allOf": [if_, then, schema]},
249249
{"allOf": [{"not": if_}, else_, schema]},
250250
]
251-
}
251+
schema = canonicalish({"anyOf": alternatives})
252252
assert isinstance(schema, dict)
253253
# Recurse into the value of each keyword with a schema (or list of them) as a value
254254
for key in SCHEMA_KEYS:

tests/test_canonicalise.py

+16
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,22 @@ def test_canonicalises_to_empty(schema):
260260
# TODO: could be {"enum": ["a", "b", "c"]},
261261
{"anyOf": [{"const": "a"}, {"const": "b"}, {"const": "c"}]},
262262
),
263+
(
264+
{"if": {"type": "null"}, "then": {"type": "null"}},
265+
{},
266+
),
267+
(
268+
{"if": {"type": "null"}, "then": {"type": "null"}, "else": {}},
269+
{},
270+
),
271+
(
272+
{"if": {"type": "null"}, "then": {}, "else": {}},
273+
{},
274+
),
275+
(
276+
{"if": {"type": "integer"}, "then": {}, "else": {}, "type": "number"},
277+
{"type": "number"},
278+
),
263279
],
264280
)
265281
def test_canonicalises_to_expected(schema, expected):

0 commit comments

Comments
 (0)