Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto wiring #823

Open
sparky2708 opened this issue Oct 9, 2024 · 3 comments
Open

Auto wiring #823

sparky2708 opened this issue Oct 9, 2024 · 3 comments
Labels

Comments

@sparky2708
Copy link

sparky2708 commented Oct 9, 2024

Is there a way to auto-wire?

e.g.:

class B:
@Inject
def init(self, registered_class: IRegisteredClass):
...

Why do I need to specify Provider[...]. If all are wired why not just auto-wire IRegisteredClass interface to the object that is registered in some container that implements this interface?

@LeOndaz
Copy link

LeOndaz commented Oct 12, 2024

If this seems viable for the author to have, I can PR this

I neither checked the code nor used the package, so the decision is mainly up to people who know it very well, I'm mainly looking to contribute in DI projects since I'm a fan of the pattern

@dbarwacz
Copy link

dbarwacz commented Dec 12, 2024

I opened issues looking for some context on this specific question as I assumed it's something that can be done but I don't know how.
My thinking as a DI user is that, if I configure/describe the dependencies and their relationships in container, then I can 'just' request the type (with qualifiers etc) and get it on a call-site.
If I need to define relationships in a container, and then reference that container in a call-site to describe where/how dependences are to be provided, it kind of defeats the purpose of DI - which is to leave config to container and have 'pure' logic methods/functions that 'just get' what they need.

@ZipFile
Copy link
Contributor

ZipFile commented Dec 17, 2024

@sparky2708

Why do I need to specify Provider[...]. If all are wired why not just auto-wire IRegisteredClass interface to the object that is registered in some container that implements this interface?

I believe main reason is the fact that Python type system is different from Java-like ones. While technically you can achieve something similar to your suggestion, it falls apart when you have to deal with protocols, python generics, missing type info, etc... Python does have some notion of nominative type system to make this work, but still its dynamic nature makes it difficult to work with types in general.

I was not here since project inception, but I've tried implementing my own DI solution once. I've abandoned the idea because loads of emerging edge cases using this approach.

Is there a way to auto-wire?

So no, not now (given your definition of auto-wire).

@dbarwacz If you need full decoupling, use Provide["service"] notation (see docs). But in general, I prefer to use @inject as a last resort, when there is no other way to provide dependency into your class/function and suggest you to do so too. This keeps your code clean from DI stuff, not interfering with DI provided by test suite (pytest fixtures).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants