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

Generic BaseModel parsing from within a generic class #9295

Open
1 task done
hwong557 opened this issue Apr 20, 2024 · 2 comments
Open
1 task done

Generic BaseModel parsing from within a generic class #9295

hwong557 opened this issue Apr 20, 2024 · 2 comments
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@hwong557
Copy link

hwong557 commented Apr 20, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I have a generic class MyParser[T] which parses a python object into a generic type Payload[T]. When specifying a concrete type (SubPayload in this case), the generic field fails to be parsed into a SubPayload.

I am aware Payload[SubPayload].model_validate works, but for my purposes, I would like the MyParser generic wrapper because in reality, it contains more logic.

Thank you.

Example Code

from typing import Generic, TypeVar

from pydantic import BaseModel

T = TypeVar("T")


class SubPayload(BaseModel):
    result: str
    conf: float


class Payload(BaseModel, Generic[T]):
    job_id: int
    response: T


class MyParser(Generic[T]):
    def parse(self, obj) -> Payload[T]:
        return Payload[T].model_validate(obj)


parser = MyParser[SubPayload]()

out = {"job_id": 999, "response": {"result": "other", "conf": 0.99}}
parsed = parser.parse(out)

assert isinstance(parsed.response, SubPayload)  # this fails!

Python, Pydantic & OS Version

pydantic version: 2.7.0
        pydantic-core version: 2.18.1
          pydantic-core build: profile=release pgo=true
                 install path: /.venv/lib/python3.11/site-packages/pydantic
               python version: 3.11.8 (main, Feb 12 2024, 14:50:05) [GCC 13.2.1 20230801]
                     platform: Linux-6.6.28-1-lts-x86_64-with-glibc2.39
             related packages: typing_extensions-4.10.0 pydantic-settings-2.2.1 fastapi-0.110.0 email-validator-2.0.0.post2 typing_extensions-4.10.0 pydantic-settings-2.2.1 fastapi-0.110.1 mypy-1.9.0 pydantic-extra-types-2.6.0
                       commit: unknown
@hwong557 hwong557 added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Apr 20, 2024
@rdeaton-freenome
Copy link

Probably same root cause as #8410

@sydney-runkle
Copy link
Member

@hwong557,

Thanks for reporting this. Definitely doesn't seem like the right behavior. PRs welcome with a fix! Could be a great issue for someone looking to learn more about our internal generic processing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation
Projects
None yet
Development

No branches or pull requests

3 participants