{"record":{"id":"38cb2cdc6d3c923e","repo":"python/cpython","slug":"iso-week-directive-v-is-incompatible-with-the-y","errorCode":null,"errorMessage":"ISO week directive '%V' is incompatible with the year directive '%Y'. Use the ISO year '%G' instead.","messagePattern":"ISO week directive '%V' is incompatible with the year directive '%Y'\\. Use the ISO year '%G' instead\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_strptime.py","lineNumber":750,"sourceCode":"\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:\n        if week_of_year is not None:\n            week_starts_Mon = True if week_of_year_start == 0 else False\n            julian = _calc_julian_from_U_or_W(year, week_of_year, weekday,\n                                                week_starts_Mon)","sourceCodeStart":732,"sourceCodeEnd":768,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_strptime.py#L732-L768","documentation":"If %V (ISO week) matched AND a regular year %Y/%y is present AND a weekday is present, all three branches of the ambiguity block are satisfied but mixing calendar year %Y with ISO week %V is wrong: at year boundaries week 1 can belong to the previous/next ISO year. _strptime raises and points to %G.","triggerScenarios":"datetime.strptime('2020 12 3', '%Y %V %u') — calendar year paired with ISO week number; typical when '2020-W12-3' style data is parsed with %Y instead of %G.","commonSituations":"Off-by-one-week bugs around New Year lead developers to ISO weeks; then they keep %Y out of habit. Dates like 2021-01-01 belong to ISO week 53 of ISO year 2020.","solutions":["Replace %Y with %G in the format for ISO week dates","If you truly have calendar year + week number (non-ISO semantics), compute the date manually with timedelta arithmetic from Jan 1 instead of strptime"],"exampleFix":"# before\n>>> datetime.strptime('2020 12 3', '%Y %V %u')\nValueError: ISO week directive '%V' is incompatible with the year directive '%Y'. Use the ISO year '%G' instead.\n\n# after\n>>> datetime.strptime('2020 12 3', '%G %V %u')\ndatetime.datetime(2020, 3, 18, 0, 0)","handlingStrategy":"validation","validationCode":"def fix_year_directive(fmt: str) -> str:\n    clean = fmt.replace('%%', '')\n    if '%V' in clean and '%Y' in clean:\n        return fmt.replace('%Y', '%G')\n    return fmt","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat %G/%V/%u as the only legal trio for ISO week dates","Convert calendar-year + week data to a date yourself with isocalendar()/timedelta","Add doctests around New Year boundaries (e.g. 2021-01-01 is 2020-W53-5)"],"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"}