I have a parent class A. It has few children: class A1, A2 etc.
I want to create a list which can contain any of children or list with any of children and interpret it as class B. Code:
#!/usr/bin/python3
from typing import Union, List, NewType, Type, Sequence
from dataclasses import dataclass
class A:
pass
class A1(A):
pass
class A2(A):
pass
B = NewType("B", Sequence[Union[Sequence[Type[A]], Type[A]]])
b1 = B([A1] + [A2]*2 + [[A1, A2]])
b2 = B([A1, A2, [A1, A2]])
If I create the list manually, it works fine. But if I create it by concatenating different lists, MyPy linter reports errors:
main.py:17: error: List item 0 has incompatible type "Type[A2]"; expected "Type[A1]"
main.py:17: error: List item 0 has incompatible type "List[Type[A]]"; expected "Type[A1]"
Is there a way to avoid the error without suppressing MyPy linter?