{"record":{"id":"4a9cb8517a1c2cde","repo":"python/cpython","slug":"iso-week-directive-v-must-be-used-with-the-iso","errorCode":null,"errorMessage":"ISO week directive '%V' must be used with the ISO year directive '%G' and a weekday directive ('%A', '%a', '%w', or '%u').","messagePattern":"ISO week directive '%V' must be used with the ISO year directive '%G' and a weekday directive \\('%A', '%a', '%w', or '%u'\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_strptime.py","lineNumber":746,"sourceCode":"                        break\n                    else:\n                        tz = value\n                        break\n\n    # Deal with the cases where ambiguities arise\n    # don't assume default values for ISO week/year\n    if iso_year is not None:\n        if julian is not None:\n            raise ValueError(\"Day of the year directive '%j' is not \"\n                             \"compatible with ISO year directive '%G'. \"\n                             \"Use '%Y' instead.\")\n        elif iso_week is None or weekday is None:\n            raise ValueError(\"ISO year directive '%G' must be used with \"\n                             \"the ISO week directive '%V' and a weekday \"\n                             \"directive ('%A', '%a', '%w', or '%u').\")\n    elif iso_week is not None:\n        if year is None or weekday is None:\n            raise ValueError(\"ISO week directive '%V' must be used with \"\n                             \"the ISO year directive '%G' and a weekday \"\n                             \"directive ('%A', '%a', '%w', or '%u').\")\n        else:\n            raise ValueError(\"ISO week directive '%V' is incompatible with \"\n                             \"the year directive '%Y'. Use the ISO year '%G' \"\n                             \"instead.\")\n\n    leap_year_fix = False\n    if year is None:\n        if month == 2 and day == 29:\n            year = 1904  # 1904 is first leap year of 20th century\n            leap_year_fix = True\n        else:\n            year = 1900\n\n    # If we know the week of the year and what day of that week, we can figure\n    # out the Julian day of the year.\n    if julian is None and weekday is not None:","sourceCodeStart":728,"sourceCodeEnd":764,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_strptime.py#L728-L764","documentation":"Mirror of the %G case: %V matched (iso_week is not None) but the regular year is missing or no weekday directive matched, so the week number cannot be anchored to a year. _strptime refuses rather than defaulting the year (defaulting ISO values is explicitly avoided per the comment 'don't assume default values for ISO week/year').","triggerScenarios":"datetime.strptime('W12 3', 'W%V %u') — %V with a weekday but no year; or datetime.strptime('W12', 'W%V') with neither year nor weekday.","commonSituations":"Parsing week numbers without year context (retail/financial week reports); log formats that emit only week and weekday.","solutions":["Add %G (and a weekday if missing) to the format: '2020 W12 3' with '%G W%V %u'","If %V must pair with a plain year, that is error 272's case — it is not allowed; convert the year to an ISO year first (date.isocalendar())"],"exampleFix":"# before\n>>> datetime.strptime('W12 3', 'W%V %u')\nValueError: ISO week directive '%V' must be used with the ISO year directive '%G' and a weekday directive ('%A', '%a', '%w', or '%u').\n\n# after\n>>> datetime.strptime('2020 W12 3', '%G W%V %u')\ndatetime.datetime(2020, 3, 18, 0, 0)","handlingStrategy":"validation","validationCode":"def need_iso_year(fmt: str) -> bool:\n    clean = fmt.replace('%%', '')\n    return '%V' in clean and '%G' not in clean","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never emit week numbers without a year in the data you control","When ingesting external week data, require the ISO year field in the contract","Centralize ISO-week parsing in one validated helper"],"tags":["strptime","iso-week","datetime","format-string","cpython"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}