Skip to content

Define schemas with pydantic #9969

Answered by gforsyth
choucavalier asked this question in Q&A
Discussion options

You must be logged in to vote

You could do something like this? Depending on how you want to type the dataclass, you might have to add in some type mapping (like if you were typing things using python builtin types instead of Ibis types).

>>> from dataclasses import dataclass, fields

>>> import ibis.expr.datatypes as dt

>>> @dataclass
      ...: class MyTable:
      ...:     a: dt.int32
      ...:     b: dt.str
      ...: 
      ...:     def schema(self):
      ...:         for field in fields(self):
      ...:             yield (field.name, field.type)

>>> t1 = MyTable(1, "b")

>>> t1
MyTable(a=1, b='b')

>>> ibis.schema(t1.schema())
ibis.Schema {
  a  int32
  b  string
}

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@choucavalier
Comment options

@gforsyth
Comment options

Answer selected by cpcloud
@choucavalier
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants