Skip to content

Commit 6729f88

Browse files
committed
Fix PyPy multipleOf float-to-int conversion
On CPython, encode_canonical_json converts integer-valued floats to ints, but on PyPy the custom JSON encoder doesn't work (it's set to None). Add explicit float-to-int conversion for multipleOf with pragma: no cover since this branch is only exercised on PyPy.
1 parent da3de68 commit 6729f88

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/hypothesis_jsonschema/_canonicalise.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,13 @@ def canonicalish(schema: JSONType) -> dict[str, Any]:
314314
for k, v in schema[key].items()
315315
}
316316
# multipleOf is semantically unaffected by the sign, so ensure it's positive.
317-
# Note: encode_canonical_json already converts integer-valued floats to ints.
317+
# On CPython, encode_canonical_json already converts integer-valued floats to ints,
318+
# but on PyPy the custom encoder doesn't work so we need to do it explicitly here.
318319
if "multipleOf" in schema:
319-
schema["multipleOf"] = abs(schema["multipleOf"])
320+
mul = abs(schema["multipleOf"])
321+
if isinstance(mul, float) and mul.is_integer(): # pragma: no cover
322+
mul = int(mul) # Needed for PyPy compatibility
323+
schema["multipleOf"] = mul
320324

321325
type_ = get_type(schema)
322326
if "number" in type_:

0 commit comments

Comments
 (0)