44"""
55Core implementation of aiosqlite proxies
66"""
7+ from __future__ import annotations
78
89import asyncio
910import logging
1314from pathlib import Path
1415from queue import Empty , Queue , SimpleQueue
1516from threading import Thread
16- from typing import Any , Callable , Literal , Optional , Union
17+ from typing import Any , Callable , Literal , Optional
1718from warnings import warn
1819
1920from .context import contextmanager
@@ -47,11 +48,11 @@ def __init__(
4748 self ,
4849 connector : Callable [[], sqlite3 .Connection ],
4950 iter_chunk_size : int ,
50- loop : Optional [ asyncio .AbstractEventLoop ] = None ,
51+ loop : asyncio .AbstractEventLoop | None = None ,
5152 ) -> None :
5253 super ().__init__ ()
5354 self ._running = True
54- self ._connection : Optional [ sqlite3 .Connection ] = None
55+ self ._connection : sqlite3 .Connection | None = None
5556 self ._connector = connector
5657 self ._tx : SimpleQueue [tuple [asyncio .Future , Callable [[], Any ]]] = SimpleQueue ()
5758 self ._iter_chunk_size = iter_chunk_size
@@ -74,7 +75,7 @@ def _conn(self) -> sqlite3.Connection:
7475
7576 return self ._connection
7677
77- def _execute_insert (self , sql : str , parameters : Any ) -> Optional [ sqlite3 .Row ] :
78+ def _execute_insert (self , sql : str , parameters : Any ) -> sqlite3 .Row | None :
7879 cursor = self ._conn .execute (sql , parameters )
7980 cursor .execute ("SELECT last_insert_rowid()" )
8081 return cursor .fetchone ()
@@ -121,7 +122,7 @@ async def _execute(self, fn, *args, **kwargs):
121122
122123 return await future
123124
124- async def _connect (self ) -> " Connection" :
125+ async def _connect (self ) -> Connection :
125126 """Connect to the actual sqlite database."""
126127 if self ._connection is None :
127128 try :
@@ -135,11 +136,11 @@ async def _connect(self) -> "Connection":
135136
136137 return self
137138
138- def __await__ (self ) -> Generator [Any , None , " Connection" ]:
139+ def __await__ (self ) -> Generator [Any , None , Connection ]:
139140 self .start ()
140141 return self ._connect ().__await__ ()
141142
142- async def __aenter__ (self ) -> " Connection" :
143+ async def __aenter__ (self ) -> Connection :
143144 return await self
144145
145146 async def __aexit__ (self , exc_type , exc_val , exc_tb ) -> None :
@@ -175,7 +176,7 @@ async def close(self) -> None:
175176
176177 @contextmanager
177178 async def execute (
178- self , sql : str , parameters : Optional [ Iterable [Any ]] = None
179+ self , sql : str , parameters : Iterable [Any ] | None = None
179180 ) -> Cursor :
180181 """Helper to create a cursor and execute the given query."""
181182 if parameters is None :
@@ -185,16 +186,16 @@ async def execute(
185186
186187 @contextmanager
187188 async def execute_insert (
188- self , sql : str , parameters : Optional [ Iterable [Any ]] = None
189- ) -> Optional [ sqlite3 .Row ] :
189+ self , sql : str , parameters : Iterable [Any ] | None = None
190+ ) -> sqlite3 .Row | None :
190191 """Helper to insert and get the last_insert_rowid."""
191192 if parameters is None :
192193 parameters = []
193194 return await self ._execute (self ._execute_insert , sql , parameters )
194195
195196 @contextmanager
196197 async def execute_fetchall (
197- self , sql : str , parameters : Optional [ Iterable [Any ]] = None
198+ self , sql : str , parameters : Iterable [Any ] | None = None
198199 ) -> Iterable [sqlite3 .Row ]:
199200 """Helper to execute a query and return all the data."""
200201 if parameters is None :
@@ -246,19 +247,19 @@ def in_transaction(self) -> bool:
246247 return self ._conn .in_transaction
247248
248249 @property
249- def isolation_level (self ) -> Optional [ str ] :
250+ def isolation_level (self ) -> str | None :
250251 return self ._conn .isolation_level
251252
252253 @isolation_level .setter
253254 def isolation_level (self , value : IsolationLevel ) -> None :
254255 self ._conn .isolation_level = value
255256
256257 @property
257- def row_factory (self ) -> Optional [ type ] :
258+ def row_factory (self ) -> type | None :
258259 return self ._conn .row_factory
259260
260261 @row_factory .setter
261- def row_factory (self , factory : Optional [ type ] ) -> None :
262+ def row_factory (self , factory : type | None ) -> None :
262263 self ._conn .row_factory = factory
263264
264265 @property
@@ -280,7 +281,7 @@ async def load_extension(self, path: str):
280281 await self ._execute (self ._conn .load_extension , path ) # type: ignore
281282
282283 async def set_progress_handler (
283- self , handler : Callable [[], Optional [ int ] ], n : int
284+ self , handler : Callable [[], int | None ], n : int
284285 ) -> None :
285286 await self ._execute (self ._conn .set_progress_handler , handler , n )
286287
@@ -315,7 +316,7 @@ def dumper():
315316
316317 while True :
317318 try :
318- line : Optional [ str ] = dump_queue .get_nowait ()
319+ line : str | None = dump_queue .get_nowait ()
319320 if line is None :
320321 break
321322 yield line
@@ -331,10 +332,10 @@ def dumper():
331332
332333 async def backup (
333334 self ,
334- target : Union [ " Connection" , sqlite3 .Connection ] ,
335+ target : Connection | sqlite3 .Connection ,
335336 * ,
336337 pages : int = 0 ,
337- progress : Optional [ Callable [[int , int , int ], None ]] = None ,
338+ progress : Callable [[int , int , int ], None ] | None = None ,
338339 name : str = "main" ,
339340 sleep : float = 0.250 ,
340341 ) -> None :
@@ -357,10 +358,10 @@ async def backup(
357358
358359
359360def connect (
360- database : Union [ str , Path ] ,
361+ database : str | Path ,
361362 * ,
362363 iter_chunk_size = 64 ,
363- loop : Optional [ asyncio .AbstractEventLoop ] = None ,
364+ loop : asyncio .AbstractEventLoop | None = None ,
364365 ** kwargs : Any ,
365366) -> Connection :
366367 """Create and return a connection proxy to the sqlite database."""
0 commit comments