-
I have a
If I try and create a subclass which should have a specific string (or set of strings) I get an error:
But to me |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
If you want to override |
Beta Was this translation helpful? Give feedback.
Literal['KEY']
is a subtype ofstr
, but they are not equivalent types. Thea
field is mutable, so subclasses ofMyBase
cannot safely override its type with a subtype of the base attribute's type. This can lead to errors ifMySpecific
is upcast toMyBase
and then someone writes astr
value toa
that is not compatible withLiteral['KEY']
.If you want to override
a
with a subtype in a type-safe manner, the attribute must be immutable in the base class. For example, if you were to define the model as frozen, its attributes would be considered immutable.