Skip to content

Allow date, datetime, time for ge, gt, le, lt #482

Open

Description

@alexei

Description

Currently the ge, gt, le, lt constraints are limited to ints and floats, but dates, datetimes and times are comparable as well:

now = datetime.now(timezone.utc)
yesterday = now - timedelta(days=1)
tomorrow = now + timedelta(days=1)
assert yesterday <= now <= tomorrow  # True

This would enable constraints like:

class Event(Struct):
    from_date: Annotated[datetime, msgspec.Meta(gt=...)]
    to_date: Annotated[datetime, msgspec.Meta(gt=...)]

Activity

  1. jcrist commented on Jul 13, 2023

    @jcrist
    Member

    Thanks for opening this, this is definitely a feature that makes sense to add. Tossing it on the todo list.

  2. alexei commented on Jul 13, 2023

    @alexei
    ContributorAuthor

    Out of curiosity, do these constraints need to be implemented for each type? I was looking over the list of supported types and realized decimals, strings and UUIDs can be compared as well, and just about anything that implements the comparison magic methods.

  3. jcrist commented on Jul 13, 2023

    @jcrist
    Member

    Currently yes. For ints/floats a type-specific implementation can be much more efficient than a generic one.

    For more complicated types like uuids/decimals/... there may be negligible benefits for implementing them in native code; we might be better served by using a generic implementation here.

  4. FHU-yezi commented on Jan 22, 2025

    @FHU-yezi

    Wonder this feaature become true.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions