44import time
55import typing as t
66from collections import deque
7- from typing import Any , Optional , Tuple , Union
7+ from typing import Any
88
99import psqlpy
1010from psqlpy import row_factories
@@ -55,7 +55,7 @@ class AsyncAdapt_psqlpy_cursor(AsyncAdapt_dbapi_cursor):
5555
5656 _adapt_connection : "AsyncAdapt_psqlpy_connection"
5757 _connection : psqlpy .Connection # type: ignore[assignment]
58- _cursor : t .Optional [ t . Any ] # type: ignore[assignment]
58+ _cursor : t .Any | None # type: ignore[assignment]
5959 _awaitable_cursor_close : bool = False
6060
6161 def __init__ (
@@ -66,17 +66,15 @@ def __init__(
6666 self .await_ = adapt_connection .await_
6767 self ._rows : deque [t .Any ] = deque ()
6868 self ._cursor = None
69- self ._description : t . Optional [ t . List [t .Tuple [ t . Any , ...]]] = None
69+ self ._description : list [ tuple [t .Any , ...]] | None = None
7070 self ._arraysize = 1
7171 self ._rowcount = - 1
7272 self ._invalidate_schema_cache_asof = 0
7373
7474 async def _prepare_execute (
7575 self ,
7676 querystring : str ,
77- parameters : t .Union [
78- t .Sequence [t .Any ], t .Mapping [str , Any ], None
79- ] = None ,
77+ parameters : t .Sequence [t .Any ] | t .Mapping [str , Any ] | None = None ,
8078 ) -> None :
8179 """Execute a prepared statement.
8280
@@ -175,10 +173,8 @@ async def _prepare_execute(
175173
176174 def _process_parameters (
177175 self ,
178- parameters : t .Union [
179- t .Sequence [t .Any ], t .Mapping [str , Any ], None
180- ] = None ,
181- ) -> t .Union [t .Sequence [t .Any ], t .Mapping [str , Any ], None ]:
176+ parameters : t .Sequence [t .Any ] | t .Mapping [str , Any ] | None = None ,
177+ ) -> t .Sequence [t .Any ] | t .Mapping [str , Any ] | None :
182178 """Process parameters for type conversion.
183179
184180 Converts UUID objects to bytes format required by psqlpy.
@@ -209,7 +205,7 @@ def process_value(value: Any) -> Any:
209205 return {
210206 key : process_value (value ) for key , value in parameters .items ()
211207 }
212- if isinstance (parameters , ( list , tuple ) ):
208+ if isinstance (parameters , list | tuple ):
213209 return type (parameters )(
214210 process_value (value ) for value in parameters
215211 )
@@ -218,10 +214,8 @@ def process_value(value: Any) -> Any:
218214 def _convert_named_params_with_casting (
219215 self ,
220216 querystring : str ,
221- parameters : t .Union [
222- t .Sequence [t .Any ], t .Mapping [str , Any ], None
223- ] = None ,
224- ) -> t .Tuple [str , t .Union [t .Sequence [t .Any ], t .Mapping [str , Any ], None ]]:
217+ parameters : t .Sequence [t .Any ] | t .Mapping [str , Any ] | None = None ,
218+ ) -> tuple [str , t .Sequence [t .Any ] | t .Mapping [str , Any ] | None ]:
225219 """Convert named parameters with PostgreSQL casting syntax to positional parameters.
226220
227221 Transforms queries like:
@@ -326,7 +320,7 @@ def _convert_named_params_with_casting(
326320 return converted_query , converted_params
327321
328322 @property
329- def description (self ) -> "Optional[ _DBAPICursorDescription] " :
323+ def description (self ) -> "_DBAPICursorDescription | None " :
330324 return self ._description
331325
332326 @property
@@ -387,7 +381,7 @@ async def _executemany(
387381
388382 # Process all parameters first
389383 if seq_of_parameters and all (
390- isinstance (p , ( list , tuple ) ) for p in seq_of_parameters
384+ isinstance (p , list | tuple ) for p in seq_of_parameters
391385 ):
392386 converted_seq = [list (p ) for p in seq_of_parameters ]
393387 else :
@@ -398,7 +392,7 @@ async def _executemany(
398392 converted_seq .append ([])
399393 elif isinstance (processed , dict ):
400394 converted_seq .append (list (processed .values ()))
401- elif isinstance (processed , ( list , tuple ) ):
395+ elif isinstance (processed , list | tuple ):
402396 converted_seq .append (list (processed ))
403397 else :
404398 converted_seq .append ([processed ])
@@ -458,7 +452,7 @@ async def _executemany(
458452 if adapt_connection ._transaction is not None :
459453 try :
460454 # Build queries list for pipeline: [(query, params), ...]
461- queries : t . List [ t . Tuple [str , t . Optional [t .List [ t . Any ]] ]] = [
455+ queries : list [ tuple [str , list [t .Any ] | None ]] = [
462456 (operation , params ) for params in converted_seq
463457 ]
464458 await adapt_connection ._transaction .pipeline (
@@ -479,9 +473,7 @@ async def _executemany(
479473 def execute (
480474 self ,
481475 operation : t .Any ,
482- parameters : t .Union [
483- t .Sequence [t .Any ], t .Mapping [str , Any ], None
484- ] = None ,
476+ parameters : t .Sequence [t .Any ] | t .Mapping [str , Any ] | None = None ,
485477 ) -> None :
486478 self .await_ (self ._prepare_execute (operation , parameters ))
487479
@@ -500,7 +492,7 @@ class AsyncAdapt_psqlpy_ss_cursor(
500492):
501493 """Server-side cursor implementation for psqlpy."""
502494
503- _cursor : t . Optional [ psqlpy .Cursor ] # type: ignore[assignment]
495+ _cursor : psqlpy .Cursor | None # type: ignore[assignment]
504496
505497 def __init__ (
506498 self , adapt_connection : "AsyncAdapt_psqlpy_connection"
@@ -514,7 +506,7 @@ def __init__(
514506 def _convert_result (
515507 self ,
516508 result : psqlpy .QueryResult ,
517- ) -> Tuple [ Tuple [Any , ...], ...]:
509+ ) -> tuple [ tuple [Any , ...], ...]:
518510 """Convert psqlpy QueryResult to tuple of tuples."""
519511 if result is None :
520512 return ()
@@ -540,7 +532,7 @@ def close(self) -> None:
540532 self ._cursor = None
541533 self ._closed = True
542534
543- def fetchone (self ) -> Optional [ Tuple [ Any , ...]] :
535+ def fetchone (self ) -> tuple [ Any , ...] | None :
544536 """Fetch the next row from the cursor."""
545537 if self ._closed or self ._cursor is None :
546538 return None
@@ -552,7 +544,7 @@ def fetchone(self) -> Optional[Tuple[Any, ...]]:
552544 except Exception :
553545 return None
554546
555- def fetchmany (self , size : Optional [ int ] = None ) -> t . List [ Tuple [Any , ...]]:
547+ def fetchmany (self , size : int | None = None ) -> list [ tuple [Any , ...]]:
556548 """Fetch the next set of rows from the cursor."""
557549 if self ._closed or self ._cursor is None :
558550 return []
@@ -565,7 +557,7 @@ def fetchmany(self, size: Optional[int] = None) -> t.List[Tuple[Any, ...]]:
565557 except Exception :
566558 return []
567559
568- def fetchall (self ) -> t . List [ Tuple [Any , ...]]:
560+ def fetchall (self ) -> list [ tuple [Any , ...]]:
569561 """Fetch all remaining rows from the cursor."""
570562 if self ._closed or self ._cursor is None :
571563 return []
@@ -576,7 +568,7 @@ def fetchall(self) -> t.List[Tuple[Any, ...]]:
576568 except Exception :
577569 return []
578570
579- def __iter__ (self ) -> t .Iterator [Tuple [Any , ...]]:
571+ def __iter__ (self ) -> t .Iterator [tuple [Any , ...]]:
580572 if self ._closed or self ._cursor is None :
581573 return
582574
@@ -596,7 +588,7 @@ class AsyncAdapt_psqlpy_connection(AsyncAdapt_dbapi_connection):
596588 _ss_cursor_cls = AsyncAdapt_psqlpy_ss_cursor # type: ignore[assignment]
597589
598590 _connection : psqlpy .Connection # type: ignore[assignment]
599- _transaction : t . Optional [ psqlpy .Transaction ]
591+ _transaction : psqlpy .Transaction | None
600592
601593 __slots__ = (
602594 "_invalidate_schema_cache_asof" ,
@@ -637,7 +629,7 @@ def __init__(
637629 # LRU cache for prepared statements. Defaults to 100 statements per
638630 # connection. The cache is on a per-connection basis, stored within
639631 # connections pooled by the connection pool.
640- self ._prepared_statement_cache : t . Optional [ util .LRUCache [t .Any , t .Any ]]
632+ self ._prepared_statement_cache : util .LRUCache [t .Any , t .Any ] | None
641633 if prepared_statement_cache_size > 0 :
642634 self ._prepared_statement_cache = util .LRUCache (
643635 prepared_statement_cache_size
@@ -649,7 +641,7 @@ def __init__(
649641 self ._prepared_statement_name_func = self ._default_name_func
650642
651643 # Legacy query cache (kept for compatibility)
652- self ._query_cache : t . Dict [str , t .Any ] = {}
644+ self ._query_cache : dict [str , t .Any ] = {}
653645 self ._cache_max_size = prepared_statement_cache_size
654646
655647 async def _check_type_cache_invalidation (
@@ -739,7 +731,7 @@ def ping(self, reconnect: t.Any = None) -> t.Any:
739731 self ._connection_valid = False
740732 return False
741733
742- def _get_cached_query (self , query_key : str ) -> t .Optional [ t . Any ] :
734+ def _get_cached_query (self , query_key : str ) -> t .Any | None :
743735 """Get a cached prepared statement if available."""
744736 return self ._query_cache .get (query_key )
745737
@@ -761,7 +753,7 @@ def close(self) -> None:
761753
762754 def cursor (
763755 self , server_side : bool = False
764- ) -> Union [ AsyncAdapt_psqlpy_cursor , AsyncAdapt_psqlpy_ss_cursor ] :
756+ ) -> AsyncAdapt_psqlpy_cursor | AsyncAdapt_psqlpy_ss_cursor :
765757 if server_side :
766758 return self ._ss_cursor_cls (self )
767759 return self ._cursor_cls (self )
0 commit comments