Skip to content

cattrs.preconf.tomlkit doesn't support TOML date type #707

Closed

Description

@RazerM
from datetime import date, datetime

import tomlkit
from attrs import define
from cattrs.preconf.tomlkit import make_converter


@define
class Event:
    event_date: date
    event_datetime: datetime


TOML_INPUT = """\
event_date = 2025-12-16
event_datetime = 2025-12-16T20:00:00
"""


def main() -> None:
    converter = make_converter()

    parsed = tomlkit.loads(TOML_INPUT)
    print(converter.structure(parsed, Event))


if __name__ == "__main__":
    main()
  + Exception Group Traceback (most recent call last):
  |   File "cattrs_tomlkit_issue.py", line 28, in <module>
  |     main()
  |   File "cattrs_tomlkit_issue.py", line 24, in main
  |     converter.structure(parsed, Event)
  |   File ".venv/lib/python3.12/site-packages/cattrs/converters.py", line 589, in structure
  |     return self._structure_func.dispatch(cl)(obj, cl)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "<cattrs generated structure __main__.Event>", line 14, in structure_Event
  | cattrs.errors.ClassValidationError: While structuring Event (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "<cattrs generated structure __main__.Event>", line 5, in structure_Event
    |   File ".venv/lib/python3.12/site-packages/cattrs/preconf/tomlkit.py", line 73, in <lambda>
    |     converter.register_structure_hook(date, lambda v, _: date.fromisoformat(v))
    |                                                          ^^^^^^^^^^^^^^^^^^^^^
    | TypeError: fromisoformat: argument must be str
    | Structuring class Event @ attribute event_date
    +------------------------------------

Activity

  1. Tinche commented on Dec 16, 2025

    @Tinche
    Member

    Ooph, got it. Let me see about fixing this.

  2. linked a pull request that will close this issuetomlkit: handle native date objects #708on Dec 16, 2025
  3. Tinche commented on Dec 16, 2025

    @Tinche
    Member

    I think I got this in #708

    In the mean time, you can work around it by overriding the hook:

    from datetime import date
    
    converter.register_structure_hook(
        date, lambda v, _: v if isinstance(v, date) else date.fromisoformat(v)
    )
  4. added a commit that references this issue on Feb 22, 2026
  5. added a commit that references this issue on May 14, 2026
  6. added a commit that references this issue on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions