diff --git a/HISTORY.rst b/HISTORY.rst index edfcceb..4161d11 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -10,6 +10,7 @@ v0.3.0 (TBC) * ``raise_exception`` removed, Models now always raise exceptions #44 * instance method validators removed TODO * django-restful-framework benchmarks added #47 +* fix inheritance bug #49 v0.2.1 (2017-06-07) ................... diff --git a/pydantic/main.py b/pydantic/main.py index cf3c095..ab21c60 100644 --- a/pydantic/main.py +++ b/pydantic/main.py @@ -55,7 +55,7 @@ class MetaModel(type): fields[var_name] = Field.infer( name=var_name, value=value, - annotation=annotations and annotations.pop(var_name, None), + annotation=annotations.pop(var_name, None) if annotations else None, class_validators=class_validators, config=config, ) diff --git a/pydantic/validators.py b/pydantic/validators.py index dc2cee8..81255d3 100644 --- a/pydantic/validators.py +++ b/pydantic/validators.py @@ -124,6 +124,9 @@ def find_validators(type_): if type_ is Any: return [] for val_type, validators in _VALIDATORS: - if issubclass(type_, val_type): - return validators + try: + if issubclass(type_, val_type): + return validators + except TypeError as e: + raise TypeError(f'error checking inheritance of {type_!r} (type: {type(type_)})') from e raise ConfigError(f'no validator found for {type_}') diff --git a/tests/test_complex.py b/tests/test_complex.py index 53dfbbe..c1e7178 100644 --- a/tests/test_complex.py +++ b/tests/test_complex.py @@ -426,3 +426,22 @@ def test_values_order(): m = Model(c=30, b=20, a=10) assert list(m) == [('a', 10), ('b', 20), ('c', 30)] + + +def test_inheritance(): + class Foo(BaseModel): + a: float = ... + + class Bar(Foo): + x: float = 12.3 + a = 123 + + assert Bar().values() == {'x': 12.3, 'a': 123} + + +def test_invalid_type(): + with pytest.raises(TypeError) as exc_info: + class Model(BaseModel): + x: 43 = 123 + + assert "error checking inheritance of 43 (type: )" in str(exc_info)