8000 Improve equivalence between type and Type. · python/mypy@2c20f0a · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 2c20f0a

Browse files
author
Guido van Rossum
committed
Improve equivalence between type and Type.
1 parent 753dcf2 commit 2c20f0a

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

mypy/subtypes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ def visit_type_type(self, left: TypeType) -> bool:
224224
if isinstance(right, CallableType):
225225
# This is unsound, we don't check the __init__ signature.
226226
return right.is_type_obj() and is_subtype(left.item, right.ret_type)
227+
if isinstance(right, Instance) and right.type.fullname() == 'builtins.type':
228+
# Treat builtins.type the same as Type[Any].
229+
return True
227230
# XXX Others? Union, Any, TypeVar
228231
return False
229232

mypy/test/data/check-classes.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,3 +1517,13 @@ class X: pass
15171517
foo(X)
15181518
[out]
15191519
main: note: In function "foo":
1520+
1521+
[case testTypeUsingTypeCBuiltinType]
1522+
from typing import Type
1523+
def foo(arg: type): pass
1524+
class X: pass
1525+
def bar(arg: Type[X]):
1526+
foo(arg)
1527+
foo(X)
1528+
[builtins fixtures/tuple.py]
1529+
[out]

mypy/test/data/fixtures/tuple.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
class object:
88
def __init__(self): pass
99

10-
class type: pass
10+
class type:
11+
def __init__(self, *a) -> None: pass
12+
def __call__(self, *a) -> object: pass
1113
class tuple(Sequence[Tco], Generic[Tco]):
1214
def __getitem__(self, x: int) -> Tco: pass
1315
class function: pass

0 commit comments

Comments
 (0)
0