Git Product home page Git Product logo

fastapi-lifespan-manager's Introduction

Hi there ๐Ÿ‘‹

Can you help me learn Haskell?

My OOP mind struggles every time I attempt to learn it ๐Ÿ˜ข

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

About me

I am a Software Engineer who mostly works with Python but used to work with C/C++ and Java a long time ago. Hope one day I will be able to understand Haskell (I have a dream to learn functional programming).

PEP 505 None-aware operators is a feature that I miss the most in Python.

I?.really?.want?.this?.feature ?? "Or not?"

Things done by me

FastAPI related

If you are using FastAPI and use function return type annotation as response_model you should know - I am the one who implemented it ๐Ÿ˜„ (FastAPI #1436).

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI(title="Bio")


class Bio(BaseModel):
    name: str
    education: str
    languages: set[str]


@app.get("/me")
async def get_bio() -> Bio:
    return Bio(
        name="Yurii",
        education="Master's degree in Computer Engineering",
        languages={"Ukrainian", "English", "Polish", "German (a bit)"},
    )

Also, I have created fastapi-pagination package that helps you to paginate things (obviously) using FastAPI ๐Ÿ“–. Another thing that I currently work on is fastapi-filters package. Hope one day it will be useful for someone as fastapi-pagination is ๐Ÿธ.

Python related

I have several contributions to Python itself (commits). The most interesting one is (#23316 Store func annotations as a tuple). This optimization reduces memory footprint and improves performance of loading modules having many func annotations.

>>> sys.getsizeof({"a":"int","b":"int","return":"int"})
232
>>> sys.getsizeof(("a","int","b","int","return","int"))
88

The tuple is converted into dict on the fly when func.__annotations__ is accessed first.

PyCharm related

I have created a plugin (pycharm-evaluate-async-code) for PyCharm that helps you evaluate async code the using Evaluate Expression window. This is really useful when you are debugging your code and want to check some async stuff. I guess it's my favorite project that I have ever done (because I used it every day and it really saved me a lot of time).

evaluate_expression

fastapi-lifespan-manager's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar uriyyo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

mvandermeulen

fastapi-lifespan-manager's Issues

compatible with asgi_lifespan

Hi
first, thanks for maintain this tool,
i used asgi_lifespan for handle async tests, after use this package for separate my lifespans I've get error:
RuntimeError: The server does not support "state" in the lifespan scope.
what should i do?

Incompatible type for lifespan manager in v.0.1.2

The last release (0.1.2) breaks the static analysis of using lifespan manager.

lifespan_manager = LifespanManager()

app = FastAPI(lifespan=lifespan_manager)

mypy error: Argument "lifespan" to "FastAPI" has incompatible type "LifespanManager[FastAPI]"; expected "Callable[[FastAPI], AbstractAsyncContextManager[None]] | Callable[[FastAPI], AbstractAsyncContextManager[Mapping[str, Any]]] | None" [arg-type]

Package versions:

  • fastapi==0.109.0
  • mypy==1.8.0
  • fastapi-lifespan-manager==0.1.2

How to have one piece of state depend on another?

Say I want to have one piece of state that is an httpx client and another that is an OpenAI API client, which takes an httpx client on initialization. If I do

manager = LifespanManager()

@manager.add
async def set_up_httpx(app: FastAPI) -> AsyncIterator[State]:
    async with httpx.AsyncClient() as client:
        yield {"http": client}

@manager.add
async def set_up_openapi(app: FastAPI) -> AsyncIterator[State]:
    http: httpx.AsyncClient = app.state.http
    client = AsyncOpenAI(http_client=http)
    yield {"openai": client}

the latter call does not receive the state from the earlier call because the state is not attached to the app here:

sub_state = await astack.enter_async_context(_run_raw_lifespan(raw_lifespan, app))

Is there a recommended way to do this?

Issue With Nginx Unit

Hi,
Thanks for your useful utility to support multi lifespans.
I have issue running application with nginx-unit when using your library.
the error is:


 File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 680, in lifespan
    raise RuntimeError(
RuntimeError: The server does not support "state" in the lifespan scope.


do you have any idea how to fix this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.