I am trying to create a dataclass that can generate a clickhouse (sql-ish) schema from type definitions.
clickhouse for example supports:
- Int8
- Int16
- Uint8
- Uint16
In python, these types don't exist (afaik). It's just int. I am hoping I can still create a type structure that let's me express it and auto generate my desired schema and maybe run validation on the types. I also hope to have my type hinter (I use PyCharm, not sure about mypy) not complain when I pass a value of type int
This is my idea so far:
@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
Is there a way to make python/my type hinter understand that I want Int8 to be interchangeable with int?