I am trying to do type enforcement in Python 3.8
but unable to enforce type bounds. Let's consider the following example:
class Animal(object): pass
class Cat(Animal): pass
class Dog(Animal): pass
# now want a list of only cats
list_of_cats:Cat = list()
list_of_cats.append(Cat())
list_of_cats.append(Dog()) # this should not be allowed
for animal in list_of_cats:
print(type(animal))
---- OUTPUT -----
<class '__main__.Cat'>
<class '__main__.Dog'>
Is there a way to enforce type bounds, using language features, in Python 3.8
(like Scala)?