Repository: python/mypy

Issue:
Covariance error inferred for lambda has no associated line number
```
from typing import TypeVar, Generic, Callable

_Tco = TypeVar('_Tco', covariant=True)

class Thing(Generic[_Tco]):

    def chain(self, func):
        # type: (Callable[[_Tco], None]) -> None
        pass

    def end(self):
        # type: () -> None

        return self.chain(lambda _: None)
```

`example.py: error: Cannot use a covariant type variable as a parameter`

This error has no associated line number. (If the lambda is expanded into an explicit inner function typed as `(_Tco) -> None`, the line number is present.)


Discussion:

Resolving patch:
diff --git a/mypy/checker.py b/mypy/checker.py
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -588,7 +588,7 @@ def check_func_item(self, defn: FuncItem,
             with self.enter_partial_types():
                 typ = self.function_type(defn)
                 if type_override:
-                    typ = type_override
+                    typ = type_override.copy_modified(line=typ.line, column=typ.column)
                 if isinstance(typ, CallableType):
                     with self.enter_attribute_inference_context():
                         self.check_func_def(defn, typ, name)
@@ -725,7 +725,10 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
                             arg_type.variance == COVARIANT and
                             defn.name() not in ('__init__', '__new__')
                         ):
-                            self.fail(messages.FUNCTION_PARAMETER_CANNOT_BE_COVARIANT, arg_type)
+                            ctx = arg_type  # type: Context
+                            if ctx.line < 0:
+                                ctx = typ
+                            self.fail(messages.FUNCTION_PARAMETER_CANNOT_BE_COVARIANT, ctx)
                     if typ.arg_kinds[i] == nodes.ARG_STAR:
                         # builtins.tuple[T] is typing.Tuple[T, ...]
                         arg_type = self.named_generic_type('builtins.tuple',
diff --git a/mypy/types.py b/mypy/types.py
--- a/mypy/types.py
+++ b/mypy/types.py
@@ -1973,6 +1973,8 @@ def callable_type(fdef: mypy.nodes.FuncItem, fallback: Instance,
         ret_type or AnyType(TypeOfAny.unannotated),
         fallback,
         name=fdef.name(),
+        line=fdef.line,
+        column=fdef.column,
         implicit=True,
     )
 


Test patch:
diff --git a/test-data/unit/check-functions.test b/test-data/unit/check-functions.test
--- a/test-data/unit/check-functions.test
+++ b/test-data/unit/check-functions.test
@@ -1879,6 +1879,34 @@ class A(Generic[t]):
 [out]
 main:5: error: Cannot use a covariant type variable as a parameter
 
+[case testRejectCovariantArgumentSplitLine]
+from typing import TypeVar, Generic
+
+t = TypeVar('t', covariant=True)
+class A(Generic[t]):
+    def foo(self,
+            x: t) -> None:
+        return None
+[builtins fixtures/bool.pyi]
+[out]
+main:6: error: Cannot use a covariant type variable as a parameter
+
+[case testRejectCovariantArgumentInLambda]
+from typing import TypeVar, Generic, Callable
+
+t = TypeVar('t', covariant=True)
+class Thing(Generic[t]):
+    def chain(self, func: Callable[[t], None]) -> None: pass
+    def end(self) -> None:
+        return self.chain(  # Note that lambda args have no line numbers
+            lambda _: None)
+[builtins fixtures/bool.pyi]
+[out]
+main:8: error: Cannot use a covariant type variable as a parameter
+
+[case testRejectCovariantArgumentInLambdaSplitLine]
+from typing import TypeVar, Generic, Callable
+
 [case testRejectContravariantReturnType]
 from typing import TypeVar, Generic