punq icon indicating copy to clipboard operation
punq copied to clipboard

Resolving with `*`

Open belegnar opened this issue 10 months ago • 0 comments

Consider the following snippet

import punq


class I:
    pass


class E:
    def __init__(self, host, user=None):
        self.host = host
        self.user = user


class A(I):
    def __init__(self, host, user=None):
        self.host = host
        self.user = user


class B(E, I):
    def __init__(self, host, user=None):
        super().__init__(host, user=user)


if __name__ == '__main__':
    c = punq.Container()
    c.register(I, factory=A)
    c.register(I, factory=B)

    for impl in c.resolve_all(I, host="host", user="user"):
        print(impl.__class__, impl.host, impl.user)

In this implementation it prints

<class '__main__.A'> host user
<class '__main__.B'> host user

But if __init__s are replaced with (add *)

    def __init__(self, host, *, user=None):

the user is lost

<class '__main__.A'> host None
<class '__main__.B'> host None

belegnar avatar Feb 27 '25 11:02 belegnar