Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I get type error if I use __root__ from pydantic while inheriting from SQLModel #281

Open
8 tasks done
jd-solanki opened this issue Mar 24, 2022 · 0 comments
Open
8 tasks done
Labels
question Further information is requested

Comments

@jd-solanki
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from sqlmodel import SQLModel
from pydantic import BaseModel

data = [
    { "id": 1, "name": "awesome-product" }
]

class ProductBase(SQLModel):
    name: str

class ProductOut(ProductBase):
    id: int

# Here 👋  If I inherit from `SQLModel`` then I get type error. However, If I inherit from `BaseModel` then I don't get error.
# UnComment below line and comment the `SQLModel` usage to resolve the type error
# class ProductList(BaseModel):
class ProductList(SQLModel):
    __root__: list[ProductOut]

class SomeResponse(SQLModel):
    products: ProductList
    msg: str

product_list_model = ProductList.parse_obj(data)
SomeResponse(products=product_list_model, msg="Hello world")

Description

I get a type error if I inherit ProductList model from SQLModel saying:

Argument of type "SQLModel" cannot be assigned to parameter "products" of type "ProductList" in function "__init__"
  "SQLModel" is incompatible with "ProductList"

However, If I use BaseModel from pydantic for inheritance error went away.

Below line gives type error

class ProductList(SQLModel):

Below line looks fine

class ProductList(BaseModel):

Operating System

Linux

Operating System Details

Ubuntu 21.10

SQLModel Version

0.0.6

Python Version

3.10.2

Additional Context

image

@jd-solanki jd-solanki added the question Further information is requested label Mar 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant