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

Field(alias=...) not honored by validate_arguments decorator #2429

Closed
3 tasks done
jwodder opened this issue Mar 1, 2021 · 4 comments
Closed
3 tasks done

Field(alias=...) not honored by validate_arguments decorator #2429

jwodder opened this issue Mar 1, 2021 · 4 comments
Labels
feature request help wanted Pull Request welcome

Comments

@jwodder
Copy link

jwodder commented Mar 1, 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
            pydantic compiled: True
                 install path: /Users/jwodder/work/sand/pydantic/venv/lib/python3.9/site-packages/pydantic
               python version: 3.9.2 (default, Feb 19 2021, 17:43:04)  [Clang 12.0.0 (clang-1200.0.32.29)]
                     platform: macOS-11.2.1-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']

The following program:

from pydantic import Field, validate_arguments
from pydantic.typing import Annotated

@validate_arguments
def foo(bar: Annotated[int, Field(alias="quux")]):
    print(bar)

foo(quux=42)

fails with the following error:

Traceback (most recent call last):
  File "/Users/jwodder/work/sand/pydantic/hyphen03.py", line 8, in <module>
    foo(quux=42)
  File "pydantic/decorator.py", line 52, in pydantic.decorator.validate_arguments.validate.wrapper_function
  File "pydantic/decorator.py", line 145, in pydantic.decorator.ValidatedFunction.call
  File "pydantic/decorator.py", line 142, in pydantic.decorator.ValidatedFunction.init_model_instance
  File "pydantic/main.py", line 391, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 2 validation errors for Foo
bar
  field required (type=value_error.missing)
kwargs
  unexpected keyword argument: 'quux' (type=type_error)

The alias parameter to Field evidently has no effect when used with validate_arguments. I can find no mention of this limitation in the docs, and so I assume it is a bug.

@jwodder jwodder added the bug V1 Bug related to Pydantic V1.X label Mar 1, 2021
@samuelcolvin
Copy link
Member

Related to #2176.

I would argue that this is a feature request, rather than a bug, we don't say anywhere (AFAIK) that alias on is supported with validate_assignment.

I do think support would make sense though.

@samuelcolvin samuelcolvin added feature request help wanted Pull Request welcome and removed bug V1 Bug related to Pydantic V1.X labels Mar 1, 2021
@spawn-guy
Copy link

spawn-guy commented Mar 1, 2021

@samuelcolvin we are experiencing same issues. this started happening after our recent dependency update to ==1.8 from ==1.7.3.

previously, we had a chance to populate by alias or/and by resulting field name. now this feature is broken.

class SomeModel(BaseModel):
    some_field: str = Field(..., alias='some_old_field_name')

    class Config:
        extra: Extra.ignore
        allow_population_by_alias = True

then both of this worked

SomeModel(some_field='value')
SomeModel(some_old_field_name='value')

now we receive:

pydantic.error_wrappers.ValidationError: 3 validation errors for SomeModel
some_old_field_name
  field required (type=value_error.missing)

we will rollback to 1.7.* for now

@PrettyWood
Copy link
Member

PrettyWood commented Mar 2, 2021

@spawn-guy You just need to have a quick look at the changelog.
Right at the beginning

Breaking Change, remove old deprecation aliases from v1, #2415 by @samuelcolvin:
    ...
    remove Config.allow_population_by_alias which was replaced by Config.allow_population_by_field_name
    ...

So just use allow_population_by_field_name = True and it works

@erudin
Copy link

erudin commented Feb 9, 2023

It seems that is issue is already fixed by PR #3019.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request help wanted Pull Request welcome
Projects
None yet
Development

No branches or pull requests

5 participants