[Python] Python injector doesn't work for NewType based on tuple[str, str]

Discussão em 'Python' iniciado por Stack, Agosto 25, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use https://github.com/python-injector/injector to inject a tuple[str, str], but it doesn't work.

    import injector
    from typing import NewType

    Foo = NewType("Foo", tuple[str, str])


    class MyModule(injector.Module):
    def configure(self, binder: injector.Binder) -> None:
    binder.bind(Foo, Foo(("x", "y")))


    injector.Injector(modules=[MyModule]).get(Foo)


    leads to error

    Traceback (most recent call last):
    File "/tmp/injector_demo/scratch.py", line 12, in <module>
    injector.Injector(modules=[MyModule]).get(Foo)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/injector_demo/.venv/lib/python3.12/site-packages/injector/__init__.py", line 925, in __init__
    self.binder.install(module)
    File "/tmp/injector_demo/.venv/lib/python3.12/site-packages/injector/__init__.py", line 564, in install
    instance(self)
    File "/tmp/injector_demo/.venv/lib/python3.12/site-packages/injector/__init__.py", line 871, in __call__
    self.configure(binder)
    File "/tmp/injector_demo/scratch.py", line 9, in configure
    binder.bind(Foo, Foo(("x", "y")))
    File "/tmp/injector_demo/.venv/lib/python3.12/site-packages/injector/__init__.py", line 465, in bind
    self._bindings[interface] = self.create_binding(interface, to, scope)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/injector_demo/.venv/lib/python3.12/site-packages/injector/__init__.py", line 569, in create_binding
    provider = self.provider_for(interface, to)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/injector_demo/.venv/lib/python3.12/site-packages/injector/__init__.py", line 631, in provider_for
    raise UnknownProvider('couldn\'t determine provider for %r to %r' % (interface, to))
    injector.UnknownProvider: couldn't determine provider for __main__.Foo to ('x', 'y')


    I've been injecting NewType('Bar', str) many times and they all work, so it has to be due to something special related to tuple. I wonder if anyone has insights into why.

    Continue reading...

Compartilhe esta Página