bobthemighty / punq

An IoC container for Python 3.6+
MIT License
301 stars 13 forks source link

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

Open belegnar opened 1 year ago

belegnar commented 1 year ago

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