I am creating a Python object from its string representation:
obj = ast.literal_eval(obj_text)
Now, I want to make sure that the object has an appropriate type. In this case, I want it to be a list of strings. This can be done, as explained here. Still, I thought that maybe newer (3.10) Python versions will have an easier way to do that, so I tried this instead:
type_ok = (type(obj) == list[str])
For some reason, this is always false. So I tried is
instead:
type_ok = (type(obj) is list[str])
But this also always evaluates to False
. Why is this the case? Isn't list[str]
the type of, say, ["a", "b", "c"]
? Anyway it seems, like I'll have to use a for
loop, if this doesn't work.