jsonschema icon indicating copy to clipboard operation
jsonschema copied to clipboard

Instance validates if `multipleOf` is an integer, but fails if it's a float

Open Zac-HD opened this issue 2 years ago • 3 comments

from jsonschema import validate

instance = 9007199254740995
validate(instance, schema={"type": "integer", "multipleOf": 11})    # passes
validate(instance, schema={"type": "integer", "multipleOf": 11.0})  # fails

I'm pretty sure that this is a bug, on the basis that the spec says all integer-valued numbers should be treated as integers.

Found via https://github.com/python-jsonschema/hypothesis-jsonschema/pull/103.

Zac-HD avatar Sep 10 '23 22:09 Zac-HD

Nice :)

Needs thinking though / reading the spec language. It's true the spec says to treat floats with no fractional part as integers, but it also says (or really leaves the JSON spec to say) that floats are an acceptable implementation of number -- and I'm not sure where if anywhere covers what the requirements are on coercion to a common type.

Specifically here, 9007199254740995 is not representable as a float, the closest float is 9007199254740996.0 which is indeed not divisible by 11, so doing the division somehow will require coercing to float.

But it's possible you're both correct on whether this is covered by the spec and also possible this is fixable, not sure, needs thinking.

Julian avatar Sep 10 '23 22:09 Julian

Both passed on older versions of jsonschema, so for whatever it's worth this probably changed in version 4.18. I've just skipped the affected test though, and happy to go with your opinion 👍

Zac-HD avatar Sep 10 '23 23:09 Zac-HD

Ah that indeed is interesting! Will have to remind myself.

On Sun, Sep 10, 2023, 19:27 Zac Hatfield-Dodds @.***> wrote:

Both passed on older versions of jsonschema, so for whatever it's worth this probably changed in version 4.18. I've just skipped the affected test though, and happy to go with your opinion 👍

— Reply to this email directly, view it on GitHub https://github.com/python-jsonschema/jsonschema/issues/1159#issuecomment-1712970145, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACQQXWKRVOUYR6GRW3MERDXZZEHZANCNFSM6AAAAAA4SOQ2YY . You are receiving this because you commented.Message ID: @.***>

Julian avatar Sep 10 '23 23:09 Julian