Skip to content

Injecting dependencies from parent class #23

Answered by Aegdesil
mateuszzebek asked this question in Q&A
Discussion options

You must be logged in to vote

There is no way to do that as there is no way to know which argument from the child class is mapped to which argument in the parent class (you could be manipulating the kwargs before calling the parent constructor).

You have 3 solutions:

  • add type hints in the child class (but if you are asking this I guess you have a reason not to do that, maybe you don't control this code ?)
  • add a Provider to generate the Y class, the provider would have the same dependencies as Y but can be injected if its constructor has typed arguments
  • subclass the child class again with a class that only overwrites the constructor with typed arguments

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@mateuszzebek
Comment options

@Aegdesil
Comment options

@mateuszzebek
Comment options

@Aegdesil
Comment options

Answer selected by alexmick
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants