We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Here's example code with error that was unclear: sqla_provider.py
from dishka import Provider, Scope, provide from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker from sqlalchemy.orm import DeclarativeBase from typing import Protocol, runtime_checkable, Self from contextlib import AbstractAsyncContextManager @runtime_checkable class TransactionManager(Protocol, AbstractAsyncContextManager): ... class Base(DeclarativeBase): """ Основа классов таблиц SQLAlchemy. """ class TransactionManagerSQLA(TransactionManager): def __init__(self, **kwargs): ... @runtime_checkable class Repository(Protocol): transaction: TransactionManager class RepositorySQLA(Repository): def __init__(self, transaction: TransactionManagerSQLA): self.transaction: TransactionManagerSQLA = transaction @staticmethod async def init_db(engine: AsyncEngine) -> None: """ Инициализирует БД. :type engine: Подключение к базе данных. :return: Ничего. """ async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) @staticmethod async def drop_db(engine: AsyncEngine) -> None: """ Удаляет все данные из БД. :type engine: Подключение к базе данных. :return: Ничего. """ async with engine.begin() as conn: await conn.run_sync(Base.metadata.drop_all) class SQLAlchemyProvider(Provider): def __init__(self, engine: AsyncEngine): super().__init__() self.engine: AsyncEngine = engine self.session_maker: async_sessionmaker[ AsyncSession ] = async_sessionmaker( self.engine, expire_on_commit=False ) @provide(scope=Scope.REQUEST) def get_session(self) -> AsyncSession: """ Получает сессию SQLAlchemy. :return: Сессия SQLAlchemy. """ return self.session_maker() @provide(scope=Scope.REQUEST) def get_transaction_manager(self, session: AsyncSession) -> TransactionManagerSQLA: """ Получает менеджер транзакций. :param session: Асинхронная сессия SQLAlchemy. :return: Менеджер транзакции. """ if (transaction_init := session.get_transaction()) is not None: return TransactionManagerSQLA(session, transaction_init) else: return TransactionManagerSQLA(session, session.begin()) @provide(scope=Scope.REQUEST) def get_repository(self) -> Repository: """ Получает репозиторий SQLAlchemy. :return: SQLAlchemy версия репозитория. """ return RepositorySQLA( self.get_transaction_manager( self.get_session() ) ) # Tests fixtures code import pytest import pytest_asyncio # noqa: used as plugin from dishka import Container, make_container from sqlalchemy.ext.asyncio import create_async_engine @pytest.fixture(scope="session") def engine(): return create_async_engine("sqlite+aiosqlite:///:memory:") @pytest.fixture(scope="session") def provider(engine) -> SQLAlchemyProvider: return SQLAlchemyProvider(engine) @pytest.fixture() def container(provider: SQLAlchemyProvider) -> Container: container: Container = make_container(provider) return container @pytest.fixture() def repo(container: Container) -> RepositorySQLA: with container() as request_container: repo: RepositorySQLA = request_container.get(RepositorySQLA) return repo def test(repo): return True
Currently returned error is reported like this:
self = <dishka.container.Container object at 0x00000217149CE260> key = DependencyKey(type_hint=<class 'server.data_storage.sql_implementation.repository_sqla.RepositorySQLA'>, component='') def _get_unlocked(self, key: DependencyKey) -> Any: if key in self._cache: return self._cache[key] compiled = self.registry.get_compiled(key) if not compiled: if not self.parent_container: > raise NoFactoryError(key) E dishka.exceptions.NoFactoryError: Cannot find factory for (RepositorySQLA, component='', scope=Scope.REQUEST). Check scopes in your providers. It is missing or has invalid scope. ..\venv\Lib\site-packages\dishka\container.py:182: NoFactoryError
Desired output: message with note about which data types can be provided that are in same hierarchy can be provided, and suggest using those
The text was updated successfully, but these errors were encountered:
Can I take this issue to work?
Sorry, something went wrong.
ApostolFet
Successfully merging a pull request may close this issue.
Here's example code with error that was unclear:
sqla_provider.py
Currently returned error is reported like this:
Desired output: message with note about which data types can be provided that are in same hierarchy can be provided, and suggest using those
The text was updated successfully, but these errors were encountered: