plaid/plaid-python

Why expected type for min_last_updated_datetime is datetime?

CptFanerka opened this issue · 3 comments

I create a request using below code:

request = AccountsBalanceGetRequest(
    access_token=access_token
    )
if parameters.get('min_last_updated_datetime'):
    request.set_attribute(
        'options', 
        AccountsBalanceGetRequestOptions(
            min_last_updated_datetime=parameters['min_last_updated_datetime']
            )
        )
response = app.client.accounts_balance_get(request)

In Plaid Docs for /accounts/balance/get endpoint specified string type for min_last_updated_datetime field.
When I try to send request with string like '2022-05-05T00:00:00Z' I got error:

Invalid type for variable 'min_last_updated_datetime'. Required value type is datetime and passed type was str at ['min_last_updated_datetime']

Than I transformed this string to datetime with code:

date_format = "%Y-%m-%dT%H:%M:%SZ"
min_last_updated_datetime = datetime.strptime('2022-05-05T00:00:00Z', date_format)

When I try to send request with datetime like '2022-05-05 00:00:00' (class datetime.datetime) I got error in Plaid response:

options.min_last_updated_datetime must be a valid RFC 3339 datetime string

I use __version__ = "8.6.0" of plaid SDK.
What am I doing wrong or misunderstanding?

This is happening because "datetime.strptime silently throws away all timezone information." (source: https://www.enricozini.org/blog/2009/debian/using-python-datetime/) which is resulting in a datetime that is not RFC3339 compliant, which is what the API is upset about.

Some suggestions on how to deal with this issue here:
https://stackoverflow.com/questions/3305413/how-to-preserve-timezone-when-parsing-date-time-strings-with-strptime
and here:
#414

Let me know if that resolves it or if you have any other questions

cc: @ghernandez-plaid for the documentation

also cc: @vorpus -- I know this is the default generator behavior but it is turning out to be pretty annoying given the the way in which Python handles date-times by default, is there any way we can get the Python client libraries to also accept strings for date-times?

Hi @CptFanerka!

The min_last_updated_datetime field for /accounts/balance/get requires a datetime object with timezone information specified (using the ISO8601 format of YYYY-MM-DDTHH:mm:ssZ).

date_format = "%Y-%m-%dT%H:%M:%SZ"
min_last_updated_datetime = datetime.strptime('2022-05-05T00:00:00Z', date_format)

In this code snippet, '2022-05-05T00:00:00Z' is a string that contains timezone information (the "Z" at the end of the string, more details in this article), but as Alex noted above, strptime will remove the timezone information. Although min_last_updated_datetime is a datetime object (i.e., class datetime.datetime), it does not include the necessary timezone information. You can check this by printing the value of min_last_updated_datetime – it returns datetime.datetime(2022, 5, 5, 0, 0), which is a datetime object without timezone information. If you call isoformat() on this object just to inspect the value, the return value is a string: '2022-05-05T00:00:00', but note the missing 'Z' (or the equivalent+00:00) at the end (i.e., the timezone information).

Consider using the following syntax instead:

a = datetime.datetime(2022, 5, 5, 0, 0, tzinfo=datetime.timezone.utc)

To inspect the value of a, call isoformat():

a.isoformat()
# '2022-05-05T00:00:00+00:00'

Note the +00:00 at the end (equivalent to Z).

I hope this is helpful - please let me know if you have any follow up questions!

Thanks,

Gilberto

Thanks for the explanation, @phoenixy1 , @ghernandez-plaid ! Your answers were helpful for me.