Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FastAPI integration #30

Merged
merged 3 commits into from
Aug 15, 2023
Merged

Add FastAPI integration #30

merged 3 commits into from
Aug 15, 2023

Conversation

hynek
Copy link
Owner

@hynek hynek commented Aug 13, 2023

So I've done this mostly blindly by trawling the FastAPI docs… it would be great if some FastAPI expert could give me feedback if this makes conceptually sense, or if I've got something fundamentally wrong. @tiangolo maybe? 😇

@hynek hynek force-pushed the fastapi branch 19 times, most recently from 68b185e to 3475083 Compare August 14, 2023 09:22
@michaeloliverx
Copy link

Maybe the FastAPI expert himself @Kludex?

@hynek
Copy link
Owner Author

hynek commented Aug 14, 2023

Every FastAPI expert is welcome; I'm not in the know enough to @ spam them all 🐶 💛

@Kludex
Copy link

Kludex commented Aug 14, 2023

@michaeloliverx used "the" because I advertise myself as such: https://www.fastapiexpert.com/

I can take a look tonight.

@hynek
Copy link
Owner Author

hynek commented Aug 14, 2023

Awesome thanks!

@tiangolo
Copy link
Contributor

And @Kludex IS the current top official FastAPI Expert: https://fastapi.tiangolo.com/fastapi-people/#experts 😎

@tiangolo
Copy link
Contributor

I haven't used svcs, but checking mainly the examples they look great! I think it's all good from the FastAPI point of view.

One small thing I would do, mainly just syntax sugar, as this seems like it would be a common pattern:

def do_stuff(
    svcs: Annotated[svcs.Container, Depends(svcs.fastapi.container)],
):
    ...

In svcs.fastapi I would also create/export a type alias with that, something like:

DepContainer = Annotated[svcs.Container, Depends(svcs.fastapi.container)]

And then in user's code they could do:

from svcs.fastapi import DepContainer

def do_stuff(
    svcs: DepContainer,
):
    ...

...one of the big reasons to support Annotated was to allow other tools (e.g. svcs) to do this trick and make users' code super simple. 🤓

I would maybe also comment in the docs that both things are equivalent. But just provide that type alias for extra niceness. ✨

@hynek
Copy link
Owner Author

hynek commented Aug 15, 2023

I haven't used svcs,

Nobody has except me. 🤓 There's still a fat warning that every release is a breaking release and it's basically an internal project that I'm trying to make fit for the public.

And to make sure I don't paint myself into a corner, I'm trying to implement idiomatic integrations for all common web frameworks (starlette is next, but I believe it's just recycling of svcs.fastapi.lifespan plus a middleware or maybe just a svcs_from).

It's been a hectic month, but I'm starting to see the light at the end of the tunnel

but checking mainly the examples they look great! I think it's all good from the FastAPI point of view.

Good to hear!

One small thing I would do, mainly just syntax sugar, as this seems like it would be a common pattern:

def do_stuff(
    svcs: Annotated[svcs.Container, Depends(svcs.fastapi.container)],
):
    ...

In svcs.fastapi I would also create/export a type alias with that, something like:

DepContainer = Annotated[svcs.Container, Depends(svcs.fastapi.container)]

And then in user's code they could do:

from svcs.fastapi import DepContainer

def do_stuff(
    svcs: DepContainer,
):
    ...

...one of the big reasons to support Annotated was to allow other tools (e.g. svcs) to do this trick and make users' code super simple. 🤓

I would maybe also comment in the docs that both things are equivalent. But just provide that type alias for extra niceness. ✨

Ah that’s cool, this is exactly the feedback I’ve been hoping for – thank you so much!

hynek and others added 2 commits August 15, 2023 15:20
@hynek hynek merged commit 8421547 into main Aug 15, 2023
@hynek hynek deleted the fastapi branch August 15, 2023 13:28
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.

4 participants