Skip to content

Bug: TypeError when init SQLAlchemyAsyncQueryRepository #534

Closed

Description

@HairlessVillager

Description

When using providers.create_service_dependencies(SomeServiceUsingAsyncQueryRepository, "query_service") to create dependencies and using them in a route method, calling this method will result in an error: TypeError: object.__init__() takes exactly one argument (the instance to initialize).

MCVE

from __future__ import annotations

from datetime import datetime

from advanced_alchemy import base
from advanced_alchemy.extensions.litestar import (
    AsyncSessionConfig,
    EngineConfig,
    SQLAlchemyAsyncConfig,
    SQLAlchemyPlugin,
    providers,
    repository,
    service,
)
from litestar import Litestar, get
from sqlalchemy import JSON
from sqlalchemy.orm import Mapped, mapped_column

sqlalchemy_config = SQLAlchemyAsyncConfig(
    connection_string="sqlite+aiosqlite:///:memory:",
    before_send_handler="autocommit",
    session_config=AsyncSessionConfig(expire_on_commit=False),
    create_all=True,
    engine_config=EngineConfig(echo=True),
)


class LogRecord(base.UUIDv7Base):
    timestamp: Mapped[datetime] = mapped_column(index=True)
    content: Mapped[str] = mapped_column(JSON)


class LogQueryService(service.SQLAlchemyAsyncRepositoryService[LogRecord]):
    repository_type = repository.SQLAlchemyAsyncQueryRepository


@get()
async def log_list(
    log_query_service: LogQueryService,
) -> None:
    return None


app = Litestar(
    [log_list],
    plugins=[SQLAlchemyPlugin(config=sqlalchemy_config)],
    dependencies=providers.create_service_dependencies(
        LogQueryService, "log_query_service"
    ),
)

Steps to reproduce

  1. Copy this code as app.py
  2. Run litestar run --debug
  3. Run curl http://127.0.0.1:8000/
  4. See error

Logs

Traceback (most recent call last):
  File "C:\Users\hlsvillager\Desktop\mvce\.venv\Lib\site-packages\litestar\middleware\_internal\exceptions\middleware.py", line 158, in __call__
    await self.app(scope, receive, capture_response_started)
  File "C:\Users\hlsvillager\Desktop\mvce\.venv\Lib\site-packages\litestar\_asgi\asgi_router.py", line 100, in __call__
    await asgi_app(scope, receive, send)
  File "C:\Users\hlsvillager\Desktop\mvce\.venv\Lib\site-packages\litestar\routes\http.py", line 81, in handle
    response = await self._get_response_for_request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        scope=scope, request=request, route_handler=route_handler, parameter_model=parameter_model
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\hlsvillager\Desktop\mvce\.venv\Lib\site-packages\litestar\routes\http.py", line 133, in _get_response_for_request
    return await self._call_handler_function(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        scope=scope, request=request, parameter_model=parameter_model, route_handler=route_handler
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\hlsvillager\Desktop\mvce\.venv\Lib\site-packages\litestar\routes\http.py", line 171, in _call_handler_function
    cleanup_group = await parameter_model.resolve_dependencies(request, kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hlsvillager\Desktop\mvce\.venv\Lib\site-packages\litestar\_kwargs\kwargs_model.py", line 395, in resolve_dependencies
    await resolve_dependency(next(iter(batch)), connection, kwargs, cleanup_group)
  File "C:\Users\hlsvillager\Desktop\mvce\.venv\Lib\site-packages\litestar\_kwargs\dependencies.py", line 72, in resolve_dependency
    value = await async_next(value)
            ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hlsvillager\Desktop\mvce\.venv\Lib\site-packages\advanced_alchemy\extensions\litestar\providers.py", line 214, in provide_service_async
    async with service_class.new(  # type: ignore[union-attr]
               ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        session=db_session,  # type: ignore[arg-type]
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<6 lines>...
        count_with_window_function=count_with_window_function,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ) as service:
    ^
  File "C:\Users\hlsvillager\AppData\Roaming\uv\python\cpython-3.13.2-windows-x86_64-none\Lib\contextlib.py", line 214, in __aenter__
    return await anext(self.gen)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hlsvillager\Desktop\mvce\.venv\Lib\site-packages\advanced_alchemy\service\_async.py", line 550, in new
    yield cls(
          ~~~^
        statement=statement,
        ^^^^^^^^^^^^^^^^^^^^
    ...<5 lines>...
        count_with_window_function=count_with_window_function,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\hlsvillager\Desktop\mvce\.venv\Lib\site-packages\advanced_alchemy\service\_async.py", line 145, in __init__
    self._repository_instance: SQLAlchemyAsyncRepositoryT = self.repository_type(  # type: ignore[assignment]
                                                            ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        session=session,
        ^^^^^^^^^^^^^^^^
    ...<11 lines>...
        **repo_kwargs,
        ^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\hlsvillager\Desktop\mvce\.venv\Lib\site-packages\advanced_alchemy\repository\_async.py", line 2285, in __init__
    super().__init__(**kwargs)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^
TypeError: object.__init__() takes exactly one argument (the instance to initialize)

Package Version

$ uv pip list
Package            Version
------------------ --------
advanced-alchemy   1.6.2   
aiosqlite          0.21.0  
alembic            1.16.5  
anyio              4.10.0  
certifi            2025.8.3
click              8.2.1   
colorama           0.4.6   
editorconfig       0.17.1  
faker              37.6.0  
fast-query-parsers 1.0.3   
greenlet           3.2.4   
h11                0.16.0  
httpcore           1.0.9   
httptools          0.6.4   
httpx              0.28.1  
idna               3.10    
jinja2             3.1.6   
jsbeautifier       1.15.4
litestar           2.17.0
litestar-htmx      0.5.0
mako               1.3.10
markdown-it-py     4.0.0
markupsafe         3.0.2
mdurl              0.1.2
msgspec            0.19.0
multidict          6.6.4
multipart          1.3.0
polyfactory        2.22.2
pygments           2.19.2
python-dotenv      1.1.1
pyyaml             6.0.2
rich               14.1.0
rich-click         1.8.9
six                1.17.0
sniffio            1.3.1
sqlalchemy         2.0.43
typing-extensions  4.15.0
tzdata             2025.2
uvicorn            0.35.0
watchfiles         1.1.0
websockets         15.0.1

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)
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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions