Repository: python/mypy

Issue:
Unsupported type Type["None"]
This message is triggered by the following minimal example:
```python
from typing import Any, Type, Union

Primitive = Union[Type[str], Type[int], Type[bool], Type[float], Type[None]]  # Unsupported type Type["None"]


def primitive(data: Any, cls: Primitive):
    if cls is type(None):
        return None
    return cls(data) # The line causing the error (if I remove it, the error disappear)
```
I understand that Mypy warns that Type[None] cannot be instantiated. And, the error cannot be silenced with a `# type: ignore` on the concerned line (`return cls(data)`).
In this example, I can silence it by putting `# type: ignore` on `Primitive` declaration line. However, in my real case, the Primitive type is in a dedicated module, and the issue is even more weird, because the error is then signaled at line *1:1*. And I'm not able to silence it anymore.

Tested with Mypy 0.770 in Python 3.8.


Discussion:
Bugs with wrong line numbers are super nasty!

Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -903,8 +903,7 @@ def check_call(self,
             return self.check_call(callee.upper_bound, args, arg_kinds, context, arg_names,
                                    callable_node, arg_messages)
         elif isinstance(callee, TypeType):
-            # Pass the original Type[] as context since that's where errors should go.
-            item = self.analyze_type_type_callee(callee.item, callee)
+            item = self.analyze_type_type_callee(callee.item, context)
             return self.check_call(item, args, arg_kinds, context, arg_names,
                                    callable_node, arg_messages)
         elif isinstance(callee, TupleType):
diff --git a/mypy/messages.py b/mypy/messages.py
--- a/mypy/messages.py
+++ b/mypy/messages.py
@@ -1050,7 +1050,7 @@ def reveal_locals(self, type_map: Dict[str, Optional[Type]], context: Context) -
             self.note(line, context)
 
     def unsupported_type_type(self, item: Type, context: Context) -> None:
-        self.fail('Unsupported type Type[{}]'.format(format_type(item)), context)
+        self.fail('Cannot instantiate type "Type[{}]"'.format(format_type_bare(item)), context)
 
     def redundant_cast(self, typ: Type, context: Context) -> None:
         self.fail('Redundant cast to {}'.format(format_type(typ)), context,


Test patch:
diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test
--- a/test-data/unit/check-classes.test
+++ b/test-data/unit/check-classes.test
@@ -3172,10 +3172,9 @@ def process(cls: Type[U]):
 
 [case testTypeUsingTypeCErrorUnsupportedType]
 from typing import Type, Tuple
-def foo(arg: Type[Tuple[int]]):  # E: Unsupported type Type["Tuple[int]"]
-    arg()
+def foo(arg: Type[Tuple[int]]):
+    arg()  # E: Cannot instantiate type "Type[Tuple[int]]"
 [builtins fixtures/tuple.pyi]
-[out]
 
 [case testTypeUsingTypeCOverloadedClass]
 from foo import *
@@ -3218,10 +3217,8 @@ def f(a: T): pass
 [case testTypeUsingTypeCTuple]
 from typing import Type, Tuple
 def f(a: Type[Tuple[int, int]]):
-    a()
+    a()  # E: Cannot instantiate type "Type[Tuple[int, int]]"
 [builtins fixtures/tuple.pyi]
-[out]
-main:2: error: Unsupported type Type["Tuple[int, int]"]
 
 [case testTypeUsingTypeCNamedTuple]
 from typing import Type, NamedTuple