Skip to content

Factory for TypeVar type can't be used for a specific type #527

Open

Description

@Fatal1ty

If we try to use a factory for TypeVar type T without a wrapper Something[T] we will get dishka.exceptions.NoFactoryError. Slightly modified example from the documentation shows the problem:

from typing import TypeVar

from dishka import make_container, Provider, provide, Scope

T = TypeVar("T", bound=int)


class MyProvider(Provider):
    @provide(scope=Scope.APP)
    def make_a(self, t: type[T]) -> T:
        print("Requested type", t)
        return 42


container = make_container(MyProvider())
container.get(int)
Traceback (most recent call last):
  File "xxx/test.py", line 16, in <module>
    container.get(int)
  File "xxx/python3.12/site-packages/dishka/container.py", line 164, in get
    return self._get_unlocked(key)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "xxx/python3.12/site-packages/dishka/container.py", line 195, in _get_unlocked
    return self.parent_container._get(key)  # noqa: SLF001
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "xxx/python3.12/site-packages/dishka/container.py", line 174, in _get
    return self._get_unlocked(key)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "xxx/python3.12/site-packages/dishka/container.py", line 189, in _get_unlocked
    raise NoFactoryError(
dishka.exceptions.NoFactoryError: Cannot find factory for (int, component='', scope=Scope.APP). Check scopes in your providers. It is missing or has invalid scope.

Process finished with exit code 1

I expected to get 42.

Activity

  1. Tishka17 commented on Aug 23, 2025

    @Tishka17
    Member

    Looks like solving this can highly affect performance when accessing parent scopes. We need a faster way to distinguish objects with incorrect scope and absent ones

  2. Tishka17 commented on Sep 13, 2025

    @Tishka17
    Member

    Probably related to implementation of #543

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions