Skip to content

Can Record date fields support datetime.date? #6

@timj

Description

@timj

I am using a Pydantic model describing my dataset and then creating a Record instance from that model. The Record model declares that the types are all meant to be datetime.date and that's what my model uses. This works in python 3.12 but now in python 3.13 I get:

    @field_validator("publication_date", mode='before')
    @classmethod
    def parse_date(cls, value) -> date:
        """
        Define several acceptable date-string formats to parse.
        """
        format_strings=["%m/%d/%Y", "%m/%d/%y", "%Y-%m-%d"]
    
        # attempt each format to attempt parsing dates
        for format in format_strings:
            try:
>               return datetime.strptime(value, format).date()
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E               TypeError: strptime() argument 1 must be str, not datetime.date

I imagine that in python 3.12 strptime was stringifying the date and then it was working but now it's more correct.

Would it be possible for your validator to be updated to add

if isinstance(value, datetime.date):
    return value

since it seems strange that both models should use date but I have to force back to string to construct the Record.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions