我正在尝试创建一个数据类,它可以从类型定义生成clickhouse(SQLISH)模式
例如,clickhouse支持:
在python中,这些类型不存在(afaik)。只是int
。我希望我仍然可以创建一个类型结构,让我来表达它,并自动生成所需的模式,也许还可以对类型进行验证。我还希望当我传递int类型的值时,我的类型提示(我使用PyCharm,不确定mypy)不会抱怨
到目前为止,这是我的想法:
@dataclass
class ClickhouseType:
@classmethod
def schema_type(cls):
return cls.__name__
@classmethod
def validate(cls, value):
# just included as an example, not important as part of this discussion :)
pass
@dataclass
class Int8(int, ClickhouseType):
@classmethod
def validate(cls, value):
# just included as an example, not important as part of this discussion :)
assert -128 <= value <= 127
@dataclass
class MySchema:
some_number: Int8
x = MySchema(some_number=4) # type hint complaints that I pass an int when an Int8 is required
有没有办法让python/my-type-hinter明白我希望Int8
可以与int
互换
尝试使用直接继承创建类型:
根据shadowranger的评论提示进行编辑:
相关问题 更多 >
编程相关推荐