edgedb / edgedb-python

The official Python client library for EdgeDB
https://edgedb.com
Apache License 2.0
369 stars 45 forks source link

Inconsistent datetime format return #357

Closed jrycw closed 1 year ago

jrycw commented 2 years ago

I created a small FastAPI application to check this behavior. edgedb schema:

module default {
    type DTime {
        required property created_on -> datetime {
            default := datetime_current();
        };
    }
}

FastAPI concept code:

import json
from datetime import datetime
from typing import Union

import edgedb
from edgedb.asyncio_client import AsyncIOClient
from fastapi import Depends, FastAPI
from pydantic import BaseModel

app = FastAPI()
async_db_client: AsyncIOClient = edgedb.create_async_client()

class DatetimeIn(BaseModel):
    created_on: Union[datetime, None]

class DatetimeOut(DatetimeIn):
    pass

def get_async_db_client() -> AsyncIOClient:
    return async_db_client

@app.post('/dtime', response_model=DatetimeOut)
async def post_dtime(client: AsyncIOClient = Depends(get_async_db_client)) -> DatetimeOut:
    query = '''SELECT (INSERT DTime) {created_on};'''
    dtime = await client.query_required_single_json(query)
    print(f'{dtime=}')
    return json.loads(dtime)

First of all, I'm not sure whether this is a bug or is the behavior the team desired. I found this behavior since occasionally my tests failed while checking datetime. While using query_required_single_jsonto get the datetime back, the last digit of microseconds will influence the return format.

For example, if the datetime in edgedb is default::DTime {created_on: <datetime>'2022-07-30T07:27:29.040208Z'}, then I could get "2022-07-30T07:27:29.040208+00:00" in python, this is perfectly fine.

However, if the datetime in edgedb is default::DTime {created_on: <datetime>'2022-07-30T07:27:29.889420Z'}, I surprisingly got "2022-07-30T07:27:29.88942+00:00" in python. It seems that if the last digit of microseconds is 0, then the client will ignore it and just return 5 digits instead of 6.

I'm using edgedb(2.0) and edgedb-python client(0.24.0) under Python 3.9.7 in Ubuntu 2004.

fantix commented 1 year ago

This was explained in edgedb/edgedb#2412