Skip to content

Commit

Permalink
Check that variables are used inside loops
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Apr 29, 2023
1 parent 23d54b8 commit d67f5ab
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 73 deletions.
2 changes: 1 addition & 1 deletion dbutils/simple_pooled_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def __init__(self, dbapi, maxconnections, *args, **kwargs):
"Database module threading support cannot be determined.")
# Establish all database connections (it would be better to
# only establish a part of them now, and the rest on demand).
for i in range(maxconnections):
for _i in range(maxconnections):
self.addConnection(dbapi.connect(*args, **kwargs))

# The following functions are used with DB-API 2 modules
Expand Down
2 changes: 1 addition & 1 deletion dbutils/simple_pooled_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def __init__(self, maxconnections, *args, **kwargs):
self._queue = Queue(maxconnections)
# Establish all database connections (it would be better to
# only establish a part of them now, and the rest on demand).
for i in range(maxconnections):
for _i in range(maxconnections):
self.cache(PgConnection(*args, **kwargs))

def cache(self, con):
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ select = [
]
# You can use `ruff rule ...` to see what these ignored rules check
ignore = [
"B007",
"B904",
"E722",
"N811",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_persistent_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_threads(dbapi): # noqa: F811
num_threads = 3
persist = PersistentDB(dbapi, closeable=True)
query_queue, result_queue = [], []
for i in range(num_threads):
for _i in range(num_threads):
query_queue.append(Queue(1))
result_queue.append(Queue(1))

Expand Down Expand Up @@ -164,7 +164,7 @@ def test_setsession(dbapi): # noqa: F811
cursor.execute('set test')
cursor.fetchone()
cursor.close()
for i in range(3):
for _i in range(3):
assert db._con.session == ['datestyle', 'test']
cursor = db.cursor()
cursor.execute('select test')
Expand Down
4 changes: 2 additions & 2 deletions tests/test_persistent_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_threads():
num_threads = 3
persist = PersistentPg()
query_queue, result_queue = [], []
for i in range(num_threads):
for _i in range(num_threads):
query_queue.append(Queue(1))
result_queue.append(Queue(1))

Expand Down Expand Up @@ -132,7 +132,7 @@ def test_setsession():
assert db._setsession_sql == ('set datestyle',)
assert db.db.session == ['datestyle']
db.query('set test')
for i in range(3):
for _i in range(3):
assert db.db.session == ['datestyle', 'test']
db.query('select test')
assert db.db.session == ['datestyle']
Expand Down
81 changes: 41 additions & 40 deletions tests/test_pooled_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def close(what=closed):
if shareable:
assert len(pool._shared_cache) == 0
cache = []
for i in range(5):
for _i in range(5):
cache.append(pool.connection())
assert len(pool._idle_cache) == 5
if shareable:
Expand All @@ -297,7 +297,7 @@ def close_shared(what=closed):

if shareable:
cache = []
for i in range(5):
for _i in range(5):
cache.append(pool.connection())
pool._shared_cache[3].con.close = close_shared
else:
Expand Down Expand Up @@ -407,63 +407,64 @@ def test_min_max_cached(dbapi, threadsafety): # noqa: F811
shareable = threadsafety > 1
pool = PooledDB(dbapi, 3)
assert len(pool._idle_cache) == 3
cache = [pool.connection() for i in range(3)]
cache = [pool.connection() for _i in range(3)]
assert len(pool._idle_cache) == 0
assert cache
del cache
assert len(pool._idle_cache) == 3
cache = [pool.connection() for i in range(6)]
cache = [pool.connection() for _i in range(6)]
assert len(pool._idle_cache) == 0
assert cache
del cache
assert len(pool._idle_cache) == 6
pool = PooledDB(dbapi, 0, 3)
assert len(pool._idle_cache) == 0
cache = [pool.connection() for i in range(3)]
cache = [pool.connection() for _i in range(3)]
assert len(pool._idle_cache) == 0
assert cache
del cache
assert len(pool._idle_cache) == 3
cache = [pool.connection() for i in range(6)]
cache = [pool.connection() for _i in range(6)]
assert len(pool._idle_cache) == 0
assert cache
del cache
assert len(pool._idle_cache) == 3
pool = PooledDB(dbapi, 3, 3)
assert len(pool._idle_cache) == 3
cache = [pool.connection() for i in range(3)]
cache = [pool.connection() for _i in range(3)]
assert len(pool._idle_cache) == 0
assert cache
del cache
assert len(pool._idle_cache) == 3
cache = [pool.connection() for i in range(6)]
cache = [pool.connection() for _i in range(6)]
assert len(pool._idle_cache) == 0
assert cache
del cache
assert len(pool._idle_cache) == 3
pool = PooledDB(dbapi, 3, 2)
assert len(pool._idle_cache) == 3
cache = [pool.connection() for i in range(4)]
cache = [pool.connection() for _i in range(4)]
assert len(pool._idle_cache) == 0
assert cache
del cache
assert len(pool._idle_cache) == 3
pool = PooledDB(dbapi, 2, 5)
assert len(pool._idle_cache) == 2
cache = [pool.connection() for i in range(10)]
cache = [pool.connection() for _i in range(10)]
assert len(pool._idle_cache) == 0
assert cache
del cache
assert len(pool._idle_cache) == 5
pool = PooledDB(dbapi, 1, 2, 3)
assert len(pool._idle_cache) == 1
cache = [pool.connection(False) for i in range(4)]
cache = [pool.connection(False) for _i in range(4)]
assert len(pool._idle_cache) == 0
if shareable:
assert len(pool._shared_cache) == 0
assert cache
del cache
assert len(pool._idle_cache) == 2
cache = [pool.connection() for i in range(10)]
cache = [pool.connection() for _i in range(10)]
assert len(pool._idle_cache) == 0
if shareable:
assert len(pool._shared_cache) == 3
Expand All @@ -474,14 +475,14 @@ def test_min_max_cached(dbapi, threadsafety): # noqa: F811
assert len(pool._shared_cache) == 0
pool = PooledDB(dbapi, 1, 3, 2)
assert len(pool._idle_cache) == 1
cache = [pool.connection(False) for i in range(4)]
cache = [pool.connection(False) for _i in range(4)]
assert len(pool._idle_cache) == 0
if shareable:
assert len(pool._shared_cache) == 0
assert cache
del cache
assert len(pool._idle_cache) == 3
cache = [pool.connection() for i in range(10)]
cache = [pool.connection() for _i in range(10)]
if shareable:
assert len(pool._idle_cache) == 1
assert len(pool._shared_cache) == 2
Expand All @@ -500,34 +501,34 @@ def test_max_shared(dbapi, threadsafety): # noqa: F811
shareable = threadsafety > 1
pool = PooledDB(dbapi)
assert len(pool._idle_cache) == 0
cache = [pool.connection() for i in range(10)]
cache = [pool.connection() for _i in range(10)]
assert len(cache) == 10
assert len(pool._idle_cache) == 0
pool = PooledDB(dbapi, 1, 1, 0)
assert len(pool._idle_cache) == 1
cache = [pool.connection() for i in range(10)]
cache = [pool.connection() for _i in range(10)]
assert len(cache) == 10
assert len(pool._idle_cache) == 0
pool = PooledDB(dbapi, 0, 0, 1)
cache = [pool.connection() for i in range(10)]
cache = [pool.connection() for _i in range(10)]
assert len(cache) == 10
assert len(pool._idle_cache) == 0
if shareable:
assert len(pool._shared_cache) == 1
pool = PooledDB(dbapi, 1, 1, 1)
assert len(pool._idle_cache) == 1
cache = [pool.connection() for i in range(10)]
cache = [pool.connection() for _i in range(10)]
assert len(cache) == 10
assert len(pool._idle_cache) == 0
if shareable:
assert len(pool._shared_cache) == 1
pool = PooledDB(dbapi, 0, 0, 7)
cache = [pool.connection(False) for i in range(3)]
cache = [pool.connection(False) for _i in range(3)]
assert len(cache) == 3
assert len(pool._idle_cache) == 0
if shareable:
assert len(pool._shared_cache) == 0
cache = [pool.connection() for i in range(10)]
cache = [pool.connection() for _i in range(10)]
assert len(cache) == 10
assert len(pool._idle_cache) == 3
if shareable:
Expand All @@ -537,7 +538,7 @@ def test_max_shared(dbapi, threadsafety): # noqa: F811
def test_sort_shared(dbapi): # noqa: F811
pool = PooledDB(dbapi, 0, 4, 4)
cache = []
for i in range(6):
for _i in range(6):
db = pool.connection()
db.cursor().execute('select test')
cache.append(db)
Expand All @@ -559,7 +560,7 @@ def test_equally_shared(dbapi, threadsafety): # noqa: F811
shareable = threadsafety > 1
pool = PooledDB(dbapi, 5, 5, 5)
assert len(pool._idle_cache) == 5
for i in range(15):
for _i in range(15):
db = pool.connection(False)
db.cursor().execute('select test')
db.close()
Expand All @@ -569,7 +570,7 @@ def test_equally_shared(dbapi, threadsafety): # noqa: F811
assert con._usage == 3
assert con._con.num_queries == 3
cache = []
for i in range(35):
for _i in range(35):
db = pool.connection()
db.cursor().execute('select test')
cache.append(db)
Expand All @@ -595,7 +596,7 @@ def test_many_shared(dbapi, threadsafety): # noqa: F811
shareable = threadsafety > 1
pool = PooledDB(dbapi, 0, 0, 5)
cache = []
for i in range(35):
for _i in range(35):
db = pool.connection()
db.cursor().execute('select test1')
db.cursor().execute('select test2')
Expand All @@ -622,7 +623,7 @@ def test_many_shared(dbapi, threadsafety): # noqa: F811
for db in cache:
if db:
db.cursor().callproc('test4')
for i in range(6):
for _i in range(6):
db = pool.connection()
db.cursor().callproc('test4')
cache.append(db)
Expand Down Expand Up @@ -685,7 +686,7 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
assert pool._connections == 0
assert len(pool._idle_cache) == 1
cache = []
for i in range(3):
for _i in range(3):
cache.append(pool.connection(False))
assert pool._connections == 3
assert len(pool._idle_cache) == 0
Expand All @@ -700,7 +701,7 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
assert len(pool._idle_cache) == 2
if shareable:
assert len(pool._shared_cache) == 0
for i in range(3):
for _i in range(3):
cache.append(pool.connection())
assert len(pool._idle_cache) == 0
if shareable:
Expand Down Expand Up @@ -794,7 +795,7 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
assert pool._connections == 0
assert len(pool._idle_cache) == 4
cache = []
for i in range(4):
for _i in range(4):
cache.append(pool.connection(False))
assert pool._connections == 4
assert len(pool._idle_cache) == 0
Expand All @@ -806,7 +807,7 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
assert pool._maxconnections == 4
assert pool._connections == 0
assert len(pool._idle_cache) == 1
for i in range(4):
for _i in range(4):
cache.append(pool.connection())
assert len(pool._idle_cache) == 0
if shareable:
Expand All @@ -826,7 +827,7 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
assert pool._maxconnections == 3
assert pool._connections == 0
cache = []
for i in range(3):
for _i in range(3):
cache.append(pool.connection(False))
assert pool._connections == 3
with pytest.raises(TooManyConnections):
Expand All @@ -835,11 +836,11 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
pool.connection(True)
cache = []
assert pool._connections == 0
for i in range(3):
for _i in range(3):
cache.append(pool.connection())
assert pool._connections == 3
if shareable:
for i in range(3):
for _i in range(3):
cache.append(pool.connection())
assert pool._connections == 3
else:
Expand All @@ -851,7 +852,7 @@ def test_maxconnections(dbapi, threadsafety): # noqa: F811, PLR0915
assert pool._maxconnections == 0
assert pool._connections == 0
cache = []
for i in range(10):
for _i in range(10):
cache.append(pool.connection(False))
cache.append(pool.connection())
if shareable:
Expand Down Expand Up @@ -967,40 +968,40 @@ def test_one_thread_two_connections(dbapi, threadsafety): # noqa: F811
shareable = threadsafety > 1
pool = PooledDB(dbapi, 2)
db1 = pool.connection()
for i in range(5):
for _i in range(5):
db1.cursor().execute('select test')
db2 = pool.connection()
assert db1 != db2
assert db1._con != db2._con
for i in range(7):
for _i in range(7):
db2.cursor().execute('select test')
assert db1._con._con.num_queries == 5
assert db2._con._con.num_queries == 7
del db1
db1 = pool.connection()
assert db1 != db2
assert db1._con != db2._con
for i in range(3):
for _i in range(3):
db1.cursor().execute('select test')
assert db1._con._con.num_queries == 8
db2.cursor().execute('select test')
assert db2._con._con.num_queries == 8
pool = PooledDB(dbapi, 0, 0, 2)
db1 = pool.connection()
for i in range(5):
for _i in range(5):
db1.cursor().execute('select test')
db2 = pool.connection()
assert db1 != db2
assert db1._con != db2._con
for i in range(7):
for _i in range(7):
db2.cursor().execute('select test')
assert db1._con._con.num_queries == 5
assert db2._con._con.num_queries == 7
del db1
db1 = pool.connection()
assert db1 != db2
assert db1._con != db2._con
for i in range(3):
for _i in range(3):
db1.cursor().execute('select test')
assert db1._con._con.num_queries == 8
db2.cursor().execute('select test')
Expand Down Expand Up @@ -1028,7 +1029,7 @@ def test_three_threads_two_connections(dbapi, threadsafety): # noqa: F811
def connection():
queue.put(pool.connection(), timeout=1)

for i in range(3):
for _i in range(3):
Thread(target=connection).start()
db1 = queue.get(timeout=1)
db2 = queue.get(timeout=1)
Expand Down

0 comments on commit d67f5ab

Please sign in to comment.