Skip to content

Commit 47cce3a

Browse files
committed
style fixes
1 parent be55108 commit 47cce3a

File tree

9 files changed

+104
-54
lines changed

9 files changed

+104
-54
lines changed

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ydb-dbapi"
3-
version = "0.1.0"
3+
version = "0.0.1"
44
description = ""
55
authors = ["Oleg Ovcharuk <[email protected]>"]
66
readme = "README.md"
@@ -58,6 +58,8 @@ ignore = [
5858
# Ignores below could be deleted
5959
"EM101", # Allow to use string literals in exceptions
6060
"TRY003", # Allow specifying long messages outside the exception class
61+
"SLF001", # Allow access private member,
62+
"PGH003", # Allow not to specify rule codes
6163
]
6264
select = ["ALL"]
6365

@@ -72,7 +74,8 @@ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
7274
force-single-line = true
7375

7476
[tool.ruff.lint.per-file-ignores]
75-
"**/test_*.py" = ["S", "SLF", "ANN201", "ARG", "PLR2004"]
77+
"**/test_*.py" = ["S", "SLF", "ANN201", "ARG", "PLR2004", "PT012"]
78+
"conftest.py" = ["ARG001"]
7679
"__init__.py" = ["F401", "F403"]
7780

7881
[tool.pytest.ini_options]

tests/conftest.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from asyncio import AbstractEventLoop
4+
from collections.abc import AsyncGenerator
35
from collections.abc import Generator
46
from typing import Any
57
from typing import Callable
@@ -9,6 +11,7 @@
911
from testcontainers.core.generic import DbContainer
1012
from testcontainers.core.generic import wait_container_is_ready
1113
from testcontainers.core.utils import setup_logger
14+
from typing_extensions import Self
1215

1316
logger = setup_logger(__name__)
1417

@@ -33,7 +36,7 @@ def __init__(
3336
self._name = name
3437
self._database_name = "local"
3538

36-
def start(self):
39+
def start(self) -> Self:
3740
self._maybe_stop_old_container()
3841
super().start()
3942
return self
@@ -115,7 +118,9 @@ def connection_kwargs(ydb_container: YDBContainer) -> dict:
115118

116119

117120
@pytest.fixture
118-
async def driver(ydb_container, event_loop):
121+
async def driver(
122+
ydb_container: YDBContainer, event_loop: AbstractEventLoop
123+
) -> AsyncGenerator[ydb.aio.Driver]:
119124
driver = ydb.aio.Driver(
120125
connection_string=ydb_container.get_connection_string()
121126
)
@@ -128,7 +133,9 @@ async def driver(ydb_container, event_loop):
128133

129134

130135
@pytest.fixture
131-
async def session_pool(driver: ydb.aio.Driver):
136+
async def session_pool(
137+
driver: ydb.aio.Driver,
138+
) -> AsyncGenerator[ydb.aio.QuerySessionPool]:
132139
session_pool = ydb.aio.QuerySessionPool(driver)
133140
async with session_pool:
134141
await session_pool.execute_with_retries(
@@ -146,8 +153,11 @@ async def session_pool(driver: ydb.aio.Driver):
146153

147154
yield session_pool
148155

156+
149157
@pytest.fixture
150-
async def session(session_pool: ydb.aio.QuerySessionPool):
158+
async def session(
159+
session_pool: ydb.aio.QuerySessionPool,
160+
) -> AsyncGenerator[ydb.aio.QuerySession]:
151161
session = await session_pool.acquire()
152162

153163
yield session

tests/test_connection.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import annotations
2+
3+
from collections.abc import AsyncGenerator
14
from contextlib import suppress
25

36
import pytest
@@ -37,7 +40,7 @@ async def _test_isolation_level_read_only(
3740
await connection.rollback()
3841

3942
async with connection.cursor() as cursor:
40-
cursor.execute("DROP TABLE foo")
43+
await cursor.execute("DROP TABLE foo")
4144

4245
async def _test_connection(self, connection: dbapi.Connection) -> None:
4346
await connection.commit()
@@ -66,7 +69,9 @@ async def _test_connection(self, connection: dbapi.Connection) -> None:
6669
await cur.execute("DROP TABLE foo")
6770
await cur.close()
6871

69-
async def _test_cursor_raw_query(self, connection: dbapi.Connection) -> None:
72+
async def _test_cursor_raw_query(
73+
self, connection: dbapi.Connection
74+
) -> None:
7075
cur = connection.cursor()
7176
assert cur
7277

@@ -107,7 +112,10 @@ async def _test_cursor_raw_query(self, connection: dbapi.Connection) -> None:
107112

108113
async def _test_errors(self, connection: dbapi.Connection) -> None:
109114
with pytest.raises(dbapi.InterfaceError):
110-
await dbapi.connect("localhost:2136", database="/local666")
115+
await dbapi.connect(
116+
"localhost:2136", # type: ignore
117+
database="/local666", # type: ignore
118+
)
111119

112120
cur = connection.cursor()
113121

@@ -142,8 +150,10 @@ async def _test_errors(self, connection: dbapi.Connection) -> None:
142150

143151
class TestAsyncConnection(BaseDBApiTestSuit):
144152
@pytest_asyncio.fixture
145-
async def connection(self, connection_kwargs):
146-
conn = await dbapi.connect(**connection_kwargs)
153+
async def connection(
154+
self, connection_kwargs: dict
155+
) -> AsyncGenerator[dbapi.Connection]:
156+
conn = await dbapi.connect(**connection_kwargs) # ignore: typing
147157
try:
148158
yield conn
149159
finally:
@@ -166,19 +176,21 @@ async def test_isolation_level_read_only(
166176
isolation_level: str,
167177
read_only: bool,
168178
connection: dbapi.Connection,
169-
):
179+
) -> None:
170180
await self._test_isolation_level_read_only(
171181
connection, isolation_level, read_only
172182
)
173183

174184
@pytest.mark.asyncio
175-
async def test_connection(self, connection: dbapi.Connection):
185+
async def test_connection(self, connection: dbapi.Connection) -> None:
176186
await self._test_connection(connection)
177187

178188
@pytest.mark.asyncio
179-
async def test_cursor_raw_query(self, connection: dbapi.Connection):
189+
async def test_cursor_raw_query(
190+
self, connection: dbapi.Connection
191+
) -> None:
180192
await self._test_cursor_raw_query(connection)
181193

182194
@pytest.mark.asyncio
183-
async def test_errors(self, connection: dbapi.Connection):
195+
async def test_errors(self, connection: dbapi.Connection) -> None:
184196
await self._test_errors(connection)

tests/test_cursor.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import pytest
22
import ydb_dbapi
3+
from ydb.aio import QuerySession
34

45

56
@pytest.mark.asyncio
6-
async def test_cursor_ddl(session):
7+
async def test_cursor_ddl(session: QuerySession) -> None:
78
cursor = ydb_dbapi.Cursor(session=session)
89

910
yql = """
@@ -27,7 +28,7 @@ async def test_cursor_ddl(session):
2728

2829

2930
@pytest.mark.asyncio
30-
async def test_cursor_dml(session):
31+
async def test_cursor_dml(session: QuerySession) -> None:
3132
cursor = ydb_dbapi.Cursor(session=session)
3233
yql_text = """
3334
INSERT INTO table (id, val) VALUES
@@ -48,12 +49,13 @@ async def test_cursor_dml(session):
4849
await cursor.execute(query=yql_text)
4950

5051
res = await cursor.fetchone()
52+
assert res is not None
5153
assert len(res) == 1
5254
assert res[0] == 3
5355

5456

5557
@pytest.mark.asyncio
56-
async def test_cursor_fetch_one(session):
58+
async def test_cursor_fetch_one(session: QuerySession) -> None:
5759
cursor = ydb_dbapi.Cursor(session=session)
5860
yql_text = """
5961
INSERT INTO table (id, val) VALUES
@@ -73,16 +75,18 @@ async def test_cursor_fetch_one(session):
7375
await cursor.execute(query=yql_text)
7476

7577
res = await cursor.fetchone()
78+
assert res is not None
7679
assert res[0] == 1
7780

7881
res = await cursor.fetchone()
82+
assert res is not None
7983
assert res[0] == 2
8084

8185
assert await cursor.fetchone() is None
8286

8387

8488
@pytest.mark.asyncio
85-
async def test_cursor_fetch_many(session):
89+
async def test_cursor_fetch_many(session: QuerySession) -> None:
8690
cursor = ydb_dbapi.Cursor(session=session)
8791
yql_text = """
8892
INSERT INTO table (id, val) VALUES
@@ -104,23 +108,26 @@ async def test_cursor_fetch_many(session):
104108
await cursor.execute(query=yql_text)
105109

106110
res = await cursor.fetchmany()
111+
assert res is not None
107112
assert len(res) == 1
108113
assert res[0][0] == 1
109114

110115
res = await cursor.fetchmany(size=2)
116+
assert res is not None
111117
assert len(res) == 2
112118
assert res[0][0] == 2
113119
assert res[1][0] == 3
114120

115121
res = await cursor.fetchmany(size=2)
122+
assert res is not None
116123
assert len(res) == 1
117124
assert res[0][0] == 4
118125

119126
assert await cursor.fetchmany(size=2) is None
120127

121128

122129
@pytest.mark.asyncio
123-
async def test_cursor_fetch_all(session):
130+
async def test_cursor_fetch_all(session: QuerySession) -> None:
124131
cursor = ydb_dbapi.Cursor(session=session)
125132
yql_text = """
126133
INSERT INTO table (id, val) VALUES
@@ -143,6 +150,7 @@ async def test_cursor_fetch_all(session):
143150
assert cursor.rowcount == 3
144151

145152
res = await cursor.fetchall()
153+
assert res is not None
146154
assert len(res) == 3
147155
assert res[0][0] == 1
148156
assert res[1][0] == 2
@@ -152,20 +160,22 @@ async def test_cursor_fetch_all(session):
152160

153161

154162
@pytest.mark.asyncio
155-
async def test_cursor_next_set(session):
163+
async def test_cursor_next_set(session: QuerySession) -> None:
156164
cursor = ydb_dbapi.Cursor(session=session)
157165
yql_text = """SELECT 1 as val; SELECT 2 as val;"""
158166

159167
await cursor.execute(query=yql_text)
160168

161169
res = await cursor.fetchall()
170+
assert res is not None
162171
assert len(res) == 1
163172
assert res[0][0] == 1
164173

165174
nextset = await cursor.nextset()
166175
assert nextset
167176

168177
res = await cursor.fetchall()
178+
assert res is not None
169179
assert len(res) == 1
170180
assert res[0][0] == 2
171181

ydb_dbapi/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from .connection import Connection
2-
from .connection import IsolationLevel
3-
from .connection import connect
1+
from .connections import Connection
2+
from .connections import IsolationLevel
3+
from .connections import connect
44
from .cursors import Cursor
55
from .errors import *

ydb_dbapi/connection.py renamed to ydb_dbapi/connections.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ def __init__(
5050
"ydb_session_pool" in self.conn_kwargs
5151
): # Use session pool managed manually
5252
self._shared_session_pool = True
53-
self._session_pool = self.conn_kwargs.pop(
54-
"ydb_session_pool"
55-
)
53+
self._session_pool = self.conn_kwargs.pop("ydb_session_pool")
5654
self._driver = self._session_pool._driver
5755
else:
5856
self._shared_session_pool = False
@@ -127,13 +125,24 @@ class Connection(BaseYDBConnection):
127125
_ydb_driver_class = ydb.aio.Driver
128126
_ydb_session_pool_class = ydb.aio.QuerySessionPool
129127

130-
def __init__(self, *args, **kwargs) -> None:
131-
super().__init__(*args, **kwargs)
128+
def __init__(
129+
self,
130+
host: str = "",
131+
port: str = "",
132+
database: str = "",
133+
**conn_kwargs: Any,
134+
) -> None:
135+
super().__init__(
136+
host,
137+
port,
138+
database,
139+
**conn_kwargs,
140+
)
132141

133142
self._session: ydb.aio.QuerySession | None = None
134-
self._tx_context: ydb.QueryTxContext | None = None
143+
self._tx_context: ydb.aio.QueryTxContext | None = None
135144

136-
async def _wait(self, timeout: int = 5) -> None:
145+
async def wait_ready(self, timeout: int = 5) -> None:
137146
try:
138147
await self._driver.wait(timeout, fail_fast=True)
139148
except ydb.Error as e:
@@ -144,13 +153,13 @@ async def _wait(self, timeout: int = 5) -> None:
144153
"Failed to connect to YDB, details "
145154
f"{self._driver.discovery_debug_details()}"
146155
)
147-
raise InterfaceError(
148-
msg
149-
) from e
156+
raise InterfaceError(msg) from e
150157

151158
self._session = await self._session_pool.acquire()
152159

153-
def cursor(self):
160+
def cursor(self) -> Cursor:
161+
if self._session is None:
162+
raise RuntimeError("Connection is not ready, use wait_ready.")
154163
if self._current_cursor and not self._current_cursor._closed:
155164
raise RuntimeError(
156165
"Unable to create new Cursor before closing existing one."
@@ -218,12 +227,13 @@ async def callee() -> None:
218227
await self._driver.scheme_client.describe_path(table_path)
219228

220229
await retry_operation_async(callee)
221-
return True
222230
except ydb.SchemeError:
223231
return False
232+
else:
233+
return True
224234

225235
async def _get_table_names(self, abs_dir_path: str) -> list[str]:
226-
async def callee():
236+
async def callee() -> ydb.Directory:
227237
return await self._driver.scheme_client.list_directory(
228238
abs_dir_path
229239
)
@@ -239,7 +249,7 @@ async def callee():
239249
return result
240250

241251

242-
async def connect(*args, **kwargs) -> Connection:
243-
conn = Connection(*args, **kwargs)
244-
await conn._wait()
252+
async def connect(*args: tuple, **kwargs: dict) -> Connection:
253+
conn = Connection(*args, **kwargs) # type: ignore
254+
await conn.wait_ready()
245255
return conn

0 commit comments

Comments
 (0)