Skip to content

Commit

Permalink
Avoid pointless work for objects
Browse files Browse the repository at this point in the history
Calculating the maxProperties lets us avoid a pointless and heavy filter in many cases, which fixes #55.
  • Loading branch information
Zac-HD committed Jun 15, 2020
1 parent a166e70 commit e041bfc
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

#### 0.16.1 - 2020-06-15
- Performance improvement for `object` schemas with `additionalProperties: false` (issue #55)

#### 0.16.0 - 2020-06-07
- Performance improvement for schemas with non-validation keys (such as `description`)
- Errors from e.g. invalid schemas are deferred from import time to become failing tests
Expand Down
2 changes: 1 addition & 1 deletion src/hypothesis_jsonschema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
The only public API is `from_schema`; check the docstring for details.
"""

__version__ = "0.16.0"
__version__ = "0.16.1"
__all__ = ["from_schema"]

from ._from_schema import from_schema
7 changes: 3 additions & 4 deletions src/hypothesis_jsonschema/_canonicalise.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,12 @@ def canonicalish(schema: JSONType) -> Dict[str, Any]:
schema.pop("items", None)
if (
"properties" in schema
and "maxProperties" in schema
and not schema.get("patternProperties")
and schema.get("additionalProperties") == FALSEY
):
schema["maxProperties"] = min(
schema["maxProperties"], len(schema["properties"])
)
max_props = schema.get("maxProperties", math.inf)
assert isinstance(max_props, (int, float))
schema["maxProperties"] = min(max_props, len(schema["properties"]))
if "object" in type_ and schema.get("minProperties", 0) > schema.get(
"maxProperties", math.inf
):
Expand Down
1 change: 1 addition & 0 deletions tests/test_canonicalise.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ def test_canonicalises_to_expected(schema, expected):
{
"properties": {"foo": {"enum": [False, True]}},
"additionalProperties": {"not": {}},
"maxProperties": 1,
},
),
(
Expand Down

0 comments on commit e041bfc

Please sign in to comment.