From 3f670729172aabbcbe0833bf888dce821ebbbf33 Mon Sep 17 00:00:00 2001 From: KotlinIsland Date: Wed, 20 Jul 2022 15:22:12 +1000 Subject: [PATCH] fixup! unsafe-variance --- mypy/checker.py | 10 ++-------- mypy/errorcodes.py | 6 ++++-- mypy/message_registry.py | 6 ++++-- test-data/unit/check-based-unsafe-variance.test | 16 ++++++++++++++++ test-data/unit/lib-stub/helper.pyi | 2 ++ 5 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 test-data/unit/check-based-unsafe-variance.test diff --git a/mypy/checker.py b/mypy/checker.py index 6b01cb268..1420cea28 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -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) @@ -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, ...] diff --git a/mypy/errorcodes.py b/mypy/errorcodes.py index e402b0b25..e9468aa01 100644 --- a/mypy/errorcodes.py +++ b/mypy/errorcodes.py @@ -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") diff --git a/mypy/message_registry.py b/mypy/message_registry.py index 4c1203411..0d1f6100f 100644 --- a/mypy/message_registry.py +++ b/mypy/message_registry.py @@ -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( diff --git a/test-data/unit/check-based-unsafe-variance.test b/test-data/unit/check-based-unsafe-variance.test new file mode 100644 index 000000000..5efa4a583 --- /dev/null +++ b/test-data/unit/check-based-unsafe-variance.test @@ -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] diff --git a/test-data/unit/lib-stub/helper.pyi b/test-data/unit/lib-stub/helper.pyi index b186d5ad6..2a5e815a5 100644 --- a/test-data/unit/lib-stub/helper.pyi +++ b/test-data/unit/lib-stub/helper.pyi @@ -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)