Breaking change in dependency type inspection in FastAPI 0.123.7 #14464
-
First Check
Commit to Help
Example Code# /// script
# requires-python = "==3.14.*"
# dependencies = [
# "fastapi==0.123.7",
# "uvicorn",
# ]
# ///
from __future__ import annotations
from typing import TYPE_CHECKING, Annotated
from fastapi import Depends, FastAPI
if TYPE_CHECKING:
from collections.abc import AsyncGenerator
app = FastAPI()
class DummyClient:
async def get_people(self) -> list:
return ["John Doe", "Jane Doe"]
async def close(self) -> None: ...
async def get_client() -> AsyncGenerator[DummyClient, None]:
client = DummyClient()
yield client
await client.close()
Client = Annotated[DummyClient, Depends(get_client)]
@app.get("/")
async def get_people(client: Client) -> list:
return await client.get_people()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)DescriptionThis code works under FastAPI < 0.123.7, but fails with newer releases: traceback
Operating SystemmacOS Operating System DetailsNo response FastAPI Version0.123.9 Pydantic VersionMA Python Version3.14.0 Additional ContextMoving the from __future__ import annotations
+ from collections.abc import AsyncGenerator
- from typing import TYPE_CHECKING, Annotated
+ from typing import Annotated
from fastapi import Depends, FastAPI
- if TYPE_CHECKING:
- from collections.abc import AsyncGeneratorNote: seems like #11355 introduced the change. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 6 replies
-
|
I think #11355 should be reverted, given the very confusing effects it has (see here). |
Beta Was this translation helpful? Give feedback.
-
|
Agreed this is a breaking change, it also forces non-runtime requirements (e.g. type/stub files) to be installed within releases, bloating install size which shouldn't be required. e.g. |
Beta Was this translation helpful? Give feedback.
-
|
It looks like it is related: |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
|
Also have another repro without the use of |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the very simple replicable example @edgarrmondragon! 🚀 I used it in the tests. I created an issue here for completeness: #14484 This should be solved by #14485 It's available in FastAPI 0.124.2, just released 🎉 |
Beta Was this translation helpful? Give feedback.
Thanks for the very simple replicable example @edgarrmondragon! 🚀
I used it in the tests.
I created an issue here for completeness: #14484
This should be solved by #14485
It's available in FastAPI 0.124.2, just released 🎉