inheritance & dependencies
See original GitHub issueHello! You helped me out already more than once, but I admit I’m getting tremendously frustrated, as I just don’t have an adequate insight to what is happening under the hood.
Let me sketch my problem – my current code has gone through so many failed experiments I’m sure it looks ridiculous.
I have a Problem
container (for a machine learning problem), and two hierarchies of other containers:
ProblemView
-> Conjoiner
-> GroundConjoiner
ProblemView
-> CutsContext
Here, problem view refers to a problem via:
class ProblemView(containers.DeclarativeContainer):
problem = providers.DependenciesContainer()
And it takes optional dependencies that override parts of Problem
, e.g.:
...
weight: Provider[xr.DataArray] = providers.Dependency(
instance_of=xr.DataArray
)
weight.override(problem.training.provided.weight)
Then it redefines parts of problem based on possibly overridden values:
training = providers.Callable(
override_weight, problem.training.provided, weight.provided
)
ProblemView works on its own.
Next, Conjoiner is a problem view (Or has a problem view – though I’d prefer is a problem view so it can inherit “has a problem” – but either way I can’t get it to work). It takes a CutsContext as a dependency.
@container.copy(ProblemView)
class Conjoiner(ProblemView):
cuts = providers.Container(CutsContext)
And GroundConjoiner
(among others) is a Conjoiner
; it provides a CutsContext
to parent Conjoiner
with given overall problem
and weights
(as well as some other customization specific to CutsContext).
@container.copy(Conjoiner)
class GroundConjoiner(Conjoiner):
problem = Conjoiner.problem # or...?
weight = Conjoiner.weight
# also tried (and same for weight)
# problem_ = Conjoiner.problem
# problem = providers.DependenciesContainer()
# problem_.override(problem)
ground_cuts_context = providers.Container(
Cuts,
problem=problem, weight=weight
...
)
cuts_context = ground_cuts_context.provided
Instantiation:
gc = GroundConjoiner(problem=problem, weight=weight)
I have tried different permutations all weekend (under a looming deadline 😦), and can generate many different errors (either some dependency not defined, or – e.g. src/dependency_injector/providers.pyx:1080: in dependency_injector.providers.ConfigurationOption.__init__: TypeError: cannot create weak reference to 'dependency_injector.providers.ProvidedInstance' object
), but I can’t glean from the documentation any insight into what is going on sufficient to alight on a correct solution (or even a clear bug report).
I’ll be very grateful for any help!
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (4 by maintainers)
Top GitHub Comments
Hey @shaunc , I’ll take a look. Will take some time for debugging. I’ll get back as soon as I got something.
Glad to help! Thanks for your patience and input to make Dependency Injector to be a better tool.