AsyncGenerator instead of AsyncSession in pytest, but why

I have fixtures

from typing import AsyncGenerator, Generator

@pytest.fixture(scope='session')
def init_session(config: Config) -> None:
init_db(config)

@pytest.fixture(scope="session")
async def session(init_session: None) -> AsyncGenerator[AsyncSession, None]:
async with get_session() as session:
yield session

@pytest.fixture(scope="session")
def user_repository(session: AsyncSession) -> UserRepository:
return UserRepository(session)

And I have test

import datetime

import pytest
from sqlalchemy.ext.asyncio import AsyncSession

import UserStatus
import UserRepository
import RawUser
from tests.test_utils import random_str
from tests.test_utils.user import convert_user_model_to_raw_user, get_user_by_id

TEST_USER_LOGIN = random_str()
TEST_USER_PASSWORD_HASH = random_str()
TEST_USER_STATUS = UserStatus.ACTIVE
TEST_USER_CREATED_AT = datetime.datetime.now(datetime.UTC)
TEST_USER_UPDATED_AT = datetime.datetime.now(datetime.UTC)

@pytest.mark.asyncio
async def test_create_user(user_repository: UserRepository, session: AsyncSession) -> None:
    raw_user = RawUser(
        login=TEST_USER_LOGIN,
        password_hash=TEST_USER_PASSWORD_HASH,
        status=TEST_USER_STATUS,
        created_at=TEST_USER_CREATED_AT,
        updated_at=TEST_USER_UPDATED_AT,
    )

    user_id = await user_repository.create_user(raw_user)

    user_model = await get_user_by_id(session, user_id)
    user_from_db = convert_user_model_to_raw_user(user_model)
    assert user_from_db == raw_user

Function create_user

from sqlalchemy import insert
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.ext.asyncio import AsyncSession

import UserModel
import RawUser


class UserRepository:
    def __init__(self, session: AsyncSession) -> None:
        self.session = session

    async def create_user(self, raw_user: RawUser) -> int:
        try:
            result = await self.session.execute(
                insert(UserModel)
                .values(
                    login=raw_user.login,
                    password_hash=raw_user.password_hash,
                    status=raw_user.status,
                    created_at=raw_user.created_at,
                    updated_at=raw_user.updated_at,
                )
                .returning(UserModel.id)
            )

            user_id = result.scalar()

            if user_id is None:
                raise RuntimeError("Failed to retrieve user ID after insertion.")

            await self.session.commit()

            return user_id
        except SQLAlchemyError as e:
            await self.session.rollback()
            raise RuntimeError(f"Failed to create user: {e}")

For some reason I get AsyncGenerator instead of AsyncSession. I’ve tried everything that comes to mind, but I don’t know how to solve this problem. I would be very grateful for your help.
I tried making fixtures synchronous, but it doesn’t work. The only way I got the test to complete was when I used a loop inside it to pull the session out of the generator, but I was told that this option is not suitable.
AttributeError: ‘async_generator’ object has no attribute ‘execute’

New contributor

Andi swim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật