mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
feat: support custom extra in validate_arguments (#3177)
This commit is contained in:
@@ -0,0 +1 @@
|
||||
`validate_arguments` now supports `extra` customization (used to always be `Extra.forbid`)
|
||||
@@ -254,6 +254,6 @@ class ValidatedFunction:
|
||||
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)
|
||||
|
||||
+19
-2
@@ -6,9 +6,9 @@ from pathlib import Path
|
||||
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
|
||||
|
||||
@@ -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'}
|
||||
|
||||
Reference in New Issue
Block a user