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

The case of multiple inheritance #48

Open
mehmetgiritli opened this issue Dec 7, 2024 · 3 comments
Open

The case of multiple inheritance #48

mehmetgiritli opened this issue Dec 7, 2024 · 3 comments

Comments

@mehmetgiritli
Copy link

Hi,

I realised that it is not possible, as far as I can see, to inject a service which inherits multiple classes.

@abstract
class A(ABC):
...

@abstract
class B(ABC):
...

@service
class C(A, B):
...

As C implements both A and B, I expect instance of C to be injected when either A or B is requested:

@service
class Something:
    def __init__(c: Annotated[A, Inject()])
...

@service
class SomethingElse:
    def __init__(c: Annotated[B, Inject()])

However, this does not seem to work:

...
wireup.errors.UnknownQualifiedServiceRequestedError: Cannot instantiate concrete class for <class ....> as qualifier 'None' is unknown. Available qualifiers: [].

I'm not sure if this is me doing something wrong or if this is an enhancement.

@maldoinc
Copy link
Owner

maldoinc commented Dec 7, 2024

The container only resolves the first parent class when registering a class. It should be possible to iterate over all bases and link all of them that are abstract with the current impl being registered. If you want to contribute that the relevant code is in ServiceRegistry.register_service method.

For the current version you can just register A and B as regular services via factories and have the factory request and inject C instead.

class A(ABC): ...


class B(ABC): ...


@service
class C(A, B): ...


@service
def a_factory(c: C) -> A:
    return c


@service
def b_factory(c: C) -> B:
    return c

@mehmetgiritli
Copy link
Author

mehmetgiritli commented Dec 7, 2024

Thanks for the response.

I tried factory solution already but I get the same error. Then I realised that I still kept @ abstract on A and B. Removing @ abstract made it work. Just dropping the note here in case somebody else gets hit by it.

I'm hoping to contribute the feature soon with a PR.

Many thanks again.

@maldoinc
Copy link
Owner

maldoinc commented Dec 8, 2024

Also FYI: In services you don't need to annotate dependencies with Annotated[T, Inject()]. That is only required in fastapi views due to how the framework is designed.

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