2

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?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy