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

Discriminator value represented in 'loc' of an error when ValidationError raised #7500

Closed
1 task done
alice-luc opened this issue Sep 19, 2023 · 1 comment
Closed
1 task done
Assignees
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable

Comments

@alice-luc
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When using List[Annotated[Union]] field with discriminator, its value appears in location tuple

Error i get
{
'input': '654f',
'loc': ('h', 'g', 0, 'c-discriminator', 'e', 'a'),
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'type': 'int_parsing',
'url': 'https://errors.pydantic.dev/2.3/v/int_parsing'
}

Expected error
{
'input': '654f',
'loc': ('h', 'g', 0, 'e', 'a'),
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'type': 'int_parsing',
'url': 'https://errors.pydantic.dev/2.3/v/int_parsing'
}

Example Code

from pprint import pprint
from typing import Annotated, Union, Literal, Any

from pydantic import BaseModel as BM, ConfigDict, Field, field_validator


class BaseModel(BM):
    model_config = ConfigDict(extra='forbid')


class A(BaseModel):
    a: int = 1
    b: str = "12345"


class B(BaseModel):
    c: int = 2
    d: str = "qwe"


class C(BaseModel):
    e: A
    t: Literal["c-discriminator"]


class D(BaseModel):
    f: C
    t: Literal["d-discriminator"]


DC = Annotated[
    Union[D, C],
    Field(discriminator="t")]


class F(BaseModel):
    g: list[DC]

    @field_validator("g", mode='before')
    def pre_root(cls, values: list[dict[str, Any]]):

        for v in values["g"]:
            if "e" in v:
                v["t"] = "c-discriminator"
            elif "f" in v:
                v["t"] = "d-discriminator"
        return values


class G(BaseModel):
    h: F


try:
    f = G(**{"h": {"g": [{"e": {"a": "654f"}}]}})
except Exception as e:
    pprint(e.errors())

Python, Pydantic & OS Version

pydantic version: 2.3.0
pydantic-core version: 2.6.3
pydantic-core build: profile=release pgo=false
install path: /[location]/lib/python3.11/site-packages/pydantic
python version: 3.11.4 (v3.11.4:d2340ef257, Jun  6 2023, 19:15:51) [Clang 13.0.0 (clang-1300.0.29.30)]
platform: macOS-13.5.2-arm64-arm-64bit
optional deps. installed: ['typing-extensions']
@alice-luc alice-luc added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Sep 19, 2023
@sydney-runkle
Copy link
Member

Hi @alice-luc,

Thanks for your question!

This is related to #7261 - specifically, see this comment. Let me know if you have any other questions!

@sydney-runkle sydney-runkle self-assigned this Sep 19, 2023
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 unconfirmed Bug not yet confirmed as valid/applicable
Projects
None yet
Development

No branches or pull requests

2 participants