python/cpython · error · TypeError

time argument must be a time instance

Error message

time argument must be a time instance

What it means

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.

Source

Thrown at Lib/_pydatetime.py:1946

    def utcnow(cls):
        "Construct a UTC datetime from time.time()."
        import warnings
        warnings.warn("datetime.datetime.utcnow() is deprecated and scheduled for "
                      "removal in a future version. Use timezone-aware "
                      "objects to represent datetimes in UTC: "
                      "datetime.datetime.now(datetime.UTC).",
                      DeprecationWarning,
                      stacklevel=2)
        t = _time.time()
        return cls._fromtimestamp(t, True, None)

    @classmethod
    def combine(cls, date, time, tzinfo=True):
        "Construct a datetime from a given date and a given time."
        if not isinstance(date, _date_class):
            raise TypeError("date argument must be a date instance")
        if not isinstance(time, _time_class):
            raise TypeError("time argument must be a time instance")
        if tzinfo is True:
            tzinfo = time.tzinfo
        return cls(date.year, date.month, date.day,
                   time.hour, time.minute, time.second, time.microsecond,
                   tzinfo, fold=time.fold)

    @classmethod
    def fromisoformat(cls, date_string):
        """Construct a datetime from a string in one of the ISO 8601 formats."""
        if not isinstance(date_string, str):
            raise TypeError('fromisoformat: argument must be str')

        if len(date_string) < 7:
            raise ValueError(f'Invalid isoformat string: {date_string!r}')

        # Split this at the separator
        try:
            separator_location = _find_isoformat_datetime_separator(date_string)

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Parse the time: datetime.combine(d, time.fromisoformat('09:00'))
  2. If the source is a datetime, pass its .time(): datetime.combine(d, other.time())
  3. For string pairs, build directly: datetime.strptime(f'{d} {s}', '%Y-%m-%d %H:%M')

Example fix

# before
dt = datetime.combine(d, '09:00')

# after
from datetime import time
dt = datetime.combine(d, time.fromisoformat('09:00'))
Defensive patterns

Strategy: type-guard

Validate before calling

from datetime import time as _time
if not isinstance(t, _time):
    if isinstance(t, str):
        t = _time.fromisoformat(t)
    elif isinstance(t, _datetime):
        t = t.time()
    else:
        raise TypeError('cannot use as time part')
dt = datetime.combine(d, t)

Type guard

from datetime import time as _time

def is_time_instance(v) -> bool:
    return isinstance(v, _time)

Try / catch

try:
    dt = datetime.combine(d, t)
except TypeError:
    dt = datetime.combine(d, _time.fromisoformat(t))  # when t was a str

Prevention

When it happens

Trigger: 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().

Common situations: 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.

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/081934183f562bd1. Report an issue: GitHub.