pydbantic icon indicating copy to clipboard operation
pydbantic copied to clipboard

Any type for pydbantic DataBaseModel

Open ftasbasi opened this issue 2 years ago • 1 comments

Your environment pydbantic Version: 0.0.38 Python Version: 3.9

What happened? I was trying to create a sample sqlite database with a simple table schema as following

from typing import Dict, Any
from pydbantic import DataBaseModel, Database

class XTable(DataBaseModel):
    attr1: str
    attr2: Dict[str, Any]
    
db = Database.create(
    f'sqlite:///zzz.db',
    tables=[XTable]
)

What did you expect to happen? zzz.db to be created with XTable inside

Steps to reproduce Run the code above

What is the error?

Traceback (most recent call last):
  File "/Users/me/xxx/yyy/hebele.py", line 12, in <module>
    db = Database.create(
  File "/Users/me/xxx/yyy/lib/python3.9/site-packages/pydbantic/database.py", line 655, in create
    new_db = cls(
  File "/Users/me/xxx/yyy/lib/python3.9/site-packages/pydbantic/database.py", line 81, in __init__
    self.add_table(table)
  File "/Users/me/xxx/yyy/lib/python3.9/site-packages/pydbantic/database.py", line 163, in add_table
    table.setup(self)
  File "/Users/me/xxx/yyy/lib/python3.9/site-packages/pydbantic/core.py", line 516, in setup
    cls.update_backward_refs()
  File "/Users/me/xxx/yyy/lib/python3.9/site-packages/pydbantic/core.py", line 670, in update_backward_refs
    data_base_model = cls.check_if_subtype({"type": model_field.type_})
  File "/Users/me/xxx/yyy/lib/python3.9/site-packages/pydbantic/core.py", line 441, in check_if_subtype
    elif issubclass(field["type"], DataBaseModel):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/abc.py", line 123, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class

when i use my own type like Union[str, int] instead of Any, it works. It seems like Any is not supported here. Am I missing something?

ftasbasi avatar Apr 25 '23 19:04 ftasbasi

@ftasbasi , you are spot on, Any type is not currently supported because it is not explicit enough for serialization. I will still mark this one as a potential fix to notify users away from Any usage instead of the current more cryptic issubclass error

codemation avatar Apr 27 '23 23:04 codemation