Skip to content

Bug: Type error: scoped_session[Session] incompatible with SQLAlchemyFactory.__session__ #730

@idan-rahamim-lendbuzz

Description

@idan-rahamim-lendbuzz

Description

When using scoped_session[Session] with SQLAlchemyFactory, mypy reports a type incompatibility error:

error: Incompatible types in assignment (expression has type "scoped_session[Session]", base class "SQLAlchemyFactory" defined the type as "Session | _SessionMaker[Session] | None")  [assignment]

Expected Behavior

scoped_session[Session] should be accepted as a valid session type since it's a SQLAlchemy session wrapper that implements the same interface as Session and is commonly used in applications.

Steps to reproduce

from sqlalchemy.orm import Session, scoped_session, sessionmaker
from sqlalchemy import create_engine
from polyfactory.factories.sqlalchemy_factory import SQLAlchemyFactory, T


engine = create_engine(DB_URL)
global_session = scoped_session(sessionmaker(bind=engine))


class BaseModelFactory(SQLAlchemyFactory[T]):
    __is_base_factory__ = True
    __session__ = global_session # Type error here

Screenshots

Image

Release Version

2.21.0

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