mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-20 06:35:46 +02:00
* `strict=False` is the default, pyupgrade to min version 3.10 adds this to be explicit w/ behavior
24 lines
731 B
Python
24 lines
731 B
Python
"""Shared async utility functions for the Postgres checkpoint & storage classes."""
|
|
|
|
from collections.abc import AsyncIterator
|
|
from contextlib import asynccontextmanager
|
|
|
|
from psycopg import AsyncConnection
|
|
from psycopg.rows import DictRow
|
|
from psycopg_pool import AsyncConnectionPool
|
|
|
|
Conn = AsyncConnection[DictRow] | AsyncConnectionPool[AsyncConnection[DictRow]]
|
|
|
|
|
|
@asynccontextmanager
|
|
async def get_connection(
|
|
conn: Conn,
|
|
) -> AsyncIterator[AsyncConnection[DictRow]]:
|
|
if isinstance(conn, AsyncConnection):
|
|
yield conn
|
|
elif isinstance(conn, AsyncConnectionPool):
|
|
async with conn.connection() as conn:
|
|
yield conn
|
|
else:
|
|
raise TypeError(f"Invalid connection type: {type(conn)}")
|