]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Declare types for dynamically populated lists in pool tests
authorDenis Laxalde <denis.laxalde@dalibo.com>
Tue, 2 Nov 2021 12:40:27 +0000 (13:40 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 3 Nov 2021 15:55:14 +0000 (16:55 +0100)
tests/pool/test_pool.py
tests/pool/test_pool_async.py

index 416b9bde0d79e422d9241ef1ee41b99bd35d4e3e..47a17a252a852bdb9e3c9e524ef7d7a3df4503de 100644 (file)
@@ -4,7 +4,7 @@ import weakref
 from time import sleep, time
 from threading import Thread, Event
 from collections import Counter
-from typing import Any
+from typing import Any, List, Tuple
 
 import pytest
 
@@ -114,7 +114,7 @@ def test_concurrent_filling(dsn, monkeypatch, retries):
 
     for retry in retries:
         with retry:
-            times = []
+            times: List[float] = []
             t0 = time()
 
             with pool.ConnectionPool(dsn, min_size=5, num_workers=2) as p:
@@ -301,7 +301,7 @@ def test_queue(dsn, retries):
 
     for retry in retries:
         with retry:
-            results = []
+            results: List[Tuple[int, float, int]] = []
             with pool.ConnectionPool(dsn, min_size=2) as p:
                 p.wait()
                 ts = [Thread(target=worker, args=(i,)) for i in range(6)]
@@ -331,8 +331,8 @@ def test_queue_size(dsn):
         else:
             success.append(True)
 
-    errors = []
-    success = []
+    errors: List[Exception] = []
+    success: List[bool] = []
 
     with pool.ConnectionPool(dsn, min_size=1, max_waiting=3) as p:
         p.wait()
@@ -374,8 +374,8 @@ def test_queue_timeout(dsn, retries):
 
     for retry in retries:
         with retry:
-            results = []
-            errors = []
+            results: List[Tuple[int, float, int]] = []
+            errors: List[Tuple[int, float, Exception]] = []
 
             with pool.ConnectionPool(dsn, min_size=2, timeout=0.1) as p:
                 ts = [Thread(target=worker, args=(i,)) for i in range(4)]
@@ -402,7 +402,7 @@ def test_dead_client(dsn):
             if timeout > 0.2:
                 raise
 
-    results = []
+    results: List[int] = []
 
     with pool.ConnectionPool(dsn, min_size=2) as p:
         ts = [
@@ -438,8 +438,8 @@ def test_queue_timeout_override(dsn, retries):
 
     for retry in retries:
         with retry:
-            results = []
-            errors = []
+            results: List[Tuple[int, float, int]] = []
+            errors: List[Tuple[int, float, Exception]] = []
 
             with pool.ConnectionPool(dsn, min_size=2, timeout=0.1) as p:
                 ts = [Thread(target=worker, args=(i,)) for i in range(4)]
@@ -652,7 +652,7 @@ def test_closed_queue(dsn, retries):
     for retry in retries:
         with retry:
             p = pool.ConnectionPool(dsn, min_size=1)
-            success = []
+            success: List[str] = []
 
             t1 = Thread(target=w1)
             t2 = Thread(target=w2)
@@ -683,7 +683,7 @@ def test_grow(dsn, monkeypatch, retries):
                 dsn, min_size=2, max_size=4, num_workers=3
             ) as p:
                 p.wait(1.0)
-                results = []
+                results: List[Tuple[int, float]] = []
 
                 ts = [Thread(target=worker, args=(i,)) for i in range(6)]
                 for t in ts:
@@ -703,7 +703,7 @@ def test_shrink(dsn, monkeypatch):
 
     from psycopg_pool.pool import ShrinkPool
 
-    results = []
+    results: List[Tuple[int, int]] = []
 
     def run_hacked(self, pool):
         n0 = pool._nconns
@@ -843,7 +843,7 @@ def test_resize(dsn):
         with p.connection() as conn:
             conn.execute("select pg_sleep(%s)", [t])
 
-    size = []
+    size: List[int] = []
 
     with pool.ConnectionPool(dsn, min_size=2, max_idle=0.2) as p:
         s = Thread(target=sampler)
index 58abfd5dd4f50230d6f6d39667f5ea3e7cf92605..d2542d3137f307d588398c617f408c1c61303c01 100644 (file)
@@ -3,7 +3,7 @@ import asyncio
 import logging
 from time import time
 from collections import Counter
-from typing import Any
+from typing import Any, List, Tuple
 
 import pytest
 
@@ -112,7 +112,7 @@ async def test_concurrent_filling(dsn, monkeypatch, retries):
 
     async for retry in retries:
         with retry:
-            times = []
+            times: List[float] = []
             t0 = time()
 
             async with pool.AsyncConnectionPool(
@@ -312,7 +312,7 @@ async def test_queue(dsn, retries):
 
     async for retry in retries:
         with retry:
-            results = []
+            results: List[Tuple[int, float, int]] = []
             async with pool.AsyncConnectionPool(dsn, min_size=2) as p:
                 await p.wait()
                 ts = [create_task(worker(i)) for i in range(6)]
@@ -339,8 +339,8 @@ async def test_queue_size(dsn):
         else:
             success.append(True)
 
-    errors = []
-    success = []
+    errors: List[Exception] = []
+    success: List[bool] = []
 
     async with pool.AsyncConnectionPool(dsn, min_size=1, max_waiting=3) as p:
         await p.wait()
@@ -379,8 +379,8 @@ async def test_queue_timeout(dsn, retries):
 
     async for retry in retries:
         with retry:
-            results = []
-            errors = []
+            results: List[Tuple[int, float, int]] = []
+            errors: List[Tuple[int, float, Exception]] = []
 
             async with pool.AsyncConnectionPool(
                 dsn, min_size=2, timeout=0.1
@@ -407,7 +407,7 @@ async def test_dead_client(dsn):
                 raise
 
     async with pool.AsyncConnectionPool(dsn, min_size=2) as p:
-        results = []
+        results: List[int] = []
         ts = [
             create_task(worker(i, timeout))
             for i, timeout in enumerate([0.4, 0.4, 0.1, 0.4, 0.4])
@@ -440,8 +440,8 @@ async def test_queue_timeout_override(dsn, retries):
 
     async for retry in retries:
         with retry:
-            results = []
-            errors = []
+            results: List[Tuple[int, float, int]] = []
+            errors: List[Tuple[int, float, Exception]] = []
 
             async with pool.AsyncConnectionPool(
                 dsn, min_size=2, timeout=0.1
@@ -645,7 +645,7 @@ async def test_closed_queue(dsn, retries):
     async for retry in retries:
         with retry:
             p = pool.AsyncConnectionPool(dsn, min_size=1)
-            success = []
+            success: List[str] = []
 
             t1 = create_task(w1())
             await asyncio.sleep(0.1)
@@ -674,7 +674,7 @@ async def test_grow(dsn, monkeypatch, retries):
             ) as p:
                 await p.wait(1.0)
                 ts = []
-                results = []
+                results: List[Tuple[int, float]] = []
 
                 ts = [create_task(worker(i)) for i in range(6)]
                 await asyncio.gather(*ts)
@@ -691,7 +691,7 @@ async def test_shrink(dsn, monkeypatch):
 
     from psycopg_pool.pool_async import ShrinkPool
 
-    results = []
+    results: List[Tuple[int, int]] = []
 
     async def run_hacked(self, pool):
         n0 = pool._nconns
@@ -837,7 +837,7 @@ async def test_resize(dsn):
         async with p.connection() as conn:
             await conn.execute("select pg_sleep(%s)", [t])
 
-    size = []
+    size: List[int] = []
 
     async with pool.AsyncConnectionPool(dsn, min_size=2, max_idle=0.2) as p:
         s = create_task(sampler())