1+ # pyright: reportPrivateUsage=false
12"""Psycopg LISTEN/NOTIFY and hybrid event backends."""
23
34import contextlib
@@ -55,7 +56,8 @@ async def publish_async(
5556 ) -> str :
5657 event_id = uuid .uuid4 ().hex
5758 envelope = self ._encode_payload (event_id , payload , metadata )
58- async with self ._config .provide_session () as driver :
59+ session_cm = self ._config .provide_session ()
60+ async with session_cm as driver : # type: ignore[union-attr]
5961 await driver .execute (SQL ("SELECT pg_notify(:channel, :payload)" , {"channel" : channel , "payload" : envelope }))
6062 await driver .commit ()
6163 self ._runtime .increment_metric ("events.publish.native" )
@@ -64,7 +66,8 @@ async def publish_async(
6466 def publish_sync (self , channel : str , payload : "dict[str, Any]" , metadata : "dict[str, Any] | None" = None ) -> str :
6567 event_id = uuid .uuid4 ().hex
6668 envelope = self ._encode_payload (event_id , payload , metadata )
67- with self ._config .provide_session () as driver :
69+ session_cm = self ._config .provide_session ()
70+ with session_cm as driver : # type: ignore[union-attr]
6871 driver .execute (SQL ("SELECT pg_notify(:channel, :payload)" , {"channel" : channel , "payload" : envelope }))
6972 driver .commit ()
7073 self ._runtime .increment_metric ("events.publish.native" )
@@ -122,9 +125,10 @@ async def _ensure_async_listener(self, channel: str) -> Any:
122125 if self ._listen_connection_async is None :
123126 validated_channel = normalize_event_channel_name (channel )
124127 self ._listen_connection_async_cm = self ._config .provide_connection ()
125- self ._listen_connection_async = await self ._listen_connection_async_cm .__aenter__ ()
126- await self ._listen_connection_async .set_autocommit (True )
127- await self ._listen_connection_async .execute (f"LISTEN { validated_channel } " )
128+ self ._listen_connection_async = await self ._listen_connection_async_cm .__aenter__ () # type: ignore[union-attr]
129+ if self ._listen_connection_async is not None :
130+ await self ._listen_connection_async .set_autocommit (True )
131+ await self ._listen_connection_async .execute (f"LISTEN { validated_channel } " )
128132 return self ._listen_connection_async
129133
130134 def _ensure_sync_listener (self , channel : str ) -> Any :
@@ -139,9 +143,10 @@ def _ensure_sync_listener(self, channel: str) -> Any:
139143 if self ._listen_connection_sync is None :
140144 validated_channel = normalize_event_channel_name (channel )
141145 self ._listen_connection_sync_cm = self ._config .provide_connection ()
142- self ._listen_connection_sync = self ._listen_connection_sync_cm .__enter__ ()
143- self ._listen_connection_sync .autocommit = True
144- self ._listen_connection_sync .execute (f"LISTEN { validated_channel } " )
146+ self ._listen_connection_sync = self ._listen_connection_sync_cm .__enter__ () # type: ignore[union-attr]
147+ if self ._listen_connection_sync is not None :
148+ self ._listen_connection_sync .autocommit = True
149+ self ._listen_connection_sync .execute (f"LISTEN { validated_channel } " )
145150 return self ._listen_connection_sync
146151
147152 async def shutdown_async (self ) -> None :
@@ -280,9 +285,10 @@ async def _ensure_async_listener(self, channel: str) -> Any:
280285 if self ._listen_connection_async is None :
281286 validated_channel = normalize_event_channel_name (channel )
282287 self ._listen_connection_async_cm = self ._config .provide_connection ()
283- self ._listen_connection_async = await self ._listen_connection_async_cm .__aenter__ ()
284- await self ._listen_connection_async .set_autocommit (True )
285- await self ._listen_connection_async .execute (f"LISTEN { validated_channel } " )
288+ self ._listen_connection_async = await self ._listen_connection_async_cm .__aenter__ () # type: ignore[union-attr]
289+ if self ._listen_connection_async is not None :
290+ await self ._listen_connection_async .set_autocommit (True )
291+ await self ._listen_connection_async .execute (f"LISTEN { validated_channel } " )
286292 return self ._listen_connection_async
287293
288294 def _ensure_sync_listener (self , channel : str ) -> Any :
@@ -297,9 +303,10 @@ def _ensure_sync_listener(self, channel: str) -> Any:
297303 if self ._listen_connection_sync is None :
298304 validated_channel = normalize_event_channel_name (channel )
299305 self ._listen_connection_sync_cm = self ._config .provide_connection ()
300- self ._listen_connection_sync = self ._listen_connection_sync_cm .__enter__ ()
301- self ._listen_connection_sync .autocommit = True
302- self ._listen_connection_sync .execute (f"LISTEN { validated_channel } " )
306+ self ._listen_connection_sync = self ._listen_connection_sync_cm .__enter__ () # type: ignore[union-attr]
307+ if self ._listen_connection_sync is not None :
308+ self ._listen_connection_sync .autocommit = True
309+ self ._listen_connection_sync .execute (f"LISTEN { validated_channel } " )
303310 return self ._listen_connection_sync
304311
305312 async def ack_async (self , event_id : str ) -> None :
@@ -364,7 +371,8 @@ async def _publish_durable_async(
364371 """
365372 now = datetime .now (timezone .utc )
366373 queue = self ._get_table_queue ()
367- async with self ._config .provide_session () as driver :
374+ session_cm = self ._config .provide_session ()
375+ async with session_cm as driver : # type: ignore[union-attr]
368376 await driver .execute (
369377 SQL (
370378 queue ._upsert_sql ,
@@ -403,7 +411,8 @@ def _publish_durable_sync(
403411 """
404412 now = datetime .now (timezone .utc )
405413 queue = self ._get_table_queue ()
406- with self ._config .provide_session () as driver :
414+ session_cm = self ._config .provide_session ()
415+ with session_cm as driver : # type: ignore[union-attr]
407416 driver .execute (
408417 SQL (
409418 queue ._upsert_sql ,
0 commit comments