Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions ju/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from functools import partial
import re
import keyword
import warnings
from copy import deepcopy

from pydantic import BaseModel
Expand Down Expand Up @@ -453,10 +454,13 @@ def json_schema_to_signature(
calls bind required parameters in schema order.

A 'properties' schema with no ``required`` key at all keeps the legacy behavior
(only properties with their own ``default`` are optional, ``default_default`` is
not applied), so positional callers of such schemas are not re-bound. Say
``'required': []`` to make everything optional. A malformed ``required``
(not a list) raises ``ValueError``.
for now (only properties with their own ``default`` are optional,
``default_default`` is not applied), so positional callers of such schemas are not
re-bound, but it emits a ``FutureWarning``: JSON Schema says no ``required`` key
means nothing is required, and a future release will follow it. Say
``'required': []`` to make everything optional now, or list the required
names to keep today's signature. A malformed ``required`` (not a list) raises
``ValueError``.

>>> schema = {'title': 'earth',
... 'type': 'object',
Expand Down Expand Up @@ -527,6 +531,8 @@ def json_schema_to_signature(
elif "properties" in json_schema:
properties = json_schema["properties"]
required = _required_names(json_schema) # None when there is no such key
if required is None:
_warn_if_missing_required_will_change(properties)
for name, field in properties.items():
is_required = required is not None and name in required
name = next(pynames)
Expand Down Expand Up @@ -566,6 +572,25 @@ def json_schema_to_signature(
# Utils


def _warn_if_missing_required_will_change(properties: Mapping):
"""Warn when a schema with no ``required`` key will be read differently once the
JSON Schema semantics (no ``required`` key means nothing is required) are adopted:
that is, when a property has no ``default`` of its own."""
if any(
not (isinstance(field, Mapping) and "default" in field)
for field in properties.values()
):
warnings.warn(
"json_schema_to_signature: this schema has no 'required' key, so its "
"properties without a 'default' are currently required. In a future "
"release they will be optional (JSON Schema semantics), and optional "
"parameters will move after required ones. Add an explicit 'required' "
"list to keep today's signature.",
FutureWarning,
stacklevel=3,
)


def _required_names(json_schema: dict):
"""Names in the schema's top-level ``required`` list; None if there is no such key."""
if "required" not in json_schema:
Expand Down
18 changes: 17 additions & 1 deletion ju/tests/json_schema_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,27 @@ def test_properties_branch_missing_required_key_keeps_legacy_binding():
"c": {"type": "integer"},
},
}
sig = json_schema_to_signature(schema)
with pytest.warns(FutureWarning, match="no 'required' key"):
sig = json_schema_to_signature(schema)
assert list(sig.parameters) == ["a", "c", "b"]
assert sig.bind(1, 2).arguments == {"a": 1, "c": 2}


def test_missing_required_key_warns_only_when_the_signature_would_change():
import warnings

all_defaulted = {"properties": {"a": {"default": 1}, "b": {"default": 2}}}
with warnings.catch_warnings():
warnings.simplefilter("error")
json_schema_to_signature(all_defaulted)
json_schema_to_signature({"properties": {}})
json_schema_to_signature({"properties": {"a": {}}, "required": ["a"]})
json_schema_to_signature({"properties": {"a": {}}, "required": []})
with pytest.warns(FutureWarning) as record:
json_schema_to_signature({"properties": {"a": {}}})
assert record[0].filename == __file__ # points at the caller


@pytest.mark.parametrize("bad", ["a", True, {"a": 1}, None])
def test_malformed_required_raises(bad):
schema = {"type": "object", "properties": {"a": {}}, "required": bad}
Expand Down
Loading