{"record":{"id":"906abe8cbd93a71e","repo":"RustPython/RustPython","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":1910,"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":1892,"sourceCodeEnd":1928,"githubUrl":"https://github.com/RustPython/RustPython/blob/aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd/Lib/_pydatetime.py#L1892-L1928","documentation":"The second half of datetime.combine's contract: the time argument must be a time instance (a datetime does not qualify — pass dt.time()). Passing a string like '09:00', a tuple, or None raises TypeError naming the time argument, since combine reads .hour/.minute/.second/.microsecond/.fold directly.","triggerScenarios":"datetime.combine(date.today(), '09:00'); passing (9, 30) tuples from config; passing a datetime where a time is expected (use dt.time()); a None default leaking through from optional config.","commonSituations":"Alarm/scheduling config holding human-readable strings; row tuples from databases mapped positionally; refactors that changed a variable from time to str.","solutions":["Parse first: datetime.combine(d, time.fromisoformat('09:00')) or time(9, 0).","When the source value is a datetime, pass value.time() to combine.","Type-check the second argument (isinstance(t, time)) before calling.","Validate config schema so time fields arrive as time objects or known strings."],"exampleFix":"// before\ndt = datetime.combine(day, cfg['alarm'])  # cfg['alarm'] == '09:00'\n\n// after\nfrom datetime import time\nalarm = time.fromisoformat(cfg['alarm']) if isinstance(cfg['alarm'], str) else cfg['alarm']\ndt = datetime.combine(day, alarm)","handlingStrategy":"type-guard","validationCode":"from datetime import time\n\ndef coerce_time_arg(t):\n    if isinstance(t, str):\n        return time.fromisoformat(t)\n    if not isinstance(t, time):\n        raise TypeError(f'time argument must be a time instance, got {type(t).__name__}')\n    return t","typeGuard":"from datetime import datetime, time\n\ndef is_time_instance(v) -> bool:\n    return isinstance(v, time) and not isinstance(v, datetime)","tryCatchPattern":"try:\n    dt = datetime.combine(d, t)\nexcept TypeError as e:\n    if 'time argument' in str(e):\n        dt = datetime.combine(d, t.time() if isinstance(t, datetime) else time.fromisoformat(t))\n    else:\n        raise","preventionTips":["Parse '09:00'-style strings with time.fromisoformat first.","Pass dt.time() when the source value is a datetime.","Validate time-typed config fields in the schema."],"tags":["datetime","combine","time","typeerror","argument-type"],"backgroundTag":"datetime-combine-argument-type","analyzedSha":"aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd","analyzedAt":"2026-08-17T00:37:52.100Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}