https://peps.python.org/pep-0544/#subty ... ther-types
Упрощая код в этом разделе, я написал код
Код: Выделить всё
from typing import Protocol, TypeVar
T = TypeVar("T", contravariant=True)
class ListLike(Protocol[T]):
def append(self, x: T) -> None:
...
class MockStack:
def append(self, x: int) -> None:
...
def populate(lst: ListLike[int]) -> int:
...
populate([1, 2, 3])
populate(MockStack())
Но pylance в VS Code повышает сообщение об ошибке:
Код: Выделить всё
Argument of type "list[int]" cannot be assigned to parameter "lst" of type "ListLike[int]" in function "populate"
"list[int]" is incompatible with protocol "ListLike[int]"
"append" is an incompatible type
Type "(__object: int, /) -> None" cannot be assigned to type "(x: T@ListLike) -> None"
Position-only parameter mismatch; expected 1 but received 0 (reportGeneralTypeIssues)
среда: Apple M1 Pro, mac OS 13.5.1
Версия кода VS: 1.84.2
Код: Выделить всё
pylanceПодробнее здесь: https://stackoverflow.com/questions/774 ... t-in-types
Мобильная версия