From: Daniele Varrazzo Date: Sun, 27 Mar 2022 20:04:47 +0000 (+0200) Subject: feat: add pretty repr to Pipeline objects X-Git-Tag: 3.1~146^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eb80f78c8cb96c0cbf845ecc615550c2112a5abc;p=thirdparty%2Fpsycopg.git feat: add pretty repr to Pipeline objects --- diff --git a/psycopg/psycopg/_pipeline.py b/psycopg/psycopg/_pipeline.py index 8a7456910..edc1485a8 100644 --- a/psycopg/psycopg/_pipeline.py +++ b/psycopg/psycopg/_pipeline.py @@ -51,6 +51,11 @@ class BasePipeline: self.command_queue = Deque[PipelineCommand]() self.result_queue = Deque[PendingResult]() + def __repr__(self) -> str: + cls = f"{self.__class__.__module__}.{self.__class__.__qualname__}" + info = pq.misc.connection_summary(self._conn.pgconn) + return f"<{cls} {info} at 0x{id(self):x}>" + @property def status(self) -> pq.PipelineStatus: return pq.PipelineStatus(self.pgconn.pipeline_status) @@ -143,6 +148,7 @@ class BasePipeline: class Pipeline(BasePipeline): """Handler for connection in pipeline mode.""" + __module__ = "psycopg" _conn: "Connection[Any]" def __init__(self, conn: "Connection[Any]") -> None: @@ -176,6 +182,7 @@ class Pipeline(BasePipeline): class AsyncPipeline(BasePipeline): """Handler for async connection in pipeline mode.""" + __module__ = "psycopg" _conn: "AsyncConnection[Any]" def __init__(self, conn: "AsyncConnection[Any]") -> None: diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py index 0079aa66c..cfbfbad11 100644 --- a/tests/test_pipeline.py +++ b/tests/test_pipeline.py @@ -10,6 +10,15 @@ from psycopg import errors as e pytestmark = pytest.mark.libpq(">= 14") +def test_repr(conn): + with conn.pipeline() as p: + assert "psycopg.Pipeline" in repr(p) + assert "[IDLE]" in repr(p) + + conn.close() + assert "[BAD]" in repr(p) + + def test_pipeline_status(conn: psycopg.Connection[Any]) -> None: assert conn._pipeline is None with conn.pipeline() as p: diff --git a/tests/test_pipeline_async.py b/tests/test_pipeline_async.py index c306d63c8..67265c7ee 100644 --- a/tests/test_pipeline_async.py +++ b/tests/test_pipeline_async.py @@ -13,6 +13,15 @@ pytestmark = [ ] +async def test_repr(aconn): + async with aconn.pipeline() as p: + assert "psycopg.AsyncPipeline" in repr(p) + assert "[IDLE]" in repr(p) + + await aconn.close() + assert "[BAD]" in repr(p) + + async def test_pipeline_status(aconn: psycopg.AsyncConnection[Any]) -> None: assert aconn._pipeline is None async with aconn.pipeline() as p: