Files
pydantic/docs/examples/validation_decorator_config.py
Samuel Colvin 9c4860ce96 Valdiate arguments config (#1663)
* add `configs` to validate_arguments

* simplify `validate_arguments` and add annotation for parameter `configs`

* change double quotes to single quotes

* reformat code

* fix mypy error

* fix mypy 'maximum semantic analysis' error

* rename 'configs' > 'config_params'

* change name and usage, start tests

* prevent setting fields on custom config

* add docs and fix mypy

* tweak docs

* add change

Co-authored-by: quantpy <quantpy@qq.com>
2020-09-06 23:17:52 +01:00

27 lines
517 B
Python

from pydantic import ValidationError, validate_arguments
class Foobar:
def __init__(self, v: str):
self.v = v
def __add__(self, other: 'Foobar') -> str:
return f'{self} + {other}'
def __str__(self) -> str:
return f'Foobar({self.v})'
@validate_arguments(config=dict(arbitrary_types_allowed=True))
def add_foobars(a: Foobar, b: Foobar):
return a + b
c = add_foobars(Foobar('a'), Foobar('b'))
print(c)
try:
add_foobars(1, 2)
except ValidationError as e:
print(e)