Skip to content

Commit 7f2d4b8

Browse files
committed
refactor: remove typing extensions from deps and apply future type hint style
1 parent 895fd91 commit 7f2d4b8

File tree

4 files changed

+35
-32
lines changed

4 files changed

+35
-32
lines changed

aiosqlite/core.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
Core implementation of aiosqlite proxies
66
"""
7+
from __future__ import annotations
78

89
import asyncio
910
import logging
@@ -13,7 +14,7 @@
1314
from pathlib import Path
1415
from queue import Empty, Queue, SimpleQueue
1516
from threading import Thread
16-
from typing import Any, Callable, Literal, Optional, Union
17+
from typing import Any, Callable, Literal, Optional
1718
from warnings import warn
1819

1920
from .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

359360
def 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."""

aiosqlite/cursor.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
# Copyright Amethyst Reese
22
# Licensed under the MIT license
3+
from __future__ import annotations
34

45
import sqlite3
56
from collections.abc import AsyncIterator, Iterable
6-
from typing import Any, Callable, Optional, TYPE_CHECKING
7+
from typing import Any, Callable, TYPE_CHECKING
78

89
if TYPE_CHECKING:
910
from .core import Connection
1011

1112

1213
class Cursor:
13-
def __init__(self, conn: "Connection", cursor: sqlite3.Cursor) -> None:
14+
def __init__(self, conn: Connection, cursor: sqlite3.Cursor) -> None:
1415
self.iter_chunk_size = conn._iter_chunk_size
1516
self._conn = conn
1617
self._cursor = cursor
@@ -32,8 +33,8 @@ async def _execute(self, fn, *args, **kwargs):
3233
return await self._conn._execute(fn, *args, **kwargs)
3334

3435
async def execute(
35-
self, sql: str, parameters: Optional[Iterable[Any]] = None
36-
) -> "Cursor":
36+
self, sql: str, parameters: Iterable[Any] | None = None
37+
) -> Cursor:
3738
"""Execute the given query."""
3839
if parameters is None:
3940
parameters = []
@@ -42,21 +43,21 @@ async def execute(
4243

4344
async def executemany(
4445
self, sql: str, parameters: Iterable[Iterable[Any]]
45-
) -> "Cursor":
46+
) -> Cursor:
4647
"""Execute the given multiquery."""
4748
await self._execute(self._cursor.executemany, sql, parameters)
4849
return self
4950

50-
async def executescript(self, sql_script: str) -> "Cursor":
51+
async def executescript(self, sql_script: str) -> Cursor:
5152
"""Execute a user script."""
5253
await self._execute(self._cursor.executescript, sql_script)
5354
return self
5455

55-
async def fetchone(self) -> Optional[sqlite3.Row]:
56+
async def fetchone(self) -> sqlite3.Row | None:
5657
"""Fetch a single row."""
5758
return await self._execute(self._cursor.fetchone)
5859

59-
async def fetchmany(self, size: Optional[int] = None) -> Iterable[sqlite3.Row]:
60+
async def fetchmany(self, size: int | None = None) -> Iterable[sqlite3.Row]:
6061
"""Fetch up to `cursor.arraysize` number of rows."""
6162
args: tuple[int, ...] = ()
6263
if size is not None:
@@ -76,7 +77,7 @@ def rowcount(self) -> int:
7677
return self._cursor.rowcount
7778

7879
@property
79-
def lastrowid(self) -> Optional[int]:
80+
def lastrowid(self) -> int | None:
8081
return self._cursor.lastrowid
8182

8283
@property
@@ -92,11 +93,11 @@ def description(self) -> tuple[tuple[str, None, None, None, None, None, None], .
9293
return self._cursor.description
9394

9495
@property
95-
def row_factory(self) -> Optional[Callable[[sqlite3.Cursor, sqlite3.Row], object]]:
96+
def row_factory(self) -> Callable[[sqlite3.Cursor, sqlite3.Row], object] | None:
9697
return self._cursor.row_factory
9798

9899
@row_factory.setter
99-
def row_factory(self, factory: Optional[type]) -> None:
100+
def row_factory(self, factory: type | None) -> None:
100101
self._cursor.row_factory = factory
101102

102103
@property

aiosqlite/tests/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
# Licensed under the MIT license
33

44
from .smoke import SmokeTest
5+
6+
__all__ = ("SmokeTest",)

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ classifiers = [
1919
]
2020
requires-python = ">=3.9"
2121
dependencies = [
22-
"typing_extensions >= 4.0",
2322
]
2423

2524
[project.optional-dependencies]

0 commit comments

Comments
 (0)