Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Nested dependency resolving issue #36

Open
pk-r opened this issue Jan 26, 2019 · 4 comments
Open

Nested dependency resolving issue #36

pk-r opened this issue Jan 26, 2019 · 4 comments

Comments

@pk-r
Copy link

pk-r commented Jan 26, 2019

Hi there
I'm having a difficulty using pinject when I'm trying to resolve a dependency which has inner nested dependencies as well.

import pinject

class User:

    def __init__(self, user_id=None, username=None):
        self.user_id = user_id
        self.username = username

    def get_user_id(self):
        return self.user_id

    def get_username(self):
        return self.username

    def __str__(self):
        return str(self.__dict__)

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

class QueueUserRepository(BaseUserRepository):

    def __init__(self, data_provider):
        self.data_provider = data_provider

    def get_user(self):
        user_data = self.data_provider.get_data('user')
        return User(user_data['user_id'], user_data['username'])

    def get_users_batch(self):
        return UserBatch(self.data_provider.get_data('users'))



class QueueDataProvider:
    class DataDoesNotExistsError(Exception):
        pass

    def __init__(self, provider_data):
        print(provider_data)        
        self.data = self.normalize_data(provider_data)

    @staticmethod
    def normalize_data(data):
        return data

    def get_data(self, key):
        try:
            return self.data[key]
        except KeyError:
            raise self.DataDoesNotExistsError('{} key does not exists'.format(key))

class MyBindingSpec(pinject.BindingSpec):
    def configure(self, bind):
        bind('provider_data', to_instance={'user': {'username': 'super', 'user_id': 'y'}})
        bind('data_provider', to_instance=QueueDataProvider)

class TestQueueUserRepository(unittest.TestCase):
    def setUp(self):
        self.container = pinject.new_object_graph(binding_specs=[MyBindingSpec()])
        
    def test_if_can_get_user(self):
        z = self.container.provide(QueueUserRepository)
        u = User('y', 'super')
        self.assertEqual(r.get_user(), u)

It seems pinject does not care about 'dependency of dependencies' as QueueDataProvider constructor does not seem to be called at all

The above code does not work however when I manually build objects, it works perfectly fine.

As I searched a lot in the documentation, I could not find any proper explanation with this.

Could you please help me with this.

EDIT:
I really tried so hard but I could not get code highlight support in this ...

UPADTE:
Unit tests are added.
To be more clear: QueueDataProvider is not receiving 'provider_data' on creation. I can say the QueueDataProvider's constructor is not called at all as far as I checked.

@huan
Copy link
Contributor

huan commented Jan 27, 2019

I had just helped you highlighted the code block in your issue, you can edit it to see what happened.

Could you provide a unit test to confirm this problem? If you could, then we will be able to look into it later.

@pk-r
Copy link
Author

pk-r commented Jan 27, 2019

  • Updated with unit tests

@birorichard
Copy link

Did you find any solution?

@craigminihan
Copy link

FWIW I think you need the following:

bind('data_provider', to_class=QueueDataProvider)

to replace bind('data_provider', to_instance=QueueDataProvider)

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

No branches or pull requests

4 participants