如何在python aioinflux中键入``pointtype''tempe对象?
我有一个与文档中定义的数据类似:
from dataclasses import dataclass
@lineprotocol
@dataclass
class Trade:
timestamp: TIMEINT
instrument: TAGENUM
source: TAG
side: TAG
price: FLOAT
size: INT
trade_id: STR
当我尝试在aioinflux write(数据:pointType)
方法中插入该类的对象时PointType
。我使用pointType
用于提示我的输入的类型,因为这是write()
方法接受的类型。如何使我的类返回对象的pointType
类型?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将数据级对象转换为词典,然后传递到
Write
方法中。因为pointType
是类型变量定义为。这意味着
pointType
必须完全是类型映射
或dict
或bytes
或pd.dataframe < /code>(如果安装了熊猫)
Dataclasses模块已经提供了一个函数来转换 dataclass obj to dict 足以使静态类型的检查器快乐!
You can convert your dataclass object into a dictionary and pass into
write
method. BecausePointType
is a type variable which is defined as.This means that
PointType
must be exactly of typemapping
ordict
orbytes
orpd.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!
正如另一个答案表明,您可以从
pointType
实例的一种类型中继承。dict
在我的情况下工作:as the other answer suggest you can make your class inherit from one of the types that the
PointType
is an instance of.dict
worked in my case like this: