Repository: python/mypy

Issue:
Type[Any] and type are not equivalent
From `typing` docs:

> `Type[Any]` is equivalent to `Type` which in turn is equivalent to `type`, which is the root of Python’s metaclass hierarchy.

But this code produces error:
```python
from typing import *
y: type
x: Type[Any] = y
```

> error: Incompatible types in assignment (expression has type "`type`", variable has type `Type[Any])`


Discussion:

Resolving patch:
diff --git a/mypy/subtypes.py b/mypy/subtypes.py
--- a/mypy/subtypes.py
+++ b/mypy/subtypes.py
@@ -52,6 +52,11 @@ def is_subtype(left: Type, right: Type,
         return any(is_subtype(left, item, type_parameter_checker,
                               ignore_pos_arg_names=ignore_pos_arg_names)
                    for item in right.items)
+    # Treat builtins.type the same as Type[Any]
+    elif is_named_instance(left, 'builtins.type'):
+            return is_subtype(TypeType(AnyType()), right)
+    elif is_named_instance(right, 'builtins.type'):
+            return is_subtype(left, TypeType(AnyType()))
     else:
         return left.accept(SubtypeVisitor(right, type_parameter_checker,
                                           ignore_pos_arg_names=ignore_pos_arg_names))
@@ -233,8 +238,7 @@ def visit_overloaded(self, left: Overloaded) -> bool:
         right = self.right
         if isinstance(right, Instance):
             return is_subtype(left.fallback, right)
-        elif isinstance(right, CallableType) or is_named_instance(
-                right, 'builtins.type'):
+        elif isinstance(right, CallableType):
             for item in left.items():
                 if is_subtype(item, right, self.check_type_parameter,
                               ignore_pos_arg_names=self.ignore_pos_arg_names):
@@ -275,8 +279,7 @@ def visit_type_type(self, left: TypeType) -> bool:
             # This is unsound, we don't check the __init__ signature.
             return right.is_type_obj() and is_subtype(left.item, right.ret_type)
         if isinstance(right, Instance):
-            if right.type.fullname() in ('builtins.type', 'builtins.object'):
-                # Treat builtins.type the same as Type[Any];
+            if right.type.fullname() == 'builtins.object':
                 # treat builtins.object the same as Any.
                 return True
             item = left.item


Test patch:
diff --git a/mypy/test/testsubtypes.py b/mypy/test/testsubtypes.py
--- a/mypy/test/testsubtypes.py
+++ b/mypy/test/testsubtypes.py
@@ -168,7 +168,7 @@ def test_var_arg_callable_subtyping_9(self) -> None:
             self.fx.callable_var_arg(0, self.fx.b, self.fx.d))
 
     def test_type_callable_subtyping(self) -> None:
-        self.assert_strict_subtype(
+        self.assert_subtype(
             self.fx.callable_type(self.fx.d, self.fx.a), self.fx.type_type)
 
         self.assert_strict_subtype(
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
@@ -2126,6 +2126,43 @@ def foo(c: Type[C], d: Type[D]) -> None:
 [out]
 main:7: error: Revealed type is 'builtins.list[Type[__main__.B]]'
 
+[case testTypeEquivalentTypeAny]
+from typing import Type, Any
+
+a = None # type: Type[Any]
+b = a # type: type
+
+x = None # type: type
+y = x # type: Type[Any]
+
+class C: ...
+
+p = None # type: type
+q = p # type: Type[C]
+
+[builtins fixtures/list.pyi]
+[out]
+
+[case testTypeEquivalentTypeAny2]
+from typing import Type, Any, TypeVar, Generic
+
+class C: ...
+x = None # type: type
+y = None # type: Type[Any]
+z = None # type: Type[C]
+
+lst = [x, y, z]
+reveal_type(lst) # E: Revealed type is 'builtins.list[builtins.type*]'
+
+T1 = TypeVar('T1', bound=type)
+T2 = TypeVar('T2', bound=Type[Any])
+class C1(Generic[T1]): ...
+class C2(Generic[T2]): ...
+
+C1[Type[Any]], C2[type] # both these should not fail
+[builtins fixtures/list.pyi]
+[out]
+
 [case testTypeMatchesOverloadedFunctions]
 from typing import Type, overload, Union