How To Check Whether A Variable Is An Instance Of A Subscripted Generics Defined In Typing?
i.e. How to make this work? python doesn't allow this: from typing import List isinstance(['a', 'b'], List[str]) # TypeError: Subscripted generics cannot be used with class and ins
Solution 1:
Thanks to @user8408080, I find typeguard, but that is not exactly what I want. then I use typeguard to search through stack overflow, and find two more similar library from here:
Finally, I got what I want as follows
import typesentry
from typing importList
string_list = ['nobody', 'expects', 'the', 'spanish', 'inqusition']
string_list_class = List[str]
tc1 = typesentry.Config()
is_typed = tc1.is_type # equivalent of isinstance()
is_typed(string_list,string_list_class)
Post a Comment for "How To Check Whether A Variable Is An Instance Of A Subscripted Generics Defined In Typing?"