{"record":{"id":"c4ae787a98c38588","repo":"python/cpython","slug":"dst-argument-must-be-a-datetime-instance-or-none","errorCode":null,"errorMessage":"dst() argument must be a datetime instance or None","messagePattern":"dst\\(\\) argument must be a datetime instance or None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_pydatetime.py","lineNumber":2531,"sourceCode":"\n    def utcoffset(self, dt):\n        if isinstance(dt, datetime) or dt is None:\n            return self._offset\n        raise TypeError(\"utcoffset() argument must be a datetime instance\"\n                        \" or None\")\n\n    def tzname(self, dt):\n        if isinstance(dt, datetime) or dt is None:\n            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'","sourceCodeStart":2513,"sourceCodeEnd":2549,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydatetime.py#L2513-L2549","documentation":"timezone.dst(dt) always returns None (fixed offsets have no daylight-saving component), but the dt argument must still be a datetime instance or None to satisfy the tzinfo protocol. Anything else raises TypeError before the constant is returned.","triggerScenarios":"tz.dst(date.today()); tz.dst(0); generic DST-checking utilities forwarding non-datetime keys. Frequently hit by code that iterates objects and calls dst() on each without type discipline.","commonSituations":"DST detection helpers applied over mixed collections; passing timestamps as ints/strings; ported Java Joda-style code assuming per-instant objects.","solutions":["Call with a datetime or None: tz.dst(None) returns None for fixed zones","If checking a specific moment, use dt.dst() on the aware datetime itself","Add isinstance guards in generic utilities that invoke tzinfo methods"],"exampleFix":"// before\nin_dst = tz.dst(event_date) is not None  # event_date is date\n\n# after\nin_dst = tz.dst(datetime.combine(event_date, time())) is not None  # False for fixed zones","handlingStrategy":"type-guard","validationCode":"from datetime import datetime\n\ndef in_dst(tz, dt) -> bool:\n    probe = dt if isinstance(dt, datetime) or dt is None else None\n    return tz.dst(probe) is not None","typeGuard":"from datetime import datetime\n\ndef is_datetime_or_none(v) -> bool:\n    return v is None or isinstance(v, datetime)","tryCatchPattern":null,"preventionTips":["Use dt.dst() on the aware datetime itself","Remember fixed-offset zones never have DST","Guard types in utilities that iterate mixed collections"],"tags":["python","datetime","timezone","type-error","api-protocol"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}