From 5bc1fcfb92aace50f18bf4ddcc915ef23e1c7431 Mon Sep 17 00:00:00 2001 From: Eric Jolibois Date: Mon, 6 Sep 2021 00:44:16 +0200 Subject: [PATCH] feat: support custom `extra` in `validate_arguments` (#3177) --- changes/3161-PrettyWood.md | 1 + pydantic/decorator.py | 2 +- tests/test_decorator.py | 21 +++++++++++++++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 changes/3161-PrettyWood.md diff --git a/changes/3161-PrettyWood.md b/changes/3161-PrettyWood.md new file mode 100644 index 0000000..3e037e5 --- /dev/null +++ b/changes/3161-PrettyWood.md @@ -0,0 +1 @@ +`validate_arguments` now supports `extra` customization (used to always be `Extra.forbid`) \ No newline at end of file diff --git a/pydantic/decorator.py b/pydantic/decorator.py index ca709b4..ced4953 100644 --- a/pydantic/decorator.py +++ b/pydantic/decorator.py @@ -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) diff --git a/tests/test_decorator.py b/tests/test_decorator.py index ea06368..97b78f8 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -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'}