Skip to content

Typing: Correctly set types when getting Record fields #1210

Open
@galah92

Description

@galah92

Consider the following:

import asyncio

import asyncpg


async def main() -> int:
    connection_string = "postgresql://postgres:postgres@127.0.0.1:5432/postgres"
    conn = await asyncpg.connect(connection_string)
    result = await conn.fetch("SELECT 1 + 1 AS num", record_class=Row)
    row = result[0]
    num = row["num"]
    print(type(num))
    return num  # mypy error: Returning Any from function declared to return "int"


class Row(asyncpg.Record):
    num: int


if __name__ == "__main__":
    asyncio.run(main())

In essence, the type of num is Any instead of int. I tried to make Row inherit from TypedDict or use a dataclass, both failed.
I'll also be happy to help develop this feature with some general guidance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions