punq icon indicating copy to clipboard operation
punq copied to clipboard

Support constructing objects with partial default parameters

Open jbcpollak opened this issue 5 years ago • 4 comments

Given code like (this is an untested example):

class A:
    pass

class B:
    def __init__(a: A, i: int = 0):
        pass

container.register(A)
container.register(B)
b = container.resolve(B)

I'm getting a stack trace with this error:

punq.MissingDependencyException: Failed to resolve implementation for <class 'int'>

Is it possible to modify punq to use the inspect library to check for defaults, and leave any unsupplied fields empty?

jbcpollak avatar Feb 24 '20 23:02 jbcpollak

I'm also running into this problem with attr when a given attribute has a default value, or when using auto_attribs. For instance, taking the above example:

@attr.s(auto_attribs=True)
class B:
  i: int

or

@attr.s()
class B:
   i: int = attr.ib(default=0)

Will trigger that error.

MennoNij avatar May 20 '20 19:05 MennoNij

Hi @jbcpollak . This definitely seems like a thing we should support. I'll take a look over the weekend and seeif we can fix it.

bobthemighty avatar Jun 05 '20 07:06 bobthemighty

Thanks for looking into both issues!

jbcpollak avatar Jun 06 '20 18:06 jbcpollak

Hi @bobthemighty , just wondering if you ever got a chance to look at this issue?

jbcpollak avatar Nov 03 '20 15:11 jbcpollak