Skip to content
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
10 changes: 2 additions & 8 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,9 +1260,7 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
if isinstance(typ.ret_type, TypeVarType):
if typ.ret_type.variance == CONTRAVARIANT:
self.fail(
message_registry.RETURN_TYPE_CANNOT_BE_CONTRAVARIANT,
typ.ret_type,
code=codes.UNSAFE_VARIANCE,
message_registry.RETURN_TYPE_CANNOT_BE_CONTRAVARIANT, typ.ret_type
)
self.check_unbound_return_typevar(typ)

Expand Down Expand Up @@ -1369,11 +1367,7 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
ctx: Context = arg_type
if ctx.line < 0:
ctx = typ
self.fail(
message_registry.FUNCTION_PARAMETER_CANNOT_BE_COVARIANT,
ctx,
code=codes.UNSAFE_VARIANCE,
)
self.fail(message_registry.FUNCTION_PARAMETER_CANNOT_BE_COVARIANT, ctx)
if typ.arg_kinds[i] == nodes.ARG_STAR:
if not isinstance(arg_type, ParamSpecType):
# builtins.tuple[T] is typing.Tuple[T, ...]
Expand Down
6 changes: 4 additions & 2 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ def __str__(self) -> str:
"General",
default_enabled=False,
)
UNSAFE_VARIANCE: Final = ErrorCode("unsafe-variance", "Incorrect usages of variance", "General")
UNUSED_IGNORE: Final = ErrorCode("unused-ignore", "Ignore comment is unused", "General")
UNSAFE_VARIANCE: Final[ErrorCode] = ErrorCode(
"unsafe-variance", "Incorrect usages of variance", "General"
)
UNUSED_IGNORE: Final[ErrorCode] = ErrorCode("unused-ignore", "Ignore comment is unused", "General")

NO_ANY_EXPR: Final = ErrorCode("no-any-expr", "An expression contains Any", "General")
NO_ANY_EXPLICIT: Final = ErrorCode("no-any-explicit", "Usage of the Any type", "General")
Expand Down
6 changes: 4 additions & 2 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ def format(self, *args: object, **kwargs: object) -> "ErrorMessage":
RETURN_TYPE_CANNOT_BE_CONTRAVARIANT: Final = ErrorMessage(
"This usage of this contravariant type variable is unsafe as a return type.\n"
"If this is intentional and you know what you are doing, "
"you can ignore this line with 'unsafe-variance'"
"you can ignore this line with 'unsafe-variance'",
codes.UNSAFE_VARIANCE,
)
FUNCTION_PARAMETER_CANNOT_BE_COVARIANT: Final = ErrorMessage(
"This usage of this covariant type variable is unsafe as an input parameter.\n"
"If this is intentional and you know what you are doing, "
"you can ignore this line with 'unsafe-variance'"
"you can ignore this line with 'unsafe-variance'",
codes.UNSAFE_VARIANCE,
)
INCOMPATIBLE_IMPORT_OF: Final = "Incompatible import of"
FUNCTION_TYPE_EXPECTED: Final = ErrorMessage(
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-based-unsafe-variance.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[case testUnsafeVarianceCo]
from helper import T_out
from typing import Generic
class G(Generic[T_out]):
def f(self, t: T_out): ... # E: This usage of this covariant type variable is unsafe as an input parameter.$NL\
If this is intentional and you know what you are doing, you can ignore this \
line with 'unsafe-variance' [unsafe-variance]


[case testUnsafeVarianceContra]
from helper import T_in
from typing import Generic
class G(Generic[T_in]):
def f(self) -> T_in: ... # E: This usage of this contravariant type variable is unsafe as a return type.$NL\
If this is intentional and you know what you are doing, you can ignore this line \
with 'unsafe-variance' [unsafe-variance]
2 changes: 2 additions & 0 deletions test-data/unit/lib-stub/helper.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import TypeVar

T = TypeVar("T")
T_out = TypeVar("T_out", covariant=True)
T_in = TypeVar("T_in", contravariant=True)