bobthemighty / punq

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

Priority for kwargs, or support for recursive resolve #169

Open prefect4742 opened 1 year ago

prefect4742 commented 1 year ago

Two separate issues but they are related. I have classes that are nodes in a tree and I would like to be able to send in a registered entity as a parameter in kwargs when calling resolve(), like this:

class Example:
    def __init__(self, parent: 'Example', someParam: str):
        ...

parentExample = container.resolve(parent=None, someParam="Hello")
oneExample = container.resolve(parent=parentExample, someParam="world")

But as I understand the code in resolve_impl, punq will try to resolve any "needs" (registered) parameters first which would cause a loop in the __init__ code above, correct?

Is there some way to achieve this other than registering factory classes?

bobthemighty commented 1 year ago

Hey @prefect4742

Sorry for the delayed reply. That sounds like a sensible change. I might take a look this week and see what's involved in implementing it this way.