Рассмотрим следующий надуманный минимальный пример, в котором линтер Pyright выдает следующую ошибку проверки типа аргумента:
Аргумент типа list[ChildItem] не может быть назначен элементам параметра типа list[ParentItem] в функции __init__
Код: Выделить всё
list[ChildItem]Код: Выделить всё
class ParentItem:
def __init__(self) -> None:
pass
class ChildItem(ParentItem):
def __init__(self) -> None:
super().__init__()
class ParentGroup:
def __init__(self, items: list[ParentItem]) -> None:
self._items = items
def add(self, item: ParentItem) -> None:
self._items.append(item)
class ChildGroup(ParentGroup):
def __init__(self, child_items: list[ChildItem]) -> None:
super().__init__(child_items) # pyright argument type checking error here
Код: Выделить всё
def __init__(self, child_items: list[ChildItem]) -> None:
Код: Выделить всё
def __init__(self, child_items: list[ParentItem]) -> None:
Код: Выделить всё
i1 = ChildItem()
i2 = ChildItem()
ilist = [i1, i2]
g_with_child_items = ChildGroup(ilist) # same error as before, but regarding assignment to "child_items" parameter instead of "items" parameter
Подробнее здесь: https://stackoverflow.com/questions/788 ... arents-typ