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 return provide object not actual dependency object

See original GitHub issue

i have a class to be injected src/runner_rating/api/rating/controller.py

class Rating():
    @inject
    def __init__(
                self,
                repo: RatingRepository = Provide[Container.repo.rating]):
        self.repo = repo

    def get_by_id(self, id):
        data = self.repo.get_by_id(id)
        if not data:
            raise HTTPNotFoundException(self.repo.index_name)

this is my container looks like: src/container.py

class ESContainer(containers.DeclarativeContainer):
    config = providers.Configuration()
    client = providers.Singleton(
        elasticsearch_client.ElasticsearchClient,
        config=config.elasticsearch
    )

class RepositoryContainer(containers.DeclarativeContainer):

    config = providers.Configuration()
    db = providers.DependenciesContainer()

    rating = providers.Singleton(
        RatingRepository,
        db=db.client
    )

class Container(containers.DeclarativeContainer):

    config = providers.Configuration()
    es = providers.Container(
        ESContainer,
        config=config
    )

    repo = providers.Container(
        RepositoryContainer,
        config=config,
        db=es
    )

this is when Container were called: src/application.py

from src import runner_rating
def main():
    # register dependency
    di_container = Container()
    di_container.config.from_dict(config.get_config())
    di_container.wire(modules=[runner_rating])


when i try Rating.get_by_id it shows error that:

 data = self.repo.get_by_id(id)
AttributeError: 'Provide' object has no attribute 'get_by_id'

Why? i’am using v2.5.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
rmk135commented, Nov 25, 2020

@ijalalfrz , try to change di_container.wire(modules=[runner_rating]) to di_container.wire(packages=[runner_rating]).

0reactions
rmk135commented, Nov 26, 2020

Yes, ‘get_by_id()’ works because you call it in the runtime.

Thank you. My pleasure to help.

Best, Roman

Read more comments on GitHub >

github_iconTop Results From Across the Web

'Provide' object has no attribute 'test' - Stack Overflow
When I am injecting the dependencies to the endpoints I always gets Provide object not actual class object due to this code always...
Read more >
Wiring — Dependency Injector 4.41.0 documentation
Wiring¶. Wiring feature provides a way to inject container providers into the functions and methods. To use wiring you need: Place @inject decorator....
Read more >
Provide/Inject Have Nothing to Do With Dependency Injection
The dependencies aren't created by the object itself. Instead they are injected into the object when the program is running. These dependencies are...
Read more >
Dependency injection guidelines - .NET | Microsoft Learn
This article provides general guidelines and best practices for implementing dependency injection in .NET applications.
Read more >
Understanding dependency injection - Endjin
Dependency injection is the principle that the objects which a class needs should be 'injected' into it, not created inside it.
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