apache/beam · error · TypeError

Cannot interpret as micros.

Error message

Cannot interpret %s %s as micros.

What it means

When constructing a Timestamp with the `micros` argument, micros must be an int or float. Passing a string (e.g. from config or a parsed token) raises this TypeError. This guard sits inside the `if micros is not None` block of __init__.

Solutions

  1. Convert with int(micros) before constructing
  2. Parse the source value to int at read time (int(json_value))
  3. Validate isinstance(micros, (int, float)) before calling Timestamp

Example fix

// before
Timestamp(micros=row['micros'])  # str from CSV
// after
Timestamp(micros=int(row['micros']))
Defensive patterns

Strategy: type-guard

Validate before calling

if micros is not None and not isinstance(micros, (int, float)):
    micros = int(micros)

Type guard

def is_numeric(v) -> bool:
    return isinstance(v, (int, float)) and not isinstance(v, bool)

Prevention

When it happens

Trigger: Timestamp(micros='1000000') or Timestamp(micros=some_str_from_json); from_utc_datetime passing a non-numeric micros value derived from parsing.

Common situations: Reading micros from CSV/JSON where values stay strings; passing Decimal, which is neither int nor float, from a database driver.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/6eabb55525f5d949. Report an issue: GitHub.

Appendix: source

Thrown at sdks/python/apache_beam/utils/timestamp.py:98

      *,
      micros: Optional[Union[int, float]] = None) -> None:
    if not isinstance(seconds, (int, float)):
      raise TypeError(
          'Cannot interpret %s %s as seconds.' % (seconds, type(seconds)))
    if not isinstance(subseconds, (int, float)):
      raise TypeError(
          'Cannot interpret %s %s as subseconds.' %
          (subseconds, type(subseconds)))
    if not isinstance(precision, int):
      raise TypeError(
          'Cannot interpret %s %s as precision.' % (precision, type(precision)))
    if not 0 <= precision <= Timestamp.NANOS_PRECISION:
      raise ValueError(
          'Timestamp precision must be between 0 and %d (inclusive), '
          'but was %d.' % (Timestamp.NANOS_PRECISION, precision))
    if micros is not None:
      if not isinstance(micros, (int, float)):
        raise TypeError(
            'Cannot interpret %s %s as micros.' % (micros, type(micros)))
      if subseconds:
        raise ValueError(
            'micros and subseconds are mutually exclusive, got micros=%s, '
            'subseconds=%s.' % (micros, subseconds))
      if precision != Timestamp.MICROS_PRECISION:
        raise ValueError(
            'micros implies microsecond precision (6) but precision was %d; '
            'use subseconds instead.' % precision)
      subseconds = micros
    self._precision = precision
    total = int(seconds * _POW_10[precision]) + int(subseconds)
    self._seconds, self._subseconds = divmod(total, _POW_10[precision])

  def _total(self, precision: int) -> int:
    """Returns the total time since the epoch in units of 10**-precision
    seconds.

View on GitHub (pinned to 12126d8942)