Skip to content

Commit

Permalink
feat: support custom extra in validate_arguments (pydantic#3177)
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood authored and jpribyl committed Oct 7, 2021
1 parent fe7d91b commit 835c13b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
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'}

0 comments on commit 835c13b

Please sign in to comment.