[Python] Generic of Class ignored by Generic Pydantic constructor

Discussão em 'Python' iniciado por Stack, Dezembro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I have a generic class with a function that returns a Pydantic model where one of the fields is the generic type. What follows is a code snippet that defines two classes the generic GetValue and GetInt. I don't understand why the behavior of GetValue[int] is not the same as GetInt.

    from typing import Any, Generic, TypeVar

    from pydantic import BaseModel

    X = TypeVar("X")


    class TestModel(BaseModel, Generic[X]):
    a: X


    T = TypeVar("T")


    class GetValue(Generic[T]):

    @classmethod
    def get_value(cls, x: Any) -> TestModel[T]:
    return TestModel[T](a=x)


    class GetInt:
    @classmethod
    def get_value(cls, x: Any) -> TestModel[int]:
    return TestModel[int](a=x)


    res = GetValue[int].get_value("1")
    assert type(res.a) == str


    res_2 = GetInt.get_value("1")

    assert type(res_2.a) == int

    assert res.a != res_2.a # I want these two to be equal, not sure why they are different.

    x: Any = "1"
    res_3 = TestModel[int](a=x)
    assert type(res_3.a) == int


    Inspecting things in my debugger I see that in this function call GetValue[int].get_value("1") T is just a TypeVar. This causes Pydantic to fall back on an Any annotation for a so it stays as a str. res_3 shows that when the type is supplied to the Pydantic class it will convert the string to an int. I'm not sure why this construction isn't working how I'd expect. Still new to python's typing system, was hoping someone could shed light on this.

    Python version: Python 3.12.5

    pydantic version: 2.10.0

    Continue reading...

Compartilhe esta Página