Skip to content

parse_obj return type is SQLModel #317

@phi-friday

Description

@phi-friday

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

class test(SQLModel):
    attr: int
    desc: str

class real(test, table=True):
    val: float

test_inst: test = test(attr=1, desc='test_inst')
real_inst: real = real.parse_obj(test_inst.dict())
"""
(method) parse_obj: (obj: Any, update: Dict[str, Any] | None = None) -> SQLModel
Expression of type "SQLModel" cannot be assigned to declared type "real"
  "SQLModel" is incompatible with "real"PylancereportGeneralTypeIssues
"""

Description

real.parse_obj return type should be real.

Wanted Solution

# sqlmodel/main.py

from typing import TypeVar
_T = TypeVar("_T", bound="SQLModel")

(...)

# line no.569
    @classmethod
    def parse_obj(
        cls: Type[_T], obj: Any, update: Optional[Dict[str, Any]] = None
    ) -> _T:
        obj = cls._enforce_dict_if_root(obj)
        # SQLModel, support update dict
        if update is not None:
            obj = {**obj, **update}
        # End SQLModel support dict
        return super().parse_obj(obj)

Wanted Code

...

Alternatives

It's being used temporarily like this.

from typing import Any, TypeVar, cast

from sqlmodel import SQLModel

_T = TypeVar("_T", bound=SQLModel)


class fix_parse_obj_model(SQLModel):
    @classmethod
    def parse_obj(cls: type[_T], obj: Any, update: dict[str, Any] | None = None) -> _T:
        return cast(_T, super().parse_obj(obj, update))

Operating System

Linux

Operating System Details

OS: Arch Linux x86_64
Host: 82FX Yoga Slim 7 Pro 14ITL5
Kernel: 5.17.4-arch1-1
Uptime: 3 hours, 32 mins
Packages: 969 (pacman), 23 (flatpak)
Shell: zsh 5.8.1
Resolution: 2240x1400, 2560x1080
DE: GNOME 42.0
WM: Mutter
WM Theme: Adwaita
Theme: Arc [GTK2/3]
Icons: Papirus [GTK2/3]
Terminal: vscode
CPU: 11th Gen Intel i5-1135G7 (8) @ 4.200GHz
GPU: Intel TigerLake-LP GT2 [Iris Xe Graphics]
GPU: NVIDIA GeForce MX450
Memory: 5423MiB / 15785MiB

SQLModel Version

0.0.6

Python Version

Python 3.10.4

Additional Context

I'd like to do a "pull requests"
shame I'm not used to git yet...

Metadata

Metadata

Assignees

No one assigned

    Labels

    answeredbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions