from typing import TypeVar, List

class Mammal:
    pass

class Cat(Mammal):
    pass

T = TypeVar('T')

def count_mammals(seq : List[Mammal]) -> int:
    return len(seq)

lst = [1, 2, 3]
mlst = [Mammal(), Mammal()]
clst = [Cat(), Cat()]

print(count_mammals(clst))

from typing import List, TypeVar, Iterable, Generic

class Mammal:
    pass

class Cat(Mammal):
    pass

def count_mammals_list(seq : List[Mammal]) -> int:
    return len(seq)

mlst = [Mammal(), Mammal()]
print(count_mammals_list(mlst))

# This will fail a mypy check, because List is not covariant.
clst = [Cat(), Cat()]
print(count_mammals_list(clst))

# Now we define a simplistic immutable list wrapper with a covariant type
# parameter. This will pass type checking
T_co = TypeVar('T_co', covariant=True)

class ImmutableList(Generic[T_co]):
    def __init__(self, items: Iterable[T_co]) -> None:
        self.lst = list(items)

    def __len__(self) -> int:
        return len(self.lst)

def count_mammals_ilist(seq : ImmutableList[Mammal]) -> int:
    return len(seq)

mimmlst = ImmutableList([Mammal(), Mammal()])
print(count_mammals_ilist(mimmlst))

cimmlst = ImmutableList([Cat(), Cat()])
print(count_mammals_ilist(cimmlst))

Update the sample with covariant markings
Update the sample with covariant markings

- from typing import TypeVar, List
+ from typing import List, TypeVar, Iterable, Generic
  
  class Mammal:
      pass
  
  class Cat(Mammal):
      pass
  
- T = TypeVar('T')
- 
- def count_mammals(seq : List[Mammal]) -> int:
+ def count_mammals_list(seq : List[Mammal]) -> int:
      return len(seq)
  
- lst = [1, 2, 3]
  mlst = [Mammal(), Mammal()]
+ print(count_mammals_list(mlst))
+ 
+ # This will fail a mypy check, because List is not covariant.
  clst = [Cat(), Cat()]
+ print(count_mammals_list(clst))
  
- print(count_mammals(clst))
+ # Now we define a simplistic immutable list wrapper with a covariant type
+ # parameter. This will pass type checking
+ T_co = TypeVar('T_co', covariant=True)
  
+ class ImmutableList(Generic[T_co]):
+     def __init__(self, items: Iterable[T_co]) -> None:
+         self.lst = list(items)
+ 
+     def __len__(self) -> int:
+         return len(self.lst)
+ 
+ def count_mammals_ilist(seq : ImmutableList[Mammal]) -> int:
+     return len(seq)
+ 
+ mimmlst = ImmutableList([Mammal(), Mammal()])
+ print(count_mammals_ilist(mimmlst))
+ 
+ cimmlst = ImmutableList([Cat(), Cat()])
+ print(count_mammals_ilist(cimmlst))
+ 
Update the sample with covariant markings
## Code Before:
from typing import TypeVar, List

class Mammal:
    pass

class Cat(Mammal):
    pass

T = TypeVar('T')

def count_mammals(seq : List[Mammal]) -> int:
    return len(seq)

lst = [1, 2, 3]
mlst = [Mammal(), Mammal()]
clst = [Cat(), Cat()]

print(count_mammals(clst))

## Instruction:
Update the sample with covariant markings
## Code After:
from typing import List, TypeVar, Iterable, Generic

class Mammal:
    pass

class Cat(Mammal):
    pass

def count_mammals_list(seq : List[Mammal]) -> int:
    return len(seq)

mlst = [Mammal(), Mammal()]
print(count_mammals_list(mlst))

# This will fail a mypy check, because List is not covariant.
clst = [Cat(), Cat()]
print(count_mammals_list(clst))

# Now we define a simplistic immutable list wrapper with a covariant type
# parameter. This will pass type checking
T_co = TypeVar('T_co', covariant=True)

class ImmutableList(Generic[T_co]):
    def __init__(self, items: Iterable[T_co]) -> None:
        self.lst = list(items)

    def __len__(self) -> int:
        return len(self.lst)

def count_mammals_ilist(seq : ImmutableList[Mammal]) -> int:
    return len(seq)

mimmlst = ImmutableList([Mammal(), Mammal()])
print(count_mammals_ilist(mimmlst))

cimmlst = ImmutableList([Cat(), Cat()])
print(count_mammals_ilist(cimmlst))
