{"record":{"id":"081934183f562bd1","repo":"python/cpython","slug":"time-argument-must-be-a-time-instance","errorCode":null,"errorMessage":"time argument must be a time instance","messagePattern":"time argument must be a time instance","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_pydatetime.py","lineNumber":1946,"sourceCode":"    def utcnow(cls):\n        \"Construct a UTC datetime from time.time().\"\n        import warnings\n        warnings.warn(\"datetime.datetime.utcnow() is deprecated and scheduled for \"\n                      \"removal in a future version. Use timezone-aware \"\n                      \"objects to represent datetimes in UTC: \"\n                      \"datetime.datetime.now(datetime.UTC).\",\n                      DeprecationWarning,\n                      stacklevel=2)\n        t = _time.time()\n        return cls._fromtimestamp(t, True, None)\n\n    @classmethod\n    def combine(cls, date, time, tzinfo=True):\n        \"Construct a datetime from a given date and a given time.\"\n        if not isinstance(date, _date_class):\n            raise TypeError(\"date argument must be a date instance\")\n        if not isinstance(time, _time_class):\n            raise TypeError(\"time argument must be a time instance\")\n        if tzinfo is True:\n            tzinfo = time.tzinfo\n        return cls(date.year, date.month, date.day,\n                   time.hour, time.minute, time.second, time.microsecond,\n                   tzinfo, fold=time.fold)\n\n    @classmethod\n    def fromisoformat(cls, date_string):\n        \"\"\"Construct a datetime from a string in one of the ISO 8601 formats.\"\"\"\n        if not isinstance(date_string, str):\n            raise TypeError('fromisoformat: argument must be str')\n\n        if len(date_string) < 7:\n            raise ValueError(f'Invalid isoformat string: {date_string!r}')\n\n        # Split this at the separator\n        try:\n            separator_location = _find_isoformat_datetime_separator(date_string)","sourceCodeStart":1928,"sourceCodeEnd":1964,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydatetime.py#L1928-L1964","documentation":"Raised by datetime.combine() when the second argument is not a time instance. The clock fields (hour, minute, second, microsecond, fold) are read from the time parameter, which is isinstance-checked against _time_class; strings like '09:00', timedeltas, or None for the time part all fail with this TypeError.","triggerScenarios":"datetime.combine(d, '09:00'); datetime.combine(d, timedelta(hours=9)); datetime.combine(d, None); passing a struct_time or a datetime where only its time component was intended is fine only if a real time is extracted with .time().","commonSituations":"Web/API time inputs kept as strings; reusing a parsed datetime's sibling value instead of calling .time(); scheduled-job configs where the run time comes from YAML as a string.","solutions":["Parse the time: datetime.combine(d, time.fromisoformat('09:00'))","If the source is a datetime, pass its .time(): datetime.combine(d, other.time())","For string pairs, build directly: datetime.strptime(f'{d} {s}', '%Y-%m-%d %H:%M')"],"exampleFix":"# before\ndt = datetime.combine(d, '09:00')\n\n# after\nfrom datetime import time\ndt = datetime.combine(d, time.fromisoformat('09:00'))","handlingStrategy":"type-guard","validationCode":"from datetime import time as _time\nif not isinstance(t, _time):\n    if isinstance(t, str):\n        t = _time.fromisoformat(t)\n    elif isinstance(t, _datetime):\n        t = t.time()\n    else:\n        raise TypeError('cannot use as time part')\ndt = datetime.combine(d, t)","typeGuard":"from datetime import time as _time\n\ndef is_time_instance(v) -> bool:\n    return isinstance(v, _time)","tryCatchPattern":"try:\n    dt = datetime.combine(d, t)\nexcept TypeError:\n    dt = datetime.combine(d, _time.fromisoformat(t))  # when t was a str","preventionTips":["Convert config-file time strings with time.fromisoformat before combine","When sourcing from another datetime, pass .time() explicitly","Type-annotate helper signatures (date, time) so mypy catches misuse"],"tags":["datetime","combine","typeerror","parsing"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}