Skip to content
Merged
Show file tree
Hide file tree
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
51 changes: 21 additions & 30 deletions rethinkdb/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2411,24 +2411,25 @@ def needs_wrap(arg):

# pylint: disable=too-many-return-statements
def expr(
val: str
val: bool
| int
| float
| str
| bytes
| RqlQuery
| RqlBinary
| datetime.date
| 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.")

Expand All @@ -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)):
Expand All @@ -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)

Expand Down
29 changes: 29 additions & 0 deletions tests/test_date_and_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Loading