Skip to content

email.headerregistry.DateHeader's datetime property can be None #14787

@aclemons

Description

@aclemons

If the header value is malformed / cannot be parsed, the datetime property is None.

def datetime(self) -> _datetime: ...

For example:

$ python3
Python 3.12.11 (main, Jun  6 2025, 10:48:48) [GCC 14.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from email import message_from_bytes
>>> import email.policy
>>> h = message_from_bytes(b"Date: malformed\r\n", policy=email.policy.default)['date']
>>> type(h)
<class 'email.headerregistry._UniqueDateHeader'>
>>> type(h.datetime)
<class 'NoneType'>
>>> from email import utils
>>> from datetime import datetime, UTC
>>> h = message_from_bytes(f"Date: {utils.format_datetime(datetime.now(tz=UTC))}\r\n".encode("us-ascii"), policy=email.policy.default)['date']
>>> type(h)
<class 'email.headerregistry._UniqueDateHeader'>
>>> h.datetime
datetime.datetime(2025, 9, 26, 0, 43, 53, tzinfo=datetime.timezone.utc)

Metadata

Metadata

Assignees

No one assigned

    Labels

    stubs: false negativeType checkers do not report an error, but should

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions