question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

injection of a dynamic class

See original GitHub issue

Given a class known at runtime (dynamic) with dependencies in the c’tor, can we use the container to initialize such class?

E.g:

from dependencies import Injector

class Person:
    def __init__(self, name: str):
        pass

def get_cls():
    return Person

class Container(Injector):  # (1)
    ...
    # (2)

# (3)
person = ...  # (4)
  1. I tried to inherit from more classes, but got an exception: _dependencies.exceptions.DependencyError: Multiple inheritance is allowed for Injector subclasses only
  2. I tried to use __new__ but with no luck
  3. I tried to register a class variable after the class definition, but got an exception: _dependencies.exceptions.DependencyError: 'Injector' modification is not allowed

Is there away to create such dynamic behavior? Or is it inevitable becuase of the nature of the simplified and non-boilerplate API?

@proofit404 Looking forward to hear your opinion 😃

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
proofit404commented, May 13, 2022

Feel free to reopen this issue if you have further questions.

Have a good day 🌴 🍸

Best regards, Artem.

1reaction
proofit404commented, May 13, 2022
>>> from dependencies import Injector
>>> from dataclasses import dataclass
>>> @dataclass
... class Person:
...     name: str
...
>>> @dataclass
... class Animal:
...     age: int
...
>>> def get_plugin_classes():
...     return [Person, Animal]
...
>>> @dataclass
... class Plugins:
...     plugins_lst: ...
...
>>> class AllDeps(Injector):
...     name = 'hello'
...     age = 5
...
>>> class Container(Injector):
...     plugins = Plugins
...
>>> Container(plugins_lst=[AllDeps(plugin=plugin).plugin for plugin in get_plugin_classes()]).plugins
Plugins(plugins_lst=[Person(name='hello'), Animal(age=5)])

We could try something like this.

It still requires you to know names of possible dependencies for all classes which could be returned from get_plugin_classes.

What do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Injection of a dynamic class · Issue #586 - GitHub
Given a class known at runtime (dynamic) with dependencies in the c'tor, can we use the container to initialize such class?
Read more >
Dynamic dependency injection - Stack Overflow
Let plugins rely on Constructor Injection, which has the advantage that constructors statically announce each class' dependencies. Then use ...
Read more >
Dynamic dependency injection by pwm - GitHub Pages
In this short article I would like to demonstrate a way to inject dependencies that are not known until runtime. There are many...
Read more >
Dynamic request class injection in laravel - Laracasts
I have a custom request class which is getting longer and longer with project development. An API endpoint is expected to have multiple...
Read more >
Dependency injection in dynamically create instances
I have a class that is dynamically instantiated using reflection and which need to use a service. The service depends on a repository...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found