punq icon indicating copy to clipboard operation
punq copied to clipboard

`MissingDependencyError` if class declared in a module with `__main__` part

Open belegnar opened this issue 2 years ago • 0 comments

Hello

# app.py
import aiohttp.web
import punq

from project import routes


class A:
    pass


if __name__ == "__main__":
    container = punq.Container()
    container.register(A, instance=A())

    app = aiohttp.web.Application()
    app["container"] = container
    routes.setup_routes(app)
    aiohttp.web.run_app(app)
# routes.py
import aiohttp.web

from project import view


def setup_routes(app: aiohttp.web.Application):
    app.router.add_route("GET", "/route", view.handler)
# view.py
import aiohttp.web
import punq

from project import app


async def handler(r: aiohttp.web.Request):
    c: punq.Container = r.app["container"]
    c.resolve(app.A)
    return aiohttp.web.Response()

Request to /route results in MissingDependencyError because it exists as __main__.A as I understand

belegnar avatar Sep 09 '23 14:09 belegnar