Before I upgraded to python 3.6 from python 3.5 this worked:
import typing
issubclass(list, typing.List[int]) # returns True
isinstance([1, 2 ,3], typing.List[int]) # returns True
now in python 3.6 both of these raise the following exception:
TypeError: Parameterized generics cannot be used with class or instance checks
Is this new intended behavior or a bug? If it is intended how can I perform the checks the code above is doing in python 3.6?
It is intentional, you shouldn't be mixing classes with types as defined in typing, at least, that's the gist of it from what I've understood. A great deal of discussion for this is contained in the issue #136 Kill __subclasscheck__ which also introduced this change. The commit message also references how the isinstance/subclass checks will raise TypeErrors:
Using
isinstance()orissubclass()raisesTypeErrorfor almost everything. There are exceptions: [...]
You can compare without specifying the contained types for the generic types, i.e:
isinstance(list, typing.List[int])
but that's the best you can do afaik.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With