python/cpython · error · TypeError

'NoneType' object cannot be interpreted as an integer

Error message

'NoneType' object cannot be interpreted as an integer

What it means

Raised by date.fromtimestamp() when called with t=None. The pure-Python implementation guards explicitly against None before calling time.localtime(), because localtime(None) would otherwise behave inconsistently (in some builds None is treated as the current time, in others it errors). Passing None is treated as a programming error, not as 'now'.

Source

Thrown at Lib/_pydatetime.py:1029

            self = object.__new__(cls)
            self.__setstate(year)
            self._hashcode = -1
            return self
        year, month, day = _check_date_fields(year, month, day)
        self = object.__new__(cls)
        self._year = year
        self._month = month
        self._day = day
        self._hashcode = -1
        return self

    # Additional constructors

    @classmethod
    def fromtimestamp(cls, t):
        "Construct a date from a POSIX timestamp (like time.time())."
        if t is None:
            raise TypeError("'NoneType' object cannot be interpreted as an integer")
        y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(t)
        return cls(y, m, d)

    @classmethod
    def today(cls):
        "Construct a date from time.time()."
        t = _time.time()
        return cls.fromtimestamp(t)

    @classmethod
    def fromordinal(cls, n):
        """Construct a date from a proleptic Gregorian ordinal.

        January 1 of year 1 is day 1.  Only the year, month and day are
        non-zero in the result.
        """
        y, m, d = _ord2ymd(n)
        return cls(y, m, d)

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Default to the current time explicitly: date.fromtimestamp(t if t is not None else time.time())
  2. Handle NULL/None at the data boundary before calling fromtimestamp
  3. Use date.today() when 'now' is the intent

Example fix

# before
d = date.fromtimestamp(row['ts'])  # ts is None for NULL
# after
d = date.fromtimestamp(row['ts']) if row['ts'] is not None else date.today()
Defensive patterns

Strategy: validation

Validate before calling

if t is None:
    raise TypeError('timestamp required, got None')
d = date.fromtimestamp(t)

Type guard

def valid_timestamp(t) -> bool:
    return isinstance(t, (int, float))

Prevention

When it happens

Trigger: date.fromtimestamp(None); threading a value that defaults to None (e.g. date.fromtimestamp(os.environ.get('TS'))) into the API; calling fromtimestamp(row['ts']) where the column is NULL.

Common situations: Optional database timestamps that are NULL; function parameters defaulting to None then forwarded to fromtimestamp; migration from time.localtime(None)-tolerant code.

Related errors


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