[Python] Type hinting sqlalchemy query result

Discussão em 'Python' iniciado por Stack, Março 19, 2025.

  1. Stack

    Stack Membro Participativo

    I can't figure out what kind of object a sqlalchemy query returns.

    entries = session.query(Foo.id, Foo.date).all()


    The type of each object in entries seems to be sqlalchemy.util._collections.result, but a quick from sqlalchemy.util._collections import result in a python interpreter raises an ImportError.

    What I'm ultimately trying to do is to type hint this function:

    def my_super_function(session: Session) -> ???:
    entries = session.query(Foo.id, Foo.date).all()
    return entries


    What should I put in place of ???? mypy (in this case) seems to be fine with List[Tuple[int, str]] because yes indeed I can access my entries like if they were tuples, but i can also access them with entry.date, for example.

    Continue reading...

Compartilhe esta Página