Let's say I have the following singleton:
from typing import Union, TypeVar
from typing import Literal
from pydantic import BaseModel
class UnidentifiedType(BaseModel):
_instance = None
def __init__(self):
if UnidentifiedType._instance is None:
Unidentified._instance = self
@property
def instance(self):
if self._instance is None:
UnidentifiedType()
return self._instance
Unidentified = UnidentifiedType.instance
Then I make the following typing alias:
Identifiable = Union[Literal[Unidentified], TypeVar("T")]
I already know that I can make a model and give a field bar
a default value of Unidentified
like this:
class foo(BaseModel):
bar: Identifiable[str] = Unidentified
But how do I make it so that any field of type Indentifiable[something] has a default value of Unidentified
?
In other words, is there a way to do:
class foo(BaseModel):
bar: Identifiable[str]
And have bar
still have a default value of Unidentified
?