I have a base class BaseTemplateData
which inherits from pydantic.BaseModel
. Then, I want to have a property of the BaseTemplate
class which stores a type of a child class of BaseTemplateData
.
I'm doing the following, but I'm getting a mypy error saying Type variable "file.path.TemplateDataType" is unbound
, when I'm explicitly passing a bound
parameter to the TypeVar
call.
I also would like to have another class BaseTemplate2
which property doesn't store the type itself, but an instance of a child class of BaseTemplateData
. Would the approach be correct?
Any help would be much appreciated. Thanks!
from typing import Type, TypeVar
from pydantic import BaseModel
class BaseTemplateData(BaseModel):
"""
Base class for all templates.
"""
TemplateDataType = TypeVar("TemplateDataType", bound=BaseTemplateData)
class BaseTemplate(BaseModel):
"""
Template class for email templates
"""
data_model: Type[TemplateDataType]
class BaseTemplate2(BaseModel):
"""
Template class for email templates 2
"""
data_model: TemplateDataType