Skip to content

In FastAPI 0.123.7, annotations from code imported in if TYPE_CHECKING could break #14484

@tiangolo

Description

@tiangolo

Privileged issue

  • I'm @tiangolo or he asked me directly to create an issue here.

Issue Content

In FastAPI 0.123.7, annotations from code imported in if TYPE_CHECKING could break

Originally discussed in #14464 and #11355

Minimal example extracted from the discussion:

# /// 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)

Handled in #14485

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions