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

TypeError when using ClassAssistedBuilder with @inject #63

Closed
davidparsson opened this issue Apr 10, 2017 · 4 comments
Closed

TypeError when using ClassAssistedBuilder with @inject #63

davidparsson opened this issue Apr 10, 2017 · 4 comments

Comments

@davidparsson
Copy link
Collaborator

I've been trying to use the ClassAssistedBuilder and the AssistedBuilder as described in the docs, but I'm getting the following exception:

Traceback (most recent call last):
  File "reproduce.py", line 9, in <module>
    class NeedsUserUpdater(object):
  File "reproduce.py", line 11, in NeedsUserUpdater
    def __init__(self, builder: ClassAssistedBuilder[UserUpdater]):
  File "/Users/me/injector-reproduce/.venvs/3.5/lib/python3.5/site-packages/injector.py", line 1093, in inject
    bindings = _infer_injected_bindings(function)
  File "/Users/me/injector-reproduce/.venvs/3.5/lib/python3.5/site-packages/injector.py", line 967, in _infer_injected_bindings
    if _is_specialization(v, Union):
  File "/Users/me/injector-reproduce/.venvs/3.5/lib/python3.5/site-packages/injector.py", line 490, in _is_specialization
    (cls.__origin__ is generic_class or issubclass(cls.__origin__, generic_class))
  File "/usr/local/opt/pyenv/versions/3.5.3/lib/python3.5/typing.py", line 770, in __subclasscheck__
    raise TypeError("Unions cannot be used with issubclass().")
TypeError: Unions cannot be used with issubclass().

This is the code I'm running:

#!/usr/bin/env python3
from injector import ClassAssistedBuilder, Injector, inject


class UserUpdater:
    def __init__(self, user):
        self._user = user

class NeedsUserUpdater(object):
    @inject
    def __init__(self, builder: ClassAssistedBuilder[UserUpdater]):
        self.updater_builder = builder

    def method(self):
        updater = self.updater_builder.build(user=None)


injector = Injector()
instance = injector.get(NeedsUserUpdater)
instance.method()

Am I doing something wrong, or is it not working as intended?

@davidparsson davidparsson changed the title TypeError when using ClassAssistedBuilder TypeError when using ClassAssistedBuilder with @inject Apr 10, 2017
@davidparsson
Copy link
Collaborator Author

davidparsson commented Apr 10, 2017

It however works when the @inject decorator is removed, and I do this at the end:

injector = Injector()
builder = injector.get(ClassAssistedBuilder[UserUpdater])
instance = NeedsUserUpdater(builder)
instance.method()

@ghost
Copy link

ghost commented Apr 11, 2017

I believe that PR #61 fixes this, but it has not yet been released.

@davidparsson
Copy link
Collaborator Author

davidparsson commented Apr 11, 2017 via email

@alecthomas
Copy link
Collaborator

Just released 0.12.1 which includes this fix.

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

2 participants