{"record":{"id":"4053ac780930017a","repo":"python/cpython","slug":"fold-must-be-either-0-or-1-not-fold","errorCode":null,"errorMessage":"fold must be either 0 or 1, not {fold}","messagePattern":"fold must be either 0 or 1, not (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_pydatetime.py","lineNumber":595,"sourceCode":"    if not 1 <= day <= dim:\n        raise ValueError(f\"day {day} must be in range 1..{dim} for month {month} in year {year}\")\n    return year, month, day\n\ndef _check_time_fields(hour, minute, second, microsecond, fold):\n    hour = _index(hour)\n    minute = _index(minute)\n    second = _index(second)\n    microsecond = _index(microsecond)\n    if not 0 <= hour <= 23:\n        raise ValueError(f\"hour must be in 0..23, not {hour}\")\n    if not 0 <= minute <= 59:\n        raise ValueError(f\"minute must be in 0..59, not {minute}\")\n    if not 0 <= second <= 59:\n        raise ValueError(f\"second must be in 0..59, not {second}\")\n    if not 0 <= microsecond <= 999999:\n        raise ValueError(f\"microsecond must be in 0..999999, not {microsecond}\")\n    if fold not in (0, 1):\n        raise ValueError(f\"fold must be either 0 or 1, not {fold}\")\n    return hour, minute, second, microsecond, fold\n\ndef _check_tzinfo_arg(tz):\n    if tz is not None and not isinstance(tz, tzinfo):\n        raise TypeError(\n            \"tzinfo argument must be None or of a tzinfo subclass, \"\n            f\"not {type(tz).__name__!r}\"\n        )\n\ndef _divide_and_round(a, b):\n    \"\"\"divide a by b and round result to the nearest integer\n\n    When the ratio is exactly half-way between two integers,\n    the even integer is returned.\n    \"\"\"\n    # Based on the reference implementation for divmod_near\n    # in Objects/longobject.c.\n    q, r = divmod(a, b)","sourceCodeStart":577,"sourceCodeEnd":613,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydatetime.py#L577-L613","documentation":"Raised by _check_time_fields() when the fold argument to time()/datetime() is not exactly 0 or 1. fold is PEP 495's disambiguator for repeated wall times during DST fold-back; it is a strict two-valued flag, not a boolean-coerced or arbitrary integer field.","triggerScenarios":"time(1, 30, fold=2); datetime(..., fold=True) works (bool is int 1) but fold=2 fails; passing a fold computed from arithmetic like fold_a + fold_b.","commonSituations":"Serializing/deserializing fold from JSON where it arrives as 0/1 strings or other ints; copying fold between objects with arithmetic; confusing fold with an hour-offset count.","solutions":["Pass only literal 0 or 1 (or a bool) as fold","Normalize on ingest: fold = 1 if fold in ('1', 1, True) else 0","Keep fold out of arithmetic; treat it as a flag you copy verbatim"],"exampleFix":"// before\nt = time(1, 30, fold=int(fold_str) * 2)  # can produce 2\n// after\nt = time(1, 30, fold=1 if fold_str == '1' else 0)","handlingStrategy":"validation","validationCode":"fold = 1 if fold in (1, True, '1') else 0\nt = time(h, m, s, us, fold=fold)","typeGuard":"def valid_fold(f) -> bool:\n    return f in (0, 1)","tryCatchPattern":null,"preventionTips":["Treat fold as a copy-only flag, never do arithmetic on it","Normalize fold from external data to literal 0/1 at the boundary"],"tags":["datetime","valueerror","validation","dst","pep-495"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}