{"record":{"id":"a62d993c2002ba1c","repo":"python/cpython","slug":"invalid-weekday-day-range-is-1-7","errorCode":null,"errorMessage":"Invalid weekday: {day} (range is [1, 7])","messagePattern":"Invalid weekday: (.+?) \\(range is \\[1, 7\\]\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_pydatetime.py","lineNumber":532,"sourceCode":"    if not MINYEAR <= year <= MAXYEAR:\n        raise ValueError(f\"year must be in {MINYEAR}..{MAXYEAR}, not {year}\")\n\n    if not 0 < week < 53:\n        out_of_range = True\n\n        if week == 53:\n            # ISO years have 53 weeks in them on years starting with a\n            # Thursday and leap years starting on a Wednesday\n            first_weekday = _ymd2ord(year, 1, 1) % 7\n            if (first_weekday == 4 or (first_weekday == 3 and\n                                       _is_leap(year))):\n                out_of_range = False\n\n        if out_of_range:\n            raise ValueError(f\"Invalid week: {week}\")\n\n    if not 0 < day < 8:\n        raise ValueError(f\"Invalid weekday: {day} (range is [1, 7])\")\n\n    # Now compute the offset from (Y, 1, 1) in days:\n    day_offset = (week - 1) * 7 + (day - 1)\n\n    # Calculate the ordinal day for monday, week 1\n    day_1 = _isoweek1monday(year)\n    ord_day = day_1 + day_offset\n\n    return _ord2ymd(ord_day)\n\n\n# Just raise TypeError if the arg isn't None or a string.\ndef _check_tzname(name):\n    if name is not None and not isinstance(name, str):\n        raise TypeError(\"tzinfo.tzname() must return None or string, \"\n                        f\"not {type(name).__name__!r}\")\n\n# name is the offset-producing method, \"utcoffset\" or \"dst\".","sourceCodeStart":514,"sourceCodeEnd":550,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydatetime.py#L514-L550","documentation":"ISO weekday numbers are 1 (Monday) through 7 (Sunday), unlike datetime.isoweekday conventions being the same but user-facing calendars often 0-based. _isoweek_to_gregorian raises ValueError('Invalid weekday: N (range is [1, 7])') for day 0, day 8, or negative values.","triggerScenarios":"date.fromisocalendar(2021, 5, 0); passing a Python date.weekday() result (0..6, Monday=0) where an ISO weekday (1..7) is expected; arrays indexed 0-based fed component-wise into fromisocalendar.","commonSituations":"Confusing date.weekday() (0-6) with date.isoweekday() (1-7); JavaScript/Java weekday conventions crossing into Python; cron-like 0-based day fields reused for ISO weeks.","solutions":["Convert 0-based days: iso_day = zero_based + 1 (verify the source convention first)","Prefer deriving dates via timedelta from a known Monday instead of manual weekday arithmetic","Validate 1 <= day <= 7 before calling fromisocalendar"],"exampleFix":"// before\ndow = some_date.weekday()            # 0..6\nd = date.fromisocalendar(y, w, dow)  # ValueError when dow == 0\n\n# after\ndow = some_date.isoweekday()         # 1..7\nd = date.fromisocalendar(y, w, dow)","handlingStrategy":"validation","validationCode":"def check_iso_weekday(day: int) -> None:\n    if not 1 <= day <= 7:\n        raise ValueError(f'ISO weekday must be 1..7, got {day}')","typeGuard":"def is_valid_iso_weekday(d: object) -> bool:\n    return isinstance(d, int) and 1 <= d <= 7","tryCatchPattern":null,"preventionTips":["Use date.isoweekday() (1..7), never date.weekday() (0..6), when feeding fromisocalendar","Convert 0-based external calendars explicitly: iso = zero_based or 7","Document the 1..7 convention at every fromisocalendar call site"],"tags":["datetime","isocalendar","fromisocalendar","weekday","valueerror","stdlib"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}