55import logging
66import time
77from datetime import datetime , timezone
8- from typing import Any , Callable , Dict , List , Mapping , Optional , Sequence , Tuple
8+ from typing import (
9+ Any ,
10+ Callable ,
11+ Dict ,
12+ List ,
13+ Mapping ,
14+ Optional ,
15+ Sequence ,
16+ Set ,
17+ Tuple ,
18+ Union ,
19+ )
920
1021import clickhouse_driver
1122import numpy
@@ -156,7 +167,7 @@ def __init__(
156167 self ._client = client
157168 # The following attributes belong to the batched insert mechanism.
158169 # Inserting in batches is much faster than inserting single rows.
159- self ._str_cols = set ()
170+ self ._str_cols : Set [ str ] = set ()
160171 self ._insert_query : str = ""
161172 self ._insert_queue : List [Dict [str , Any ]] = []
162173 self ._last_insert = time .time ()
@@ -176,13 +187,16 @@ def append(
176187 self ._insert_query = f"INSERT INTO { self .cid } (`_draw_idx`,`{ names } `) VALUES"
177188 self ._str_cols = {k for k , v in params .items () if "str" in numpy .asarray (v ).dtype .name }
178189
179- # Convert str ndarrays to lists
190+ params_ins : Dict [str , Union [numpy .ndarray , int , float , List [str ]]] = {
191+ "_draw_idx" : self ._draw_idx ,
192+ ** params ,
193+ }
194+ # Convert str-dtyped ndarrays to lists
180195 for col in self ._str_cols :
181- params [col ] = params [col ].tolist ()
196+ params_ins [col ] = params [col ].tolist ()
182197
183198 # Queue up for insertion
184- params ["_draw_idx" ] = self ._draw_idx
185- self ._insert_queue .append (params )
199+ self ._insert_queue .append (params_ins )
186200 self ._draw_idx += 1
187201
188202 if (
@@ -242,13 +256,14 @@ def _get_rows(
242256
243257 # Without draws return empty arrays of the correct shape/dtype
244258 if not draws :
245- if is_rigid (nshape ):
246- return numpy .empty (shape = [0 ] + nshape , dtype = dtype )
259+ if is_rigid (nshape ) and nshape is not None :
260+ return numpy .empty (shape = [0 , * nshape ] , dtype = dtype )
247261 return numpy .array ([], dtype = object )
248262
249263 # The unpacking must also account for non-rigid shapes
250264 # and str-dtyped empty arrays default to fixed length 1 strings.
251265 # The [None] list is slower, but more flexible in this regard.
266+ buffer : Union [numpy .ndarray , Sequence ]
252267 if is_rigid (nshape ) and dtype != "str" :
253268 assert nshape is not None
254269 buffer = numpy .empty ((draws , * nshape ), dtype )
@@ -292,7 +307,7 @@ def __init__(
292307 self ,
293308 meta : RunMeta ,
294309 * ,
295- created_at : datetime = None ,
310+ created_at : Optional [ datetime ] = None ,
296311 client_fn : Callable [[], clickhouse_driver .Client ],
297312 ) -> None :
298313 self ._client_fn = client_fn
@@ -331,8 +346,8 @@ class ClickHouseBackend(Backend):
331346
332347 def __init__ (
333348 self ,
334- client : clickhouse_driver .Client = None ,
335- client_fn : Callable [[], clickhouse_driver .Client ] = None ,
349+ client : Optional [ clickhouse_driver .Client ] = None ,
350+ client_fn : Optional [ Callable [[], clickhouse_driver .Client ] ] = None ,
336351 ):
337352 """Create a ClickHouse backend around a database client.
338353
0 commit comments