From 4b6d1c95a3aaf6d3e9b1acd8e283cee9c90ec862 Mon Sep 17 00:00:00 2001 From: Thor Whalen <1906276+thorwhalen@users.noreply.github.com> Date: Sun, 6 Dec 2026 01:06:00 +0000 Subject: [PATCH] FutureWarning when a properties schema has no 'required' key and its signature will change (#12) --- ju/json_schema.py | 33 +++++++++++++++++++++++++++++---- ju/tests/json_schema_test.py | 18 +++++++++++++++++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/ju/json_schema.py b/ju/json_schema.py index 19d1997..122cfc9 100644 --- a/ju/json_schema.py +++ b/ju/json_schema.py @@ -30,6 +30,7 @@ from functools import partial import re import keyword +import warnings from copy import deepcopy from pydantic import BaseModel @@ -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', @@ -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) @@ -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: diff --git a/ju/tests/json_schema_test.py b/ju/tests/json_schema_test.py index 3df58d5..1bd1f7c 100644 --- a/ju/tests/json_schema_test.py +++ b/ju/tests/json_schema_test.py @@ -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}