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

feat: support custom extra in validate_arguments #3177

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/3161-PrettyWood.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`validate_arguments` now supports `extra` customization (used to always be `Extra.forbid`)
2 changes: 1 addition & 1 deletion pydantic/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,6 @@ def check_duplicate_kwargs(cls, v: Optional[List[str]]) -> None:
raise TypeError(f'multiple values for argument{plural}: {keys}')

class Config(CustomConfig):
extra = Extra.forbid
extra = getattr(CustomConfig, 'extra', Extra.forbid)

self.model = create_model(to_camel(self.raw_function.__name__), __base__=DecoratorBaseModel, **fields)
21 changes: 19 additions & 2 deletions tests/test_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from typing import List

import pytest
from typing_extensions import Annotated
from typing_extensions import Annotated, TypedDict

from pydantic import BaseModel, Field, ValidationError, validate_arguments
from pydantic import BaseModel, Extra, Field, ValidationError, validate_arguments
from pydantic.decorator import ValidatedFunction
from pydantic.errors import ConfigError

Expand Down Expand Up @@ -427,3 +427,20 @@ def foo(dt: datetime = Field(default_factory=lambda: 946684800), /):
)
assert module.foo() == datetime(2000, 1, 1, tzinfo=timezone.utc)
assert module.foo(0) == datetime(1970, 1, 1, tzinfo=timezone.utc)


def test_validate_extra():
class TypedTest(TypedDict):
y: str

@validate_arguments(config={'extra': Extra.allow})
def test(other: TypedTest):
return other

assert test(other={'y': 'b', 'z': 'a'}) == {'y': 'b', 'z': 'a'}

@validate_arguments(config={'extra': Extra.ignore})
def test(other: TypedTest):
return other

assert test(other={'y': 'b', 'z': 'a'}) == {'y': 'b'}