Skip to content

Conversation

@kapkekes
Copy link

Currently, the recommended way to use Depends (judging by the FastAPI docs) is with typing.Annotated:

Prefer to use the Annotated version if possible.

However, typing.get_type_hints strips Annotated metadata by default, so you cannot do something like this at the moment:

...

async def provide_storage() -> dict[str, int]:
    return {"A": 1, "B": 2}


StorageDependency = Annotated[dict[str, int], Depends(provide_storage)]


router = APIRouter()

@cbv(router)
class ExampleView:
    storage: StorageDependency

    async def get_value(self, name: str) -> int:
        return self.storage[name]

...

Passing include_extras=True to get_type_hints solves this.

@kapkekes kapkekes changed the title Add support for Annotated[..., Depends(...)] Add support for Annotated[..., Depends(...)] in CBV Nov 11, 2024
@kapkekes
Copy link
Author

@yuval9313, hi!

Stumbled upon this issue, quite enjoying using @cbv, let's improve it further!

@kapkekes kapkekes force-pushed the annotated-depends-support branch from 21e12bd to 5c5cb96 Compare November 11, 2024 17:08
@yuval9313
Copy link
Collaborator

What exactly does it solve? I can't see an issue about it, also, there is no test case, I suggest you add one

@kapkekes kapkekes closed this Nov 12, 2024
@kapkekes
Copy link
Author

I'll open another with tests.

@kapkekes kapkekes deleted the annotated-depends-support branch November 12, 2024 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants