{"record":{"id":"6f307a737501edbc","repo":"python/cpython","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":1234,"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":1216,"sourceCodeEnd":1252,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydatetime.py#L1216-L1252","documentation":"Raised by date.__add__ when adding a timedelta to a date produces an ordinal day number outside the representable range (1 to date.max.toordinal(), _MAXORDINAL). Python dates span 0001-01-01 to 9999-12-31, and arithmetic that crosses either boundary raises OverflowError instead of wrapping. Note __sub__ with a timedelta delegates to __add__ with the negated timedelta, so it can raise the same error.","triggerScenarios":"date.min - timedelta(days=1); date.max + timedelta(days=1); date(1,1,1) + timedelta(days=-365*3000); date(9999,12,31) + timedelta(weeks=2); any accumulated loop of date += timedelta that eventually steps past date.max.","commonSituations":"Date arithmetic over long horizons (interest calculation to year 9999+), unbounded while-loops that increment a date, subtracting large timedeltas from early dates, or business logic projecting far-future expiry dates.","solutions":["Clamp inputs before arithmetic: verify date.min <= d + td <= date.max before applying","Bound the loop or offset so the running date stays within date.min..date.max","Catch OverflowError at the call site and handle the boundary case explicitly (e.g. cap at date.max)"],"exampleFix":"// before\nnew_date = d + timedelta(days=offset)  # OverflowError near boundaries\n\n// after\nnew_date = d + timedelta(days=offset) if date.min <= d + timedelta(days=offset) <= date.max else None\nif new_date is None:\n    new_date = date.max if offset > 0 else date.min","handlingStrategy":"validation","validationCode":"from datetime import date, timedelta\n\ndef safe_add(d: date, td: timedelta) -> date:\n    lo = d.toordinal() + td.days\n    if 1 <= lo <= date.max.toordinal():\n        return d + td\n    raise OverflowError(f'{d} + {td} leaves representable range')","typeGuard":null,"tryCatchPattern":"try:\n    new_d = d + td\nexcept OverflowError:\n    new_d = date.max if td.days > 0 else date.min  # explicit clamp policy","preventionTips":["Bound date-iteration loops by date.max explicitly","Validate horizon lengths (days/weeks) before applying to dates near 0001 or 9999","Treat dates outside year 1..9999 as a data-quality problem at ingest"],"tags":["datetime","overflowerror","date-arithmetic","range"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}