Skip to content

Unhandled OverflowError when checking multipleOf with large numbers #743

Closed
@Zac-HD

Description

@Zac-HD
>>> jsonschema.validate(1e308, {"type": "integer", "multipleOf": 0.5})

Traceback (most recent call last):
    ...
    File "jsonschema/_validators.py", line 170, in multipleOf
        failed = int(quotient) != quotient
OverflowError: cannot convert float infinity to integer

So the specific problem is that while 1e308 is finite, 1e308 / 0.5 overflows to infinity and then this raises an error when int(inf) is called. (it's a floating-point issue, but at least it's not "division doesn't work like real numbers"!)

I do consider this a bug because attempting to validate a well-formed instance with a well-formed schema should return either True or False, not raise an error. You could note that the spec allows implementations to limit the range of numbers they accept, but if so I'd still want a better error message and more specific exception type.

Of course, the question of which this should return is a somewhat ambiguous...

  • 0.5 is exactly representable as a float, 1e308 ==int(1e308), and obviously integers are all multiples of 0.5. My personal preference is therefore to call this instance valid and that's what master...Zac-HD:multipleOf-overflow would do.

  • The spec for multipleOf just says "A numeric instance is valid only if division by this keyword's value results in an integer"; overflowing to infinity is not an integer, and therefore the instance is invalid. That could easily be implemented with except OverflowError: failed = True.

In either case, we should probably also add an optional test to the upstream test suite...

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething doesn't work the way it should.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions