wd-smebiz/utils/pydantic_utils.py

22 lines
713 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:32:22 +08:00
try:
from pydantic._internal._model_construction import ModelMetaclass
except Exception as e:
raise e
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)