wd-smebiz/utils/pydantic_utils.py

18 lines
656 B
Python
Raw Normal View History

2023-08-02 14:24:28 +08:00
from typing import Optional
2023-08-02 16:32:36 +08:00
try:
from pydantic.main import ModelMetaclass
except:
2023-08-02 18:29:26 +08:00
from pydantic._internal._model_construction import ModelMetaclass
2023-08-02 14:24:28 +08:00
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)