1+ from __future__ import annotations
12import numpy as np
23from struct import pack , unpack_from
4+ from typing import Any
35
46
57class HalfVector :
6- def __init__ (self , value ) :
8+ def __init__ (self , value : Any ) -> None :
79 # asarray still copies if same dtype
810 if not isinstance (value , np .ndarray ) or value .dtype != '>f2' :
911 value = np .asarray (value , dtype = '>f2' )
@@ -13,40 +15,40 @@ def __init__(self, value):
1315
1416 self ._value = value
1517
16- def __repr__ (self ):
18+ def __repr__ (self ) -> str :
1719 return f'HalfVector({ self .to_list ()} )'
1820
19- def __eq__ (self , other ) :
21+ def __eq__ (self , other : Any ) -> bool :
2022 if isinstance (other , self .__class__ ):
2123 return np .array_equal (self .to_numpy (), other .to_numpy ())
2224 return False
2325
24- def dimensions (self ):
26+ def dimensions (self ) -> int :
2527 return len (self ._value )
2628
27- def to_list (self ):
29+ def to_list (self ) -> list [ float ] :
2830 return self ._value .tolist ()
2931
30- def to_numpy (self ):
32+ def to_numpy (self ) -> np . ndarray :
3133 return self ._value
3234
33- def to_text (self ):
35+ def to_text (self ) -> str :
3436 return '[' + ',' .join ([str (float (v )) for v in self ._value ]) + ']'
3537
36- def to_binary (self ):
38+ def to_binary (self ) -> bytes :
3739 return pack ('>HH' , self .dimensions (), 0 ) + self ._value .tobytes ()
3840
3941 @classmethod
40- def from_text (cls , value ) :
42+ def from_text (cls , value : str ) -> HalfVector :
4143 return cls ([float (v ) for v in value [1 :- 1 ].split (',' )])
4244
4345 @classmethod
44- def from_binary (cls , value ) :
46+ def from_binary (cls , value : bytes ) -> HalfVector :
4547 dim , unused = unpack_from ('>HH' , value )
4648 return cls (np .frombuffer (value , dtype = '>f2' , count = dim , offset = 4 ))
4749
4850 @classmethod
49- def _to_db (cls , value , dim = None ):
51+ def _to_db (cls , value : Any , dim : int | None = None ) -> str | None :
5052 if value is None :
5153 return value
5254
@@ -59,7 +61,7 @@ def _to_db(cls, value, dim=None):
5961 return value .to_text ()
6062
6163 @classmethod
62- def _to_db_binary (cls , value ) :
64+ def _to_db_binary (cls , value : Any ) -> bytes | None :
6365 if value is None :
6466 return value
6567
@@ -69,15 +71,15 @@ def _to_db_binary(cls, value):
6971 return value .to_binary ()
7072
7173 @classmethod
72- def _from_db (cls , value ) :
73- if value is None or isinstance (value , cls ):
74+ def _from_db (cls , value : str | HalfVector | None ) -> HalfVector | None :
75+ if value is None or isinstance (value , HalfVector ):
7476 return value
7577
7678 return cls .from_text (value )
7779
7880 @classmethod
79- def _from_db_binary (cls , value ) :
80- if value is None or isinstance (value , cls ):
81+ def _from_db_binary (cls , value : bytes | HalfVector | None ) -> HalfVector | None :
82+ if value is None or isinstance (value , HalfVector ):
8183 return value
8284
8385 return cls .from_binary (value )
0 commit comments