Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

chore(ruff): parse date-only values as dates (#10999)

Shahmir Varqhab962456d28f7
diff --git a/marimo/_ai/text_to_notebook.py b/marimo/_ai/text_to_notebook.py
index 8fa49235ae..e2b394897b 100644
--- a/marimo/_ai/text_to_notebook.py
+++ b/marimo/_ai/text_to_notebook.py
@@ -22,7 +22,7 @@ Before using marimo's Text-To-Notebook AI feature, you should know:
5. You can revoke consent at any time by modifying ~/.marimo/state.toml
"""
-TERMS_LAST_UPDATED = datetime.datetime(2025, 4, 1)
+TERMS_LAST_UPDATED = datetime.date(2025, 4, 1)
def text_to_notebook(prompt: str) -> str:
@@ -96,7 +96,5 @@ def _should_show_terms(last_accepted_at: str | None) -> bool:
"""
if not last_accepted_at:
return True
- last_accepted_date = datetime.datetime.strptime(
- last_accepted_at, "%Y-%m-%d"
- )
+ last_accepted_date = datetime.date.fromisoformat(last_accepted_at)
return last_accepted_date < TERMS_LAST_UPDATED
diff --git a/marimo/_cli/upgrade.py b/marimo/_cli/upgrade.py
index 6c69647993..53d1e44c45 100644
--- a/marimo/_cli/upgrade.py
+++ b/marimo/_cli/upgrade.py
@@ -4,7 +4,7 @@ from __future__ import annotations
import json
import os
import urllib.error
-from datetime import datetime
+from datetime import date, datetime
from typing import TYPE_CHECKING, Any, cast
from marimo import _loggers
@@ -105,10 +105,8 @@ def _update_with_latest_version(state: MarimoCLIState) -> MarimoCLIState:
# We only update the state once a day
now = datetime.now()
if state.last_checked_at:
- last_checked_date = datetime.strptime(
- state.last_checked_at, DATE_FORMAT
- )
- if _is_same_day(last_checked_date, now):
+ last_checked_date = date.fromisoformat(state.last_checked_at)
+ if last_checked_date == now.date():
# Same day, so do nothing
return state
@@ -147,10 +145,6 @@ def _fetch_data_from_url(url: str) -> dict[str, Any]:
raise
-def _is_same_day(date1: datetime, date2: datetime) -> bool:
- return date1.date() == date2.date()
-
-
def update_notices(response: dict[str, Any]) -> list[str]:
"""
Extract notices from the version endpoint response.
diff --git a/marimo/_plugins/ui/_impl/dates.py b/marimo/_plugins/ui/_impl/dates.py
index 0cdcff19ff..95eb4c18ec 100644
--- a/marimo/_plugins/ui/_impl/dates.py
+++ b/marimo/_plugins/ui/_impl/dates.py
@@ -158,7 +158,10 @@ class date(UIElement[str, dt.date]):
return value
if isinstance(value, dt.datetime):
return value.date()
- return dt.datetime.strptime(value, self.DATE_FORMAT).date()
+ # This is date-only input; preserve `strptime`'s accepted formats.
+ return dt.datetime.strptime( # noqa: DTZ007
+ value, self.DATE_FORMAT
+ ).date()
@property
def start(self) -> dt.date:
@@ -314,7 +317,9 @@ class datetime(UIElement[str | None, dt.datetime | None]):
]
for fmt in POSSIBLE_FORMATS:
try:
- return dt.datetime.strptime(value, fmt)
+ # The browser's datetime-local input intentionally represents
+ # wall-clock time without a timezone.
+ return dt.datetime.strptime(value, fmt) # noqa: DTZ007
except ValueError:
pass
raise ValueError(f"Invalid datetime format: {value}")
@@ -471,7 +476,10 @@ class date_range(UIElement[tuple[str, str], tuple[dt.date, dt.date]]):
return value
if isinstance(value, dt.datetime):
return value.date()
- return dt.datetime.strptime(value, self.DATEFORMAT).date()
+ # This is date-only input; preserve `strptime`'s accepted formats.
+ return dt.datetime.strptime( # noqa: DTZ007
+ value, self.DATEFORMAT
+ ).date()
@property
def start(self) -> dt.date:
diff --git a/pyproject.toml b/pyproject.toml
index 5b2093f06d..c0878a6c48 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -371,7 +371,6 @@ ignore = [
"S102", # Use of `exec`
"DTZ005", # `datetime.now()` called without `tzinfo`
"DTZ006", # `datetime.fromtimestamp()` called without `tzinfo`
- "DTZ007", # `datetime.strptime()` called without zone
"DTZ011", # `date.today()` used
"UP007", # Use `X | Y` for type annotations
"DTZ012", # `date.fromtimestamp()` called without `tzinfo`
diff --git a/tests/_plugins/ui/_impl/test_dates.py b/tests/_plugins/ui/_impl/test_dates.py
index 1e329f602d..66967dcf14 100644
--- a/tests/_plugins/ui/_impl/test_dates.py
+++ b/tests/_plugins/ui/_impl/test_dates.py
@@ -23,6 +23,9 @@ def test_date() -> None:
date = ui.date(value="2024-01-01")
assert date.value == datetime.date(2024, 1, 1)
+ date = ui.date(value="2024-1-2")
+ assert date.value == datetime.date(2024, 1, 2)
+
date = ui.date(value="2024-01-01")
date._update("2024-01-02")
assert date.value == datetime.date(2024, 1, 2)
@@ -109,6 +112,9 @@ def test_date_range() -> None:
dr = ui.date_range(value=("2024-01-01", "2024-01-31"))
assert dr.value == (datetime.date(2024, 1, 1), datetime.date(2024, 1, 31))
+ dr = ui.date_range(value=("2024-1-2", "2024-1-3"))
+ assert dr.value == (datetime.date(2024, 1, 2), datetime.date(2024, 1, 3))
+
# Test updating the value
dr._update(("2024-02-01", "2024-02-29"))
assert dr.value == (datetime.date(2024, 2, 1), datetime.date(2024, 2, 29))