{"record":{"id":"68205f7bcc119522","repo":"RustPython/RustPython","slug":"result-out-of-range","errorCode":null,"errorMessage":"result out of range","messagePattern":"result out of range","errorType":"exception","errorClass":"OverflowError","httpStatus":null,"severity":"error","filePath":"Lib/_pydatetime.py","lineNumber":1218,"sourceCode":"        y, m, d = self._year, self._month, self._day\n        y2, m2, d2 = other._year, other._month, other._day\n        return _cmp((y, m, d), (y2, m2, d2))\n\n    def __hash__(self):\n        \"Hash.\"\n        if self._hashcode == -1:\n            self._hashcode = hash(self._getstate())\n        return self._hashcode\n\n    # Computations\n\n    def __add__(self, other):\n        \"Add a date to a timedelta.\"\n        if isinstance(other, timedelta):\n            o = self.toordinal() + other.days\n            if 0 < o <= _MAXORDINAL:\n                return type(self).fromordinal(o)\n            raise OverflowError(\"result out of range\")\n        return NotImplemented\n\n    __radd__ = __add__\n\n    def __sub__(self, other):\n        \"\"\"Subtract two dates, or a date and a timedelta.\"\"\"\n        if isinstance(other, timedelta):\n            return self + timedelta(-other.days)\n        if isinstance(other, date):\n            days1 = self.toordinal()\n            days2 = other.toordinal()\n            return timedelta(days1 - days2)\n        return NotImplemented\n\n    def weekday(self):\n        \"Return day of the week, where Monday == 0 ... Sunday == 6.\"\n        return (self.toordinal() + 6) % 7\n","sourceCodeStart":1200,"sourceCodeEnd":1236,"githubUrl":"https://github.com/RustPython/RustPython/blob/aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd/Lib/_pydatetime.py#L1200-L1236","documentation":"Raised by date.__add__ when adding a timedelta moves the result outside the representable date range (year 1-01-01 through year 9999-12-31, i.e. ordinals 1 to date.max.toordinal()). Dates are stored as proleptic Gregorian ordinals and cannot wrap around or go below day 1, so any addition whose target ordinal is <= 0 or > _MAXORDINAL is rejected with OverflowError instead of producing an invalid date.","triggerScenarios":"date.max + timedelta(days=1); date(1, 1, 1) + timedelta(days=-1); date(9999, 12, 31) + timedelta(weeks=2); day-stepping loops (d += timedelta(days=1)) that run past the horizon; large user-supplied offsets such as timedelta(days=400000) added to arbitrary dates.","commonSituations":"Scheduled-event generators iterating forward without a stop check; billing or maturity date math that adds years as big day counts; porting arithmetic from systems that clamp or wrap dates; property/fuzz tests adding random timedeltas to random dates.","solutions":["Validate the target ordinal before adding: 1 <= d.toordinal() + delta.days <= date.max.toordinal(); reject or clamp the input if outside.","Catch OverflowError at the boundary and clamp to date.min/date.max or surface a user-facing 'date out of range' message.","Bound the offset itself (cap timedelta.days) when it originates from user input or configuration.","If dates outside year 1..9999 are genuinely needed, switch representation (integer year/month/day or a third-party date type) instead of fighting the limit."],"exampleFix":"// before\nd = start + timedelta(days=offset)  # OverflowError when start is near date.max\n\n// after\nfrom datetime import date, timedelta\n\ndef add_days(d: date, offset: int) -> date:\n    ordinal = d.toordinal() + offset\n    if not 1 <= ordinal <= date.max.toordinal():\n        raise OverflowError(f'{offset} days from {d} leaves year 1..9999')\n    return d + timedelta(days=offset)","handlingStrategy":"validation","validationCode":"from datetime import date, timedelta\n\ndef safe_date_add(d: date, delta: timedelta) -> date:\n    target = d.toordinal() + delta.days\n    if not 1 <= target <= date.max.toordinal():\n        raise OverflowError(f'{delta} moves {d} outside year 1..9999')\n    return d + delta","typeGuard":null,"tryCatchPattern":"try:\n    result = d + delta\nexcept OverflowError:\n    result = date.max  # or reject the request with a user-facing error","preventionTips":["Check d.toordinal() + delta.days against 1..date.max.toordinal() before adding.","Cap user-supplied day/week offsets at ingestion.","Give day-stepping loops an explicit date.max stop condition.","Remember timedelta.days truncates toward zero for sub-day deltas."],"tags":["datetime","date","timedelta","overflow","range-check"],"backgroundTag":"datetime-arithmetic-overflow","analyzedSha":"aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd","analyzedAt":"2026-08-17T00:37:52.100Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}