Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing: Correctly set types when getting Record fields #1210

Open
galah92 opened this issue Nov 5, 2024 · 0 comments
Open

Typing: Correctly set types when getting Record fields #1210

galah92 opened this issue Nov 5, 2024 · 0 comments

Comments

@galah92
Copy link

galah92 commented Nov 5, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant