Skip to content

Commit f03fc87

Browse files
gordthompsonrafiss
authored andcommitted
Avoid lint errors and SQLA version mismatch in CI
1 parent 133a3a5 commit f03fc87

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ authors = [
99
readme = "README.md"
1010
requires-python = ">=3.9"
1111
dependencies = [
12-
"sqlalchemy (>=2.0.41,<3.0.0)"
12+
"sqlalchemy (>=2.0.40,<3.0.0)"
1313
]
1414

1515

1616
[build-system]
1717
requires = ["poetry-core>=2.0.0,<3.0.0"]
1818
build-backend = "poetry.core.masonry.api"
1919

20+
[tool.black]
21+
line-length = 100
22+
2023
[tool.poetry.group.dev.dependencies]
2124
alembic = "1.15.2"
2225
async-timeout = "5.0.1"

sqlalchemy_cockroachdb/transaction.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,13 @@ def retry_exponential_backoff(retry_count: int, max_backoff: int = 0) -> None:
9898
:return: None
9999
"""
100100

101-
sleep_secs = uniform(0, min(max_backoff, 0.1 * (2 ** retry_count)))
101+
sleep_secs = uniform(0, min(max_backoff, 0.1 * (2**retry_count)))
102102
sleep(sleep_secs)
103103

104104

105-
def run_in_nested_transaction(conn, callback, max_retries, max_backoff, inject_error=False, **kwargs):
105+
def run_in_nested_transaction(
106+
conn, callback, max_retries, max_backoff, inject_error=False, **kwargs
107+
):
106108
if isinstance(conn, sqlalchemy.orm.Session):
107109
dbapi_name = conn.bind.driver
108110
else:
@@ -124,11 +126,13 @@ def run_in_nested_transaction(conn, callback, max_retries, max_backoff, inject_e
124126
if dbapi_name == "psycopg2":
125127
import psycopg2
126128
import psycopg2.errorcodes
129+
127130
if isinstance(e.orig, psycopg2.OperationalError):
128131
if e.orig.pgcode == psycopg2.errorcodes.SERIALIZATION_FAILURE:
129132
do_retry = True
130133
else:
131134
import psycopg
135+
132136
if isinstance(e.orig, psycopg.errors.SerializationFailure):
133137
do_retry = True
134138
if do_retry:
@@ -149,5 +153,7 @@ def _txn_retry_loop(conn, callback, max_retries, max_backoff, **kwargs):
149153
result = run_in_nested_transaction(conn, callback, max_retries, max_backoff, **kwargs)
150154
if isinstance(result, ChainTransaction):
151155
for transaction in result.transactions:
152-
result.add_result(run_in_nested_transaction(conn, transaction, max_retries, max_backoff, **kwargs))
156+
result.add_result(
157+
run_in_nested_transaction(conn, transaction, max_retries, max_backoff, **kwargs)
158+
)
153159
return result

test/test_run_transaction_core.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from sqlalchemy.testing import fixtures
44
from sqlalchemy.types import Integer
55
import threading
6-
from sqlalchemy.orm import sessionmaker, scoped_session, relationship
6+
from sqlalchemy.orm import sessionmaker, scoped_session
77

88

99
from sqlalchemy_cockroachdb import run_transaction
@@ -140,7 +140,6 @@ def txn_body(conn):
140140
rs = run_transaction(conn, txn_body)
141141
assert rs[0] == (1, 100)
142142

143-
144143
def test_run_transaction_retry_with_nested(self):
145144
def txn_body(conn):
146145
rs = conn.execute(text("select acct, balance from account where acct = 1"))
@@ -151,25 +150,22 @@ def txn_body(conn):
151150
rs = run_transaction(conn, txn_body, use_cockroach_restart=False)
152151
assert rs[0] == (1, 100)
153152

154-
155153
def test_run_chained_transaction(self):
156154
def txn_body(conn):
157155
# first transaction inserts
158-
conn.execute(
159-
account_table.insert(), [dict(acct=99, balance=100)]
160-
)
156+
conn.execute(account_table.insert(), [dict(acct=99, balance=100)])
161157
conn.execute(text("select crdb_internal.force_retry('1s')"))
162158

163159
def _get_val(s):
164160
rs = s.execute(text("select acct, balance from account where acct = 99"))
165161
return [r for r in rs]
166162

167-
# chain the get into a separate nested transaction, so that the value
163+
# chain the get into a separate nested transaction, so that the value
168164
# in the previous nested transaction is flushed and available
169-
return ChainTransaction([lambda s : _get_val(s), lambda s : _get_val(s)])
165+
return ChainTransaction([lambda s: _get_val(s), lambda s: _get_val(s)])
170166

171167
with testing.db.connect() as conn:
172168
rs = run_transaction(conn, txn_body, use_cockroach_restart=False)
173169
assert len(rs.results) == 2
174-
assert rs.results[0][0] == (99,100)
175-
assert rs.results[1][0] == (99,100)
170+
assert rs.results[0][0] == (99, 100)
171+
assert rs.results[1][0] == (99, 100)

0 commit comments

Comments
 (0)