maldoinc / wireup

Performant, concise, and easy-to-use dependency injection container for Python 3.8+.
https://maldoinc.github.io/wireup/
MIT License
97 stars 2 forks source link

Suggestion: Allow for protocols to be used as interfaces #33

Closed ewilazarus closed 1 month ago

ewilazarus commented 1 month ago

Currently it seems like only abc.ABC classes can be used as interfaces.

It would be great to add support for typing.Protocols as well!

Happy to try to put a PR together, but might need some guidance.

ewilazarus commented 1 month ago

Duplicates https://github.com/maldoinc/wireup/issues/3

maldoinc commented 1 month ago

Thanks for the interest in this. Registering a protocol with @abstract should already work but when registering a service, the code needs to associate the thing being registered with the given protocol.

The main thing to do here would be to update the registry register_{service,factory} methods and to update registration decorators with any new arguments.

maldoinc commented 1 month ago

A reason why I haven't prioritized this is also that I cannot quite get the typing to work as I want: When decorating a class or factory with @container.register(supports=SupportsFoo) ideally the type checker should flag if the thing being created does not actually adhere to the protocol.