python-jsonschema / python-jsonschema/jsonschema

is_duration raises an uncaught decimal.Overflow on a duration with a large exponent

Open Beginner friendly
#1,511 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Test Upstream
Dominant language
Python
Stars
5k
Forks
671
Avg merge
1d 1h
Merged PRs (30d)
10

Description

The duration format check can raise decimal.Overflow instead of returning a result, so validating certain strings throws an uncaught exception rather than reporting the instance as invalid.

Versions
  • jsonschema 4.25.1
  • isoduration 20.11.0 (the optional dependency is_duration delegates to)
  • Python 3.9
Reproduction
from jsonschema import Draft202012Validator as V
from jsonschema import FormatChecker

v = V({"format": "duration"}, format_checker=FormatChecker())
list(v.iter_errors("P1E1000000D"))

raises:

decimal.Overflow: [<class 'decimal.Overflow'>]
Cause

_format.is_duration calls isoduration.parse_duration(instance) and is registered with raises=isoduration.DurationParsingException. isoduration parses each component amount with Decimal(...), and for an exponent past the decimal context's Emax (999999), Decimal("1E1000000") raises decimal.Overflow. That is not a subclass of DurationParsingException, so it propagates out of the format checker uncaught.

The boundary is exact: P1E999999D is accepted, P1E1000000D overflows. It is also reachable without an exponent, via a plain digit run longer than Emax.

Expected

The check should report the instance as invalid (P1E1000000D is not a valid RFC 3339 Appendix A duration - the grammar uses 1*DIGIT, with no exponent), not raise.

Possible fix

Either broaden what is_duration catches (add ArithmeticError / decimal.Overflow alongside DurationParsingException, or wrap the parse_duration call), or treat any exception from parse_duration as "not a duration".

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the _format.is_duration entry point and inspect how it calls isoduration.parse_duration and registers exceptions with FormatChecker. Run the provided P1E1000000D reproduction, then verify that validation reports the instance as invalid without propagating decimal.Overflow, while the boundary behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.