Skip to content

Instantly share code, notes, and snippets.

@deanm0000
Last active July 16, 2025 19:31
Show Gist options
  • Select an option

  • Save deanm0000/5380b550acff6b16811e8f7915deeced to your computer and use it in GitHub Desktop.

Select an option

Save deanm0000/5380b550acff6b16811e8f7915deeced to your computer and use it in GitHub Desktop.
psycopg3 fetchone typer
from __future__ import annotations
from typing import TYPE_CHECKING, Any, TypeVar, cast, overload
if TYPE_CHECKING:
from psycopg import AsyncCursor
T1 = TypeVar("T1")
T2 = TypeVar("T2")
T3 = TypeVar("T3")
T4 = TypeVar("T4")
T5 = TypeVar("T5")
T6 = TypeVar("T6")
T7 = TypeVar("T7")
T8 = TypeVar("T8")
T9 = TypeVar("T9")
T10 = TypeVar("T10")
@overload
async def fetchone(cur: AsyncCursor): ...
@overload
async def fetchone(cur: AsyncCursor, types1: type[T1], /) -> T1: ...
@overload
async def fetchone(
cur: AsyncCursor, types1: type[T1], types2: type[T2], /
) -> tuple[T1, T2]: ...
@overload
async def fetchone(
cur: AsyncCursor, types1: type[T1], types2: type[T2], types3: type[T3], /
) -> tuple[T1, T2, T3]: ...
@overload
async def fetchone(
cur: AsyncCursor,
types1: type[T1],
types2: type[T2],
types3: type[T3],
types4: type[T4],
/,
) -> tuple[T1, T2, T3, T4]: ...
@overload
async def fetchone(
cur: AsyncCursor,
types1: type[T1],
types2: type[T2],
types3: type[T3],
types4: type[T4],
types5: type[T5],
/,
) -> tuple[T1, T2, T3, T4, T5]: ...
@overload
async def fetchone(
cur: AsyncCursor,
types1: type[T1],
types2: type[T2],
types3: type[T3],
types4: type[T4],
types5: type[T5],
types6: type[T6],
/,
) -> tuple[T1, T2, T3, T4, T5, T6]: ...
@overload
async def fetchone(
cur: AsyncCursor,
types1: type[T1],
types2: type[T2],
types3: type[T3],
types4: type[T4],
types5: type[T5],
types6: type[T6],
types7: type[T7],
/,
) -> tuple[T1, T2, T3, T4, T5, T6, T7]: ...
@overload
async def fetchone(
cur: AsyncCursor,
types1: type[T1],
types2: type[T2],
types3: type[T3],
types4: type[T4],
types5: type[T5],
types6: type[T6],
types7: type[T7],
types8: type[T8],
/,
) -> tuple[T1, T2, T3, T4, T5, T6, T7, T8]: ...
@overload
async def fetchone(
cur: AsyncCursor,
types1: type[T1],
types2: type[T2],
types3: type[T3],
types4: type[T4],
types5: type[T5],
types6: type[T6],
types7: type[T7],
types8: type[T8],
types9: type[T9],
/,
) -> tuple[T1, T2, T3, T4, T5, T6, T7, T8, T9]: ...
@overload
async def fetchone(
cur: AsyncCursor,
types1: type[T1],
types2: type[T2],
types3: type[T3],
types4: type[T4],
types5: type[T5],
types6: type[T6],
types7: type[T7],
types8: type[T8],
types9: type[T9],
types10: type[T10],
/,
) -> tuple[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]: ...
async def fetchone(
cur: AsyncCursor,
*types: type,
) -> tuple[Any, ...] | Any:
row = await cur.fetchone()
assert row is not None
if len(row) == 1:
return row[0]
else:
return row
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment