I have a dataclass similar to the one defined in the docs :
from dataclasses import dataclass
@lineprotocol
@dataclass
class Trade:
timestamp: TIMEINT
instrument: TAGENUM
source: TAG
side: TAG
price: FLOAT
size: INT
trade_id: STR
when I try to insert objects of this class in aioinflux write(data: PointType)
method I get a static typing warning that my object is not of PointType
. I used PointType
for type hinting my input because this is the type that the write()
method accepts. How can I make my class return objects of PointType
type?
You can convert your dataclass object into a dictionary and pass into write
method. Because PointType
is a type variable which is defined as.
if pd:
PointType = TypeVar('PointType', Mapping, dict, bytes, pd.DataFrame)
.....
else:
PointType = TypeVar('PointType', Mapping, dict, bytes)
.....
This means that PointType
must be exactly of type mapping
or dict
or bytes
or pd.DataFrame
(if pandas is installed)
The dataclasses module already provides a function to convert dataclass obj to a dict which is sufficient to make the static type checker happy!
from dataclasses import asdict
your_write_method(asdict(your_dataclass_object))