wd-smebiz/utils/pydantic_utils.py

19 lines
635 B
Python

from typing import Optional
try:
from pydantic.main import ModelMetaclass
except:
from pydantic.v1.main import ModelMetaclass
class AllOptional(ModelMetaclass):
def __new__(cls, name, bases, namespaces, **kwargs):
annotations = namespaces.get('__annotations__', {})
for base in bases:
annotations.update(base.__annotations__)
for field in annotations:
if not field.startswith('__'):
annotations[field] = Optional[annotations[field]]
namespaces['__annotations__'] = annotations
return super().__new__(cls, name, bases, namespaces, **kwargs)