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