-
I am trying to define a Generic base class, but see different type checking behavior when I inherit from abc.ABC versus Protocol. I am trying to figure out if it is really a difference of variance or if it is just an implementation detail of the type checkers. I've tried against pyright and mypy. Comparing
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Full TypeVar variance consistency is checked only for protocols (as indicated in the typing spec), not for nominal class definitions (including ABCs). Both pyright and mypy do minimal checks for improper usage of covariant and contravariant TypeVars within method parameters and return types, but this isn't the same as full variance consistency checks. The new TypeVar syntax introduced in PEP 695 eliminates the need to worry about variance consistency because variance is calculated by the type checker based on usage. Pyright has had support for PEP 695 for a long time already, and the next release of mypy will also include support for it. Code sample in pyright playground import abc
from typing import Protocol, TypedDict
class BaseResult(TypedDict):
...
class BadProtocolResultWriter[R: BaseResult](Protocol):
@abc.abstractmethod
def write(self, result: R) -> None:
...
class ABCResultWriter[R: BaseResult](abc.ABC):
@abc.abstractmethod
def write(self, result: R) -> None:
...
class ContraProtocolResultWriter[R: BaseResult](Protocol):
@abc.abstractmethod
def write(self, result: R) -> None:
...
class InvariantProtocolResultWriter[R: BaseResult](Protocol):
@abc.abstractmethod
def write(self, result: R) -> R:
... If you're using pyright or pylance, you can hover over the TypeVar declaration to see the computed variance. |
Beta Was this translation helpful? Give feedback.
Full TypeVar variance consistency is checked only for protocols (as indicated in the typing spec), not for nominal class definitions (including ABCs). Both pyright and mypy do minimal checks for improper usage of covariant and contravariant TypeVars within method parameters and return types, but this isn't the same as full variance consistency checks.
The new TypeVar syntax introduced in PEP 695 eliminates the need to worry about variance consistency because variance is calculated by the type checker based on usage. Pyright has had support for PEP 695 for a long time already, and the next release of mypy will also include support f…