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

Alias ignored on custom root type #3560

Closed
3 tasks done
okonos opened this issue Dec 22, 2021 · 1 comment
Closed
3 tasks done

Alias ignored on custom root type #3560

okonos opened this issue Dec 22, 2021 · 1 comment
Assignees
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@okonos
Copy link

okonos commented Dec 22, 2021

Checks

  • I added a descriptive title to this issue
  • I have searched (google, github) for similar issues and couldn't find anything
  • I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.8.2
            pydantic compiled: True
                 install path: /home/user/.local/share/virtualenvs/pydanticvenv/lib/python3.9/site-packages/pydantic
               python version: 3.9.5 (default, Nov 18 2021, 16:00:48)  [GCC 10.3.0]
                     platform: Linux-5.11.0-41-generic-x86_64-with-glibc2.33
     optional deps. installed: ['typing-extensions']

The provided snippet works fine on 1.7.4, but does not on 1.8.x.
git bisect points to #2238 as first bad commit. The culprit seems to be the validate method, where value is wrapped with the dict under ROOT_KEY, leading to validation error.

from pydantic import BaseModel, Field


class NestedModel(BaseModel):
    __root__: int = Field(..., alias='x')


class MyModel(BaseModel):
    v: NestedModel


MyModel(**{'v': {'x': 2}})
# pydantic.error_wrappers.ValidationError: 1 validation error for MyModel
# v -> x
#   field required (type=value_error.missing)
@okonos okonos added the bug V1 Bug related to Pydantic V1.X label Dec 22, 2021
@Kludex
Copy link
Member

Kludex commented Aug 21, 2023

Since this is stale, I'll be closing it. Sorry for the long delay.

I don't think the behavior you describe was ever supposed to be supported, since you can achieve it with the following:

from pydantic import BaseModel
from typing_extensions import TypedDict


class NestedModel(TypedDict):
    x: int


class MyModel(BaseModel):
    v: NestedModel


MyModel(**{"v": {"x": 1}})

Note

The above works on either V1 and V2.

Hope this is enough. If not, please open a new issue. 🙏

@Kludex Kludex closed this as not planned Won't fix, can't repro, duplicate, stale Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

No branches or pull requests

3 participants