mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
Subclass abcmeta (#123)
* Added compatibility with python's ABC * Added documentation * Added link
This commit is contained in:
committed by
Samuel Colvin
parent
f0fda8c2a4
commit
06008146fe
@@ -3,6 +3,10 @@
|
||||
History
|
||||
-------
|
||||
|
||||
v0.6.5 (2018-02-XX)
|
||||
...................
|
||||
* added compatibility with abstract base classes (ABCs) #123
|
||||
|
||||
v0.6.4 (2018-02-01)
|
||||
...................
|
||||
* allow python date and times objects #122
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import abc
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class FooBarModel(BaseModel, abc.ABC):
|
||||
a: str
|
||||
b: int
|
||||
|
||||
@abc.abstractmethod
|
||||
def my_abstract_method(self):
|
||||
pass
|
||||
@@ -335,6 +335,14 @@ Using the same plumbing as ``copy()`` pydantic models support efficient pickling
|
||||
|
||||
.. literalinclude:: examples/ex_pickle.py
|
||||
|
||||
Abstract Base Classes
|
||||
.....................
|
||||
|
||||
Pydantic models can be used alongside Python's
|
||||
`Abstract Base Classes <https://docs.python.org/3/library/abc.html>`_ (ABCs).
|
||||
|
||||
.. literalinclude:: examples/ex_abc.py
|
||||
|
||||
.. _benchmarks_tag:
|
||||
|
||||
Benchmarks
|
||||
|
||||
+2
-1
@@ -1,4 +1,5 @@
|
||||
import warnings
|
||||
from abc import ABCMeta
|
||||
from collections import OrderedDict
|
||||
from pathlib import Path
|
||||
from types import FunctionType
|
||||
@@ -51,7 +52,7 @@ def _extract_validators(namespace):
|
||||
return validators
|
||||
|
||||
|
||||
class MetaModel(type):
|
||||
class MetaModel(ABCMeta):
|
||||
@classmethod
|
||||
def __prepare__(mcs, *args, **kwargs):
|
||||
return OrderedDict()
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import abc
|
||||
|
||||
import pytest
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
def test_model_subclassing_abstract_base_classes():
|
||||
class Model(BaseModel, abc.ABC):
|
||||
some_field: str
|
||||
|
||||
|
||||
def test_model_subclassing_abstract_base_classes_without_implementation_raises_exception():
|
||||
class Model(BaseModel, abc.ABC):
|
||||
some_field: str
|
||||
|
||||
@abc.abstractmethod
|
||||
def my_abstract_method(self):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
@abc.abstractmethod
|
||||
def my_abstract_classmethod(cls):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
@abc.abstractmethod
|
||||
def my_abstract_staticmethod():
|
||||
pass
|
||||
|
||||
@property
|
||||
@abc.abstractmethod
|
||||
def my_abstract_property(self):
|
||||
pass
|
||||
|
||||
@my_abstract_property.setter
|
||||
@abc.abstractmethod
|
||||
def my_abstract_property(self, val):
|
||||
pass
|
||||
|
||||
with pytest.raises(TypeError) as excinfo:
|
||||
Model(some_field='some_value')
|
||||
assert str(excinfo.value) == "Can't instantiate abstract class Model with abstract methods" \
|
||||
" my_abstract_classmethod, my_abstract_method, my_abstract_property, my_abstract_staticmethod"
|
||||
Reference in New Issue
Block a user