mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
inheritance bug, fix #49
This commit is contained in:
@@ -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)
|
||||
...................
|
||||
|
||||
+1
-1
@@ -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,
|
||||
)
|
||||
|
||||
@@ -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_}')
|
||||
|
||||
@@ -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: <class 'int'>)" in str(exc_info)
|
||||
|
||||
Reference in New Issue
Block a user