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

[Bug] Problems with di built-in generics #262

Open
owakira opened this issue Nov 24, 2024 · 2 comments
Open

[Bug] Problems with di built-in generics #262

owakira opened this issue Nov 24, 2024 · 2 comments

Comments

@owakira
Copy link

owakira commented Nov 24, 2024

Describe the bug
I found an interesting bug: di doesn't work with built-in generics.

class Client:
    pass


class Service:
    def __init__(self, clients: list[Client]) -> None:
        self._clients = clients


def _get_clients() -> list[Client]:
    return [Client() for _ in range(3)]


def configure_di(app: Sanic) -> None:
    app.ext.add_dependency(list[Client], _get_clients)
    app.ext.add_dependency(Service)

It throws the following error:

File "/usr/local/lib/python3.11/site-packages/sanic_ext/extensions/injection/injector.py", line 37, in finalize_injections
    injection_registry.finalize(app, constant_registry, router_types)
File "/usr/local/lib/python3.11/site-packages/sanic_ext/extensions/injection/registry.py", line 42, in finalize
    constructor.prepare(
File "/usr/local/lib/python3.11/site-packages/sanic_ext/extensions/injection/constructor.py", line 111, in prepare
    raise InitError(
sanic_ext.exceptions.InitError: Unable to resolve dependencies for 'Service'. Could not find the following dependencies:
  - clients: list[src.di.Client].
Make sure the dependencies are declared using ext.injection. See https://sanicframework.org/en/plugins/sanic-ext/injection.html#injecting-services for more details.
Not all workers acknowledged a successful startup. Shutting down.

Environment (please complete the following information):

  • OS: [e.g. Ubuntu] MacOS Sequoia
  • Version [e.g. 0.5.3] Python 3.11, sanic 22.12.0, sanic-ext 23.6.0
@owakira
Copy link
Author

owakira commented Nov 24, 2024

I found the issue in sanic_ext/extensions/injection/constructor.py:

if not isclass(annotation):
    missing.append((param, annotation))
    continue

The condition for list[Client] will always be True.
It is necessary to add a new check for built-in generics.

@owakira
Copy link
Author

owakira commented Nov 24, 2024

Created pull request
#263

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