18 lines
656 B
Python
18 lines
656 B
Python
from typing import Optional
|
|
|
|
try:
|
|
from pydantic.main import ModelMetaclass
|
|
except:
|
|
from pydantic._internal._model_construction 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)
|