{"record":{"id":"b938826652b1bccc","repo":"python/cpython","slug":"strptime-argument-must-be-str-not","errorCode":null,"errorMessage":"strptime() argument {} must be str, not {}","messagePattern":"strptime\\(\\) argument (.+?) must be str, not (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_strptime.py","lineNumber":543,"sourceCode":"    # the same as that specified by %U or %W).\n    week_0_length = (7 - first_weekday) % 7\n    if week_of_year == 0:\n        return 1 + day_of_week - first_weekday\n    else:\n        days_to_week = week_0_length + (7 * (week_of_year - 1))\n        return 1 + days_to_week + day_of_week\n\n\ndef _strptime(data_string, format=\"%a %b %d %H:%M:%S %Y\"):\n    \"\"\"Return a 3-tuple consisting of a tuple with time components,\n    an int containing the number of microseconds, and an int\n    containing the microseconds part of the GMT offset, based on the\n    input string and the format string.\"\"\"\n\n    for index, arg in enumerate([data_string, format]):\n        if not isinstance(arg, str):\n            msg = \"strptime() argument {} must be str, not {}\"\n            raise TypeError(msg.format(index, type(arg)))\n\n    global _TimeRE_cache, _regex_cache\n    with _cache_lock:\n        locale_time = _TimeRE_cache.locale_time\n        if (_getlang() != locale_time.lang or\n            time.tzname != locale_time.tzname or\n            time.daylight != locale_time.daylight):\n            _TimeRE_cache = TimeRE()\n            _regex_cache.clear()\n            locale_time = _TimeRE_cache.locale_time\n        if len(_regex_cache) > _CACHE_MAX_SIZE:\n            _regex_cache.clear()\n        format_regex = _regex_cache.get(format)\n        if not format_regex:\n            try:\n                format_regex = _TimeRE_cache.compile(format)\n            # KeyError raised when a bad format is found; can be specified as\n            # \\\\, in which case it was a stray % but with a space after it","sourceCodeStart":525,"sourceCodeEnd":561,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_strptime.py#L525-L561","documentation":"_strptime type-checks both of its arguments up front: for index, arg in enumerate([data_string, format]) it requires str. This TypeError is raised when the data string (index 0) or the format (index 1) is not a str — e.g. bytes, int, None. The message reports the argument position and the offending type.","triggerScenarios":"Calling time.strptime() or datetime.datetime.strptime() with bytes (b'2020-01-01'), an int/float timestamp, None, or a non-str format argument.","commonSituations":"Reading data from files/network opened in binary mode and passing bytes directly; passing a datetime.date object instead of its string form; forgetting that strptime (parse) is the inverse of strftime (format).","solutions":["Decode bytes to str before parsing: time.strptime(data.decode('utf-8'), fmt)","If you have a timestamp number, use datetime.fromtimestamp(ts) instead of strptime","If you have a date/datetime object, call .strftime(fmt) — strptime is only for strings"],"exampleFix":"# before\n>>> datetime.strptime(b'2020-01-01', '%Y-%m-%d')\nTypeError: strptime() argument 0 must be str, not <class 'bytes'>\n\n# after\n>>> datetime.strptime(b'2020-01-01'.decode(), '%Y-%m-%d')\ndatetime.datetime(2020, 1, 1, 0, 0)","handlingStrategy":"type-guard","validationCode":"if not isinstance(data_string, str):\n    data_string = data_string.decode('utf-8') if isinstance(data_string, bytes) else str(data_string)\nif not isinstance(fmt, str):\n    raise TypeError('fmt must be str')","typeGuard":"def is_strptime_args(*args) -> bool:\n    return all(isinstance(a, str) for a in args)","tryCatchPattern":"try:\n    dt = datetime.strptime(s, fmt)\nexcept TypeError:\n    dt = datetime.strptime(s.decode() if isinstance(s, bytes) else str(s), fmt)","preventionTips":["Open data files in text mode ('r') not binary ('rb') when the contents feed strptime","Use datetime.fromtimestamp() for numeric timestamps instead of strptime","Annotate parser entry points as (s: str, fmt: str) and run mypy"],"tags":["strptime","typeerror","bytes","type-check","cpython"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}