diff --git a/pydantic/validators.py b/pydantic/validators.py index c19e81f..5002e33 100644 --- a/pydantic/validators.py +++ b/pydantic/validators.py @@ -2,6 +2,7 @@ from collections import OrderedDict from datetime import date, datetime, time, timedelta from enum import Enum from pathlib import Path +from typing import Any from .datetime_parse import parse_date, parse_datetime, parse_duration, parse_time from .exceptions import ConfigError @@ -120,6 +121,8 @@ _VALIDATORS = [ def find_validators(type_): + if type_ is Any: + return [] for val_type, validators in _VALIDATORS: if issubclass(type_, val_type): return validators diff --git a/tests/test_complex.py b/tests/test_complex.py index daaeb41..b1ae244 100644 --- a/tests/test_complex.py +++ b/tests/test_complex.py @@ -1,5 +1,5 @@ from enum import Enum -from typing import Dict, List, Set, Union +from typing import Any, Dict, List, Set, Union import pytest @@ -351,3 +351,11 @@ def test_str_enum(): with pytest.raises(ValidationError): Model(v='different') + + +def test_any_dict(): + class Model(BaseModel): + v: Dict[int, Any] = ... + assert Model(v={1: 'foobar'}).values == {'v': {1: 'foobar'}} + assert Model(v={123: 456}).values == {'v': {123: 456}} + assert Model(v={2: [1, 2, 3]}).values == {'v': {2: [1, 2, 3]}} diff --git a/tests/test_main.py b/tests/test_main.py index 19e5570..d7162b9 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -235,3 +235,11 @@ def test_set_attr_invalid(): with pytest.raises(ValueError) as exc_info: m.setattr('c', 20) assert '"UltraSimpleModel" object has no field "c"' in str(exc_info) + + +def test_any(): + class AnyModel(BaseModel): + a: Any = 10 + + assert AnyModel().a == 10 + assert AnyModel(a='foobar').a == 'foobar'