Skip to content

Fix unsound variance #13714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2072,6 +2072,24 @@ def visit_class_def(self, defn: ClassDef) -> None:
self.allow_abstract_call = old_allow_abstract_call
# TODO: Apply the sig to the actual TypeInfo so we can handle decorators
# that completely swap out the type. (e.g. Callable[[Type[A]], Type[B]])
if typ.defn.type_vars:
for base_inst in typ.bases:
for base_tvar, base_decl_tvar in zip(
base_inst.args, base_inst.type.defn.type_vars
):
if (
isinstance(base_tvar, TypeVarType)
and base_tvar.variance != INVARIANT
and isinstance(base_decl_tvar, TypeVarType)
and base_decl_tvar.variance != base_tvar.variance
):
self.fail(
f'Variance of TypeVar "{base_tvar.name}" incompatible '
"with variance in parent type",
context=defn,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use existing type-var error code?

code=codes.TYPE_VAR,
)

if typ.is_protocol and typ.defn.type_vars:
self.check_protocol_variance(defn)
if not defn.has_incompatible_baseclass and defn.info.is_enum:
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeshed/stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class SupportsKeysAndGetItem(Protocol[_KT, _VT_co]):
def __getitem__(self, __k: _KT) -> _VT_co: ...

# stable
class SupportsGetItem(Container[_KT_contra], Protocol[_KT_contra, _VT_co]):
class SupportsGetItem(Container[_KT_contra], Protocol[_KT_contra, _VT_co]): # type: ignore
def __getitem__(self, __k: _KT_contra) -> _VT_co: ...

# stable
Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-generic-subtyping.test
Original file line number Diff line number Diff line change
Expand Up @@ -1033,3 +1033,21 @@ x2: X2[str, int]
reveal_type(iter(x2)) # N: Revealed type is "typing.Iterator[builtins.int]"
reveal_type([*x2]) # N: Revealed type is "builtins.list[builtins.int]"
[builtins fixtures/dict.pyi]

[case testIncompatibleVariance]
from typing import TypeVar, Generic
T = TypeVar('T')
T_co = TypeVar('T_co', covariant=True)
T_contra = TypeVar('T_contra', contravariant=True)

class A(Generic[T_co]): ...
class B(A[T_contra], Generic[T_contra]): ... # E: Variance of TypeVar "T_contra" incompatible with variance in parent type

class C(Generic[T_contra]): ...
class D(C[T_co], Generic[T_co]): ... # E: Variance of TypeVar "T_co" incompatible with variance in parent type

class E(Generic[T]): ...
class F(E[T_co], Generic[T_co]): ... # E: Variance of TypeVar "T_co" incompatible with variance in parent type

class G(Generic[T]): ...
class H(G[T_contra], Generic[T_contra]): ... # E: Variance of TypeVar "T_contra" incompatible with variance in parent type
4 changes: 2 additions & 2 deletions test-data/unit/fixtures/typing-full.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ class SupportsAbs(Protocol[T_co]):
def runtime_checkable(cls: T) -> T:
return cls

class ContextManager(Generic[T]):
def __enter__(self) -> T: pass
class ContextManager(Generic[T_co]):
def __enter__(self) -> T_co: pass
# Use Any because not all the precise types are in the fixtures.
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> Any: pass

Expand Down