3 ms·
Just ran into an interesting issue with Literal >>> l = Literal["foo", "bar", "baz"] >>> "foo" in l TypeError: typing.Literal['foo', 'bar', 'baz']
by BerislavLopac 4y ago
Just ran into an interesting issue with Literal
>>> l = Literal["foo", "bar", "baz"]
>>> "foo" in l
TypeError: typing.Literal['foo', 'bar', 'baz'] is not a generic class
>>>
>>> isinstance("foo", l)
TypeError: Subscripted generics cannot be used with class and instance checks
So Literal is a Schroedinger's generic.
- geekraver 4y agoType classes in Python are meant as annotations; they are not for run-time. They essentially get erased (not exactly, as they are available for introspection, but they aren't your typical code).
- BerislavLopac 4y agoNo, I understand that (although things like @runtime_checkable and NamedTuple muddle things a bit). My point is that, based on the error messages, Literal both is and isn't a generic type.