21 lines
811 B
Python
21 lines
811 B
Python
|
from pydantic import BaseModel
|
||
|
from typing import Optional
|
||
|
|
||
|
|
||
|
class AllOptional(type(BaseModel)):
|
||
|
def __new__(cls, name, bases, namespaces, **kwargs):
|
||
|
annotations = namespaces.get('__annotations__', {})
|
||
|
for base in bases:
|
||
|
annotations.update(getattr(base, '__annotations__', {}))
|
||
|
for field in annotations:
|
||
|
if not field.startswith('__'):
|
||
|
annotations[field] = Optional[annotations[field]]
|
||
|
namespaces['__annotations__'] = annotations
|
||
|
|
||
|
# Set default values
|
||
|
for field in annotations:
|
||
|
if not field.startswith('__') and field not in namespaces:
|
||
|
namespaces[field] = None # set default value to None only if it's not already set
|
||
|
|
||
|
return super().__new__(cls, name, bases, namespaces, **kwargs)
|