{"record":{"id":"0e33479fcd85fff2","repo":"apache/beam","slug":"dt-has-no-timezone-info-https-docs-python-org-3-library","errorCode":null,"errorMessage":"dt has no timezone info (https://docs.python.org/3/library/datetime.html#aware-and-naive-objects): %s","messagePattern":"dt has no timezone info \\(https://docs\\.python\\.org/3/library/datetime\\.html#aware-and-naive-objects\\): (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/utils/timestamp.py","lineNumber":162,"sourceCode":"          'Cannot interpret %s %s as Timestamp.' % (seconds, type(seconds)))\n\n  @staticmethod\n  def now() -> 'Timestamp':\n    return Timestamp(seconds=time.time())\n\n  @staticmethod\n  def _epoch_datetime_utc() -> datetime.datetime:\n    return datetime.datetime.fromtimestamp(0, pytz.utc)\n\n  @classmethod\n  def from_utc_datetime(cls, dt: datetime.datetime) -> 'Timestamp':\n    \"\"\"Create a ``Timestamp`` instance from a ``datetime.datetime`` object.\n\n    Args:\n      dt: A ``datetime.datetime`` object in UTC (offset-aware).\n    \"\"\"\n    if dt.tzinfo is None:\n      raise ValueError(\n          \"dt has no timezone info \" +\n          \"(https://docs.python.org/3/library/datetime.html\" +\n          \"#aware-and-naive-objects): %s\" % dt)\n    if dt.tzinfo != pytz.utc and dt.tzinfo != datetime.timezone.utc:\n      raise ValueError('dt not in UTC: %s' % dt)\n    duration = dt - cls._epoch_datetime_utc()\n    # Avoid total_seconds(): its float result can be off by a microsecond.\n    return Timestamp(\n        seconds=duration.days * 86400 + duration.seconds,\n        micros=duration.microseconds)\n\n  @classmethod\n  def from_rfc3339(cls, rfc3339: str) -> 'Timestamp':\n    \"\"\"Create a ``Timestamp`` instance from an RFC 3339 compliant string.\n\n    Fractional seconds up to microseconds produce a microsecond-precision\n    Timestamp; a longer fraction (up to nanoseconds) produces a Timestamp\n    whose precision matches the number of fractional digits.","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/utils/timestamp.py#L144-L180","documentation":"from_utc_datetime() requires an offset-aware datetime in UTC. A naive datetime (tzinfo is None) cannot be unambiguously converted to an absolute time, so a ValueError is raised with a link to Python's aware/naive documentation.","triggerScenarios":"Timestamp.from_utc_datetime(datetime.utcnow()) — utcnow() returns naive; Timestamp.from_utc_datetime(datetime.fromisoformat(s)) where s has no offset; parsing user timestamps without a timezone suffix.","commonSituations":"Using deprecated datetime.utcnow(); log lines like '2024-01-01 00:00:00' with no Z/offset; DB columns of type TIMESTAMP (naive) rather than TIMESTAMPTZ.","solutions":["Attach UTC explicitly: dt.replace(tzinfo=datetime.timezone.utc) for known-UTC naive values","Use datetime.now(datetime.timezone.utc) instead of utcnow()","Parse with an offset: dateutil.parser.isoparse('...Z') then pass the aware result"],"exampleFix":"// before\nts = Timestamp.from_utc_datetime(datetime.utcnow())\n// after\nts = Timestamp.from_utc_datetime(datetime.now(datetime.timezone.utc))","handlingStrategy":"validation","validationCode":"if dt.tzinfo is None:\n    dt = dt.replace(tzinfo=datetime.timezone.utc)  # only if known UTC","typeGuard":"def is_aware(dt: datetime.datetime) -> bool:\n    return dt.tzinfo is not None and dt.tzinfo.utcoffset(dt) is not None","tryCatchPattern":"try:\n    ts = Timestamp.from_utc_datetime(dt)\nexcept ValueError as e:\n    if 'no timezone info' in str(e):\n        ts = Timestamp.from_utc_datetime(dt.replace(tzinfo=datetime.timezone.utc))\n    else:\n        raise","preventionTips":["Use datetime.now(timezone.utc), never utcnow()","Prefer ISO strings with explicit Z or offset suffix","Use TIMESTAMPTZ columns; attach tzinfo right after reading naive ones"],"tags":["python","value-error","datetime","timezone"],"backgroundTag":"invalid-date-format","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}