question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] [FEATURE?] Why do the generated docs use Z-terminated ISO strings when python doesn't?

See original GitHub issue

First check

[x] I used the GitHub search to find a similar issue and didn’t find it. [x] I already searched in Google “How to X in FastAPI” and didn’t find any information. [x] I searched the FastAPI documentation, with the integrated search.

Description

So, first of all, I know that pydantic is smart and accepts strings ending in Z for datetimes. I am thankful that pydantic follows ISO guidelines even though python’s standard datetime library rejects them.

But It feels weird that the generated docs use this approach rather than the default expected by python (which is, often, to treat a naive datetime as UTC, but otherwise to append +00:00.

It’s noteworthy that even when pydantic serializes back to json, it uses the +00:00.

Additionally, given that we can’t trivially (by type hints) tell the pydantic model to be based on a specific timezone, it feels like the generated documentation is misleading, suggesting to users that they should be using a timezone-aware string when in fact, there’s no guarantee that the code is intended to handle a timezone aware string.

I think I would expect the default behavior to specify simply naive datetime iso strings and have to do something additional to make the documentation suggest tz aware strings, rather than the other way around.

Additional context

image

In [10]: x = Thing(a="2020-01-01T00:00Z")

In [11]: x.json()
Out[11]: '{"a": "2020-01-01T00:00:00+00:00"}'

In [12]: x = Thing(a="2020-01-01T00:00:00")

In [13]: x.json()
Out[13]: '{"a": "2020-01-01T00:00:00"}'

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dcrowecommented, May 18, 2022

I’ve found two ways to make FastAPI produce ISO8601 datetimes:

datetime.now(timezone.utc)
time.time()
1reaction
dorinclisucommented, Sep 28, 2021

This issue is not solved, the problem is the fastapi docs still state that datetime will be seriallized to ISO 8601. https://fastapi.tiangolo.com/tutorial/extra-data-types/ Which is a big lie as @wozniakty pointed out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert Python's .isoformat() string back into datetime ...
So in Python 3, you can generate an ISO 8601 date with .isoformat(), but you can't convert a string created by isoformat() back...
Read more >
PEP 350 – Codetags - Python Enhancement Proposals
The following contrived example demonstrates a typical use of codetags. It uses some of the available fields to specify the assignees (a pair...
Read more >
Unicode and passing strings — Cython 3.0.0a11 documentation
CPython 3.3 has moved to a flexible internal representation of unicode strings (PEP 393), making all Py_UNICODE related APIs deprecated and inefficient.
Read more >
How to Write Doc Comments for the Javadoc Tool - Oracle
The comments should not document bugs or how an implementation that is ... However, if the Javadoc tool is being used to generate...
Read more >
IO tools (text, CSV, HDF5, …) — pandas 1.5.2 documentation
index_col=False can be used to force pandas to not use the first column as the ... are faster, while the python engine is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found