Skip to content

feat: Added time constraint to the DatetimeTag #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion mininterface/tag/datetime_tag.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from dataclasses import dataclass
from datetime import date, datetime, time
from typing import Union
from .tag import Tag, TagValue, UiValue


@dataclass(repr=False)
class DatetimeTag(Tag[TagValue | date | time | datetime]):
class DatetimeTag(Tag[Union[TagValue, date, time, datetime]]):
"""
Datetime, date and time types are supported.

Expand Down Expand Up @@ -68,6 +69,12 @@ class Env:
full_precision: bool = False
""" Include full time precison, seconds, microseconds. """

before: Union[datetime, None] = None
""" The maximum allowed date/datetime value. """

after: Union[datetime, None] = None
""" The minimum allowed date/datetime value. """

# NOTE calling DatetimeTag("2025-02") should convert str to date?
def __post_init__(self):
super().__post_init__()
Expand All @@ -93,6 +100,27 @@ def __post_init__(self):
# m.ask(f"My date", DatetimeTag(date=True))
self.val = self._make_default_value()

# Add validation for before/after constraints
if self.before is not None or self.after is not None:
def validate_date_range(tag: Tag) -> Union[bool, str]:
val = tag.val

if self.before is not None:
if isinstance(val, datetime) and isinstance(self.before, date):
val = val.date()
if val > self.before:
return f"Date must be before {self.before}"

if self.after is not None:
if isinstance(val, datetime) and isinstance(self.after, date):
val = val.date()
if val < self.after:
return f"Date must be after {self.after}"

return True

self._add_validation(validate_date_range)

def __hash__(self): # every Tag child must have its own hash method to be used in Annotated
return super().__hash__()

Expand Down