Lancetnik / Propan

Propan is a powerful and easy-to-use Python framework for building event-driven applications that interact with any MQ Broker
https://lancetnik.github.io/Propan/
MIT License
485 stars 28 forks source link

Generator-based dependency functions fail with Propan #108

Closed pythrick closed 1 year ago

pythrick commented 1 year ago

Description When trying to use a FastAPI dependency function with a yield statement in Propan, I found that the function is not awaited. The assert error is raised in this case, but using a function with a return statement instead of yield works fine.

Steps to reproduce

  1. Define a FastAPI dependency function with a yield statement:

    async def get_db() -> str:
        yield "connected"
  2. Use it as a dependency in a Propan event function:

    @router.event("test")
    async def hello(m: Incoming, db: str = Depends(get_db)) -> dict:
        print(m.model_dump_json())
        print(db)
        return {"response": "Hello, Rabbit!"}
  3. Run the app and make a GET request. An AssertionError is raised.

Expected behavior The dependency function get_db should be awaited, just as when we use FastAPI's dependency injection system for HTTP requests.

Actual behavior The AssertionError is raised when trying to use a dependency function with a yield.

Environment:

Additional information

I believe the issue happens because in Propan, the cleanup part of a dependency function that yields (the part that comes after the yield statement) never gets run. FastAPI's dependencies are designed with HTTP request lifespans in mind, and as such dependency functions using yield are treated as context managers that perform cleanup after each request. The issue could be due to Propan not fully supporting this model, especially when it comes to context manager behavior of dependencies.

Any assistance would be greatly appreciated!

Lancetnik commented 1 year ago

It's a pretty strange cuz Propan as a FastAPI router uses a FastAPI DI system. Anyway, will check and fix it tomorrow, thanks for the Issue.

Lancetnik commented 1 year ago

@pythrick the job is done! Just update Propan to 0.1.5.13 and FastAPI yield Depends works fine.

pythrick commented 1 year ago

@Lancetnik thank you for the quick fix! It was fast! I'm really enjoying this framework, thank you for your great work building it.

Lancetnik commented 1 year ago

@Lancetnik thank you for the quick fix! It was fast! I'm really enjoying this framework, thank you for your great work building it.

Thank you for the Issue and for the FastDepends contribution too! I am always trying to fix any bugs issues in the same day (or a few days later), so be free to report of any bugs or features you wish to see in the Propan.

Also, if you are so enjoied by Propan, please tell others about it (by tweet, article, smth). The bigger community we have - the better project will be!