Skip to content

Commit e041bfc

Browse files
committed
Avoid pointless work for objects
Calculating the maxProperties lets us avoid a pointless and heavy filter in many cases, which fixes #55.
1 parent a166e70 commit e041bfc

File tree

4 files changed

+8
-5
lines changed

4 files changed

+8
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
#### 0.16.1 - 2020-06-15
4+
- Performance improvement for `object` schemas with `additionalProperties: false` (issue #55)
5+
36
#### 0.16.0 - 2020-06-07
47
- Performance improvement for schemas with non-validation keys (such as `description`)
58
- Errors from e.g. invalid schemas are deferred from import time to become failing tests

src/hypothesis_jsonschema/__init__.py

Lines changed: 1 addition & 1 deletion
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.16.0"
6+
__version__ = "0.16.1"
77
__all__ = ["from_schema"]
88

99
from ._from_schema import from_schema

src/hypothesis_jsonschema/_canonicalise.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,12 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]:
394394
schema.pop("items", None)
395395
if (
396396
"properties" in schema
397-
and "maxProperties" in schema
398397
and not schema.get("patternProperties")
399398
and schema.get("additionalProperties") == FALSEY
400399
):
401-
schema["maxProperties"] = min(
402-
schema["maxProperties"], len(schema["properties"])
403-
)
400+
max_props = schema.get("maxProperties", math.inf)
401+
assert isinstance(max_props, (int, float))
402+
schema["maxProperties"] = min(max_props, len(schema["properties"]))
404403
if "object" in type_ and schema.get("minProperties", 0) > schema.get(
405404
"maxProperties", math.inf
406405
):

tests/test_canonicalise.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ def test_canonicalises_to_expected(schema, expected):
292292
{
293293
"properties": {"foo": {"enum": [False, True]}},
294294
"additionalProperties": {"not": {}},
295+
"maxProperties": 1,
295296
},
296297
),
297298
(

0 commit comments

Comments
 (0)