Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register classes programmatically #59

Open
dbalabka opened this issue May 15, 2020 · 2 comments
Open

Register classes programmatically #59

dbalabka opened this issue May 15, 2020 · 2 comments

Comments

@dbalabka
Copy link
Contributor

dbalabka commented May 15, 2020

Is it possible to register classes in the same way as @inject.autoparams provides but programmatically inside configuration callback?

For example:

def configure(binder: Binder):
    binder.bind_to_constructor(SomeClass, inject.autoparams()(SomeClass.__init__))

inject.configure(configure)

This example will throw an error:

TypeError: __init__() missing 1 required positional argument: 'self'
dbalabka added a commit to dbalabka/python-inject that referenced this issue May 15, 2020
@dbalabka
Copy link
Contributor Author

dbalabka commented May 15, 2020

@ivankorobkov in PR #60 I have implemented that possibility.

Now it is possible in following way:

def configure(binder: Binder):
    binder.bind_to_constructor(SomeClass, inject.autoparams()(SomeClass))

inject.configure(configure)

@dbalabka
Copy link
Contributor Author

dbalabka commented May 15, 2020

@ivankorobkov it would be great to have the possibility to use inject.autoparams as a class decorator. Probably it will require separate decorator for classes.

The following test will fail because classes AnotherClass and SomeClass became decorated and are functions:

    def test_class_support_in_autoparams_decorator(self):
        @autoparams()
        class AnotherClass:
            pass

        @autoparams()
        class SomeClass:
            def __init__(self, another_object: AnotherClass):
                self.another_object = another_object

        inject.configure()
        some_object = inject.instance(SomeClass)
        assert isinstance(some_object, SomeClass)
        assert isinstance(some_object.another_object, AnotherClass)

To properly solve this issue we should return child class of AnotherClass and SomeClass decorator instead.

ivankorobkov added a commit that referenced this issue May 18, 2020
Add support for classes in autoparams (#59)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant