11import copy
2- import quasardb
2+ from typing import Any , Callable , Dict , List , Optional
3+
34import numpy as np
45import numpy .ma as ma
56
6- __all__ = []
7+ import quasardb
8+
9+ __all__ : List [Any ] = []
710
811
9- def _ensure_ctype (self , idx , ctype ) :
12+ def _ensure_ctype (self : Any , idx : int , ctype : quasardb . ColumnType ) -> None :
1013 assert "table" in self ._legacy_state
1114 infos = self ._legacy_state ["table" ].list_columns ()
1215 cinfo = infos [idx ]
@@ -24,7 +27,7 @@ def _ensure_ctype(self, idx, ctype):
2427 raise quasardb .IncompatibleTypeError ()
2528
2629
27- def _legacy_next_row (self , table ) :
30+ def _legacy_next_row (self : Any , table : Any ) -> Dict [ str , Any ] :
2831 if "pending" not in self ._legacy_state :
2932 self ._legacy_state ["pending" ] = []
3033
@@ -37,56 +40,56 @@ def _legacy_next_row(self, table):
3740 return self ._legacy_state ["pending" ][- 1 ]
3841
3942
40- def _legacy_current_row (self ) :
43+ def _legacy_current_row (self : Any ) -> Dict [ str , Any ] :
4144 return self ._legacy_state ["pending" ][- 1 ]
4245
4346
44- def _legacy_start_row (self , table , x ) :
47+ def _legacy_start_row (self : Any , table : Any , x : np . datetime64 ) -> None :
4548 row = _legacy_next_row (self , table )
4649 assert "$timestamp" not in row
4750 row ["$timestamp" ] = x
4851
4952
50- def _legacy_set_double (self , idx , x ) :
53+ def _legacy_set_double (self : Any , idx : int , x : float ) -> None :
5154 _ensure_ctype (self , idx , quasardb .ColumnType .Double )
5255 assert isinstance (x , float )
5356 assert idx not in _legacy_current_row (self )["by_index" ]
5457 _legacy_current_row (self )["by_index" ][idx ] = x
5558
5659
57- def _legacy_set_int64 (self , idx , x ) :
60+ def _legacy_set_int64 (self : Any , idx : int , x : int ) -> None :
5861 _ensure_ctype (self , idx , quasardb .ColumnType .Int64 )
5962 assert isinstance (x , int )
6063 assert idx not in _legacy_current_row (self )["by_index" ]
6164 _legacy_current_row (self )["by_index" ][idx ] = x
6265
6366
64- def _legacy_set_timestamp (self , idx , x ) :
67+ def _legacy_set_timestamp (self : Any , idx : int , x : np . datetime64 ) -> None :
6568 _ensure_ctype (self , idx , quasardb .ColumnType .Timestamp )
6669 assert idx not in _legacy_current_row (self )["by_index" ]
6770 _legacy_current_row (self )["by_index" ][idx ] = x
6871
6972
70- def _legacy_set_string (self , idx , x ) :
73+ def _legacy_set_string (self : Any , idx : int , x : str ) -> None :
7174 _ensure_ctype (self , idx , quasardb .ColumnType .String )
7275 assert isinstance (x , str )
7376 assert idx not in _legacy_current_row (self )["by_index" ]
7477
7578 _legacy_current_row (self )["by_index" ][idx ] = x
7679
7780
78- def _legacy_set_blob (self , idx , x ) :
81+ def _legacy_set_blob (self : Any , idx : int , x : bytes ) -> None :
7982 _ensure_ctype (self , idx , quasardb .ColumnType .Blob )
8083 assert isinstance (x , bytes )
8184 assert idx not in _legacy_current_row (self )["by_index" ]
8285
8386 _legacy_current_row (self )["by_index" ][idx ] = x
8487
8588
86- def _legacy_push (self ) :
89+ def _legacy_push (self : Any ) -> Optional [ quasardb . WriterData ] :
8790 if "pending" not in self ._legacy_state :
8891 # Extremely likely default case, no "old" rows
89- return
92+ return None
9093
9194 assert "table" in self ._legacy_state
9295 table = self ._legacy_state ["table" ]
@@ -109,7 +112,7 @@ def _legacy_push(self):
109112 all_idx = set (ctype_by_idx .keys ())
110113
111114 # Prepare data structure
112- pivoted = {"$timestamp" : [], "by_index" : {}}
115+ pivoted : Dict [ str , Any ] = {"$timestamp" : [], "by_index" : {}}
113116 for i in all_idx :
114117 pivoted ["by_index" ][i ] = []
115118
@@ -140,7 +143,6 @@ def _legacy_push(self):
140143
141144 mask = [x is None for x in xs ]
142145
143- xs_ = []
144146 if all (mask ):
145147 xs_ = ma .masked_all (len (xs ), dtype = dtype )
146148 else :
@@ -159,9 +161,11 @@ def _legacy_push(self):
159161 return push_data
160162
161163
162- def _wrap_fn (old_fn , replace_fn ):
164+ def _wrap_fn (
165+ old_fn : Callable [..., Any ], replace_fn : Callable [..., Optional [quasardb .WriterData ]]
166+ ) -> Callable [..., Any ]:
163167
164- def wrapped (self , * args , ** kwargs ) :
168+ def wrapped (self : Any , * args : Any , ** kwargs : Any ) -> Any :
165169 data = replace_fn (self )
166170 if data :
167171 return old_fn (self , data , * args , ** kwargs )
@@ -171,7 +175,7 @@ def wrapped(self, *args, **kwargs):
171175 return wrapped
172176
173177
174- def extend_writer (x ) :
178+ def extend_writer (x : Any ) -> None :
175179 """
176180 Extends the writer with the "old", batch inserter API. This is purely
177181 a backwards compatibility layer, and we want to avoid having to maintain that
0 commit comments