allow Any, fix #21

This commit is contained in:
Samuel Colvin
2017-05-31 12:07:29 +01:00
parent 94402d0627
commit 9ecae91dad
3 changed files with 20 additions and 1 deletions
+3
View File
@@ -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
+9 -1
View File
@@ -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]}}
+8
View File
@@ -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'