From f5ebc71784a789d4024b5c2670920f4935f0af66 Mon Sep 17 00:00:00 2001 From: Gabor Boros Date: Mon, 16 Nov 2026 18:23:07 +0100 Subject: [PATCH] fix: address AST date vs datetime parsing issues Signed-off-by: Gabor Boros --- rethinkdb/ast.py | 51 +++++++++++++++---------------------- tests/test_date_and_time.py | 29 +++++++++++++++++++++ 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/rethinkdb/ast.py b/rethinkdb/ast.py index 380aa21..ed12435 100644 --- a/rethinkdb/ast.py +++ b/rethinkdb/ast.py @@ -2411,7 +2411,10 @@ def needs_wrap(arg): # pylint: disable=too-many-return-statements def expr( - val: str + val: bool + | int + | float + | str | bytes | RqlQuery | RqlBinary @@ -2419,16 +2422,14 @@ def expr( | datetime.datetime | Mapping | Iterable - | Callable, + | Callable + | None, nesting_depth: int = 20, ): """ Convert a Python primitive into a Reql primitive value. """ - if not isinstance(nesting_depth, int): - raise ReqlDriverCompileError("Second argument to `r.expr` must be a number.") - if nesting_depth <= 0: raise ReqlDriverCompileError("Nesting depth limit exceeded.") @@ -2438,7 +2439,7 @@ def expr( if callable(val): return Func(val) - if isinstance(val, str): # TODO: Default is to return Datum - Remove? + if isinstance(val, str): return Datum(val) if isinstance(val, (bytes, RqlBinary)): @@ -2450,31 +2451,21 @@ def expr( if isinstance(val, abc.Iterable): return MakeArray(*[expr(v, nesting_depth - 1) for v in val]) # type: ignore - if isinstance(val, datetime.date): - if isinstance(val, datetime.datetime): - if val.tzinfo is None: - raise ReqlDriverCompileError( - f""" - Cannot convert {type(val).__name__} to Reql time object - without timezone information. You can add timezone information with - the third party module \"pytz\" or by constructing Reql compatible - timezone values with r.make_timezone(\"[+-]HH:MM\"). Alternatively, - use one of Reql's builtin time constructors, r.now, r.time, - or r.iso8601. - """ - ) - return ISO8601(val.isoformat()) + # datetime.datetime subclasses datetime.date, so it must be checked first. + if isinstance(val, datetime.datetime): + if val.tzinfo is None: + raise ReqlDriverCompileError( + f"Cannot convert {type(val).__name__} to Reql time object " + "without timezone information. You can add timezone information with " + 'the third party module "pytz" or by constructing Reql compatible ' + 'timezone values with r.make_timezone("[+-]HH:MM"). Alternatively, ' + "use one of Reql's builtin time constructors, r.now, r.time, " + "or r.iso8601." + ) + return ISO8601(val.isoformat()) - raise ReqlDriverCompileError( - f""" - Cannot convert {type(val).__name__} to Reql time object - without timezone information. You can add timezone information with - the third party module \"pytz\" or by constructing Reql compatible - timezone values with r.make_timezone(\"[+-]HH:MM\"). Alternatively, - use one of Reql's builtin time constructors, r.now, r.time, - or r.iso8601. - """ - ) + if isinstance(val, datetime.date): + return ISO8601(val.isoformat()) return Datum(val) diff --git a/tests/test_date_and_time.py b/tests/test_date_and_time.py index df02ea0..e70a807 100644 --- a/tests/test_date_and_time.py +++ b/tests/test_date_and_time.py @@ -12,9 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. +from datetime import UTC, date, datetime + import pytest from rethinkdb import ast, r +from rethinkdb.errors import ReqlDriverCompileError @pytest.mark.unit @@ -25,3 +28,29 @@ def setup_method(self): def test_get_now(self): now = r.now() assert type(now) == ast.Now + + +@pytest.mark.unit +def test_expr_date_becomes_iso8601(): + """A calendar date is a date-only ISO 8601 value, midnight UTC in ReQL.""" + result = ast.expr(date(2016, 1, 1)) + + assert isinstance(result, ast.ISO8601) + assert result._args[0].data == "2016-01-01" + + +@pytest.mark.unit +def test_expr_aware_datetime_becomes_iso8601(): + """An aware datetime keeps its offset in the ISO 8601 string.""" + value = datetime(2016, 1, 1, 12, 30, tzinfo=UTC) + result = ast.expr(value) + + assert isinstance(result, ast.ISO8601) + assert result._args[0].data == value.isoformat() + + +@pytest.mark.unit +def test_expr_naive_datetime_requires_timezone(): + """A naive datetime has no offset, so it cannot become a ReQL time.""" + with pytest.raises(ReqlDriverCompileError, match="without timezone information"): + ast.expr(datetime(2016, 1, 1, 12, 30))