wd-smebiz/utils/pydantic_utils.py

19 lines
635 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:
from pydantic.v1.main 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)