Yelp/bravado

Nanosecond datetime precision lost when SwaggerClient parses it

ecobee-robbie-dumbrell opened this issue · 1 comments

I have a field defined in a swagger-spec.json file that sits in an endpoint response definition with:

"myDateTimeField": {
          "type": "string",
          "format": "date-time",
        }

The value of myDateTimeField in my API goes to nanosecond precision and exists in the following format:

"2021-02-22T21:09:34.411978573Z"

When using
SwaggerClient.from_spec(my_swagger_json).my_api_endpoint().response().result
and inspecting the result, I can see that the nanosecond precision has been lost during parsing, and the value of myDateTimeField becomes:

datetime.datetime(2021, 2, 22, 21, 9, 34, 411978, tzinfo=tzutc())

This can cause issues as if I then go on to make use of the datetime value, or use the response in the body of a sequential PUT request, the value will be be seen by my backend as "amended".

Is this behaviour to limit datetime to microsecond precision intentional - or am I missing something in my formatting?

Thanks!

@ecobee-robbie-dumbrell Python datetime object does not support nanosecond precision

>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=9, micro=1, releaselevel='final', serial=0)
>>> import datetime
>>> datetime.datetime(2021, 2, 22, 21, 9, 34, 411978123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: microsecond must be in 0..999999

Considering this I don't think that this is a lack on the bravado library, but rather a limitation of Python.

NOTE: This is a very know limitation but seems that so far no progress has been done in this direction (https://bugs.python.org/issue15443, https://www.python.org/dev/peps/pep-0410/).