{"record":{"id":"b0d290955db3183b","repo":"python/cpython","slug":"fromutc-argument-must-be-a-datetime-instance-or","errorCode":null,"errorMessage":"fromutc() argument must be a datetime instance or None","messagePattern":"fromutc\\(\\) argument must be a datetime instance or None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_pydatetime.py","lineNumber":2540,"sourceCode":"            if self._name is None:\n                return self._name_from_offset(self._offset)\n            return self._name\n        raise TypeError(\"tzname() argument must be a datetime instance\"\n                        \" or None\")\n\n    def dst(self, dt):\n        if isinstance(dt, datetime) or dt is None:\n            return None\n        raise TypeError(\"dst() argument must be a datetime instance\"\n                        \" or None\")\n\n    def fromutc(self, dt):\n        if isinstance(dt, datetime):\n            if dt.tzinfo is not self:\n                raise ValueError(\"fromutc: dt.tzinfo \"\n                                 \"is not self\")\n            return dt + self._offset\n        raise TypeError(\"fromutc() argument must be a datetime instance\"\n                        \" or None\")\n\n    _maxoffset = timedelta(hours=24, microseconds=-1)\n    _minoffset = -_maxoffset\n\n    @staticmethod\n    def _name_from_offset(delta):\n        if not delta:\n            return 'UTC'\n        if delta < timedelta(0):\n            sign = '-'\n            delta = -delta\n        else:\n            sign = '+'\n        hours, rest = divmod(delta, timedelta(hours=1))\n        minutes, rest = divmod(rest, timedelta(minutes=1))\n        seconds = rest.seconds\n        microseconds = rest.microseconds","sourceCodeStart":2522,"sourceCodeEnd":2558,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydatetime.py#L2522-L2558","documentation":"timezone.fromutc(dt) requires its argument to be a datetime instance; None is not accepted here (unlike utcoffset/tzname/dst) because fromutc must perform arithmetic on dt. Strings, dates, or None raise TypeError.","triggerScenarios":"tz.fromutc(None); tz.fromutc('2024-01-01T00:00'); tz.fromutc(date(2024,1,1)); generic converters forwarding untyped values.","commonSituations":"Code paths where a timestamp variable is Optional and None leaks through; parsing pipelines passing raw strings; refactors that changed a datetime parameter into a date.","solutions":["Construct the datetime first: tz.fromutc(datetime.fromisoformat(s))","Guard Optional values: if dt is None: skip or default before calling","Use dt.astimezone(tz) on an aware datetime instead of calling fromutc manually"],"exampleFix":"// before\nlocal = tz.fromutc(ts)  # ts may be None\n\n# after\nif ts is None:\n    local = None\nelse:\n    local = tz.fromutc(ts)","handlingStrategy":"type-guard","validationCode":"from datetime import datetime\n\ndef safe_fromutc(tz, dt: datetime | None):\n    if dt is None:\n        return None\n    if not isinstance(dt, datetime):\n        dt = datetime.fromisoformat(str(dt))\n    return tz.fromutc(dt)","typeGuard":"from datetime import datetime\n\ndef is_datetime(v) -> bool:\n    return isinstance(v, datetime)","tryCatchPattern":null,"preventionTips":["fromutc takes no None — guard Optionals at the call site","Parse strings to datetime before conversion","Use astimezone() for routine UTC→local work"],"tags":["python","datetime","timezone","type-error","utc"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}