Skip to content

Commit 3e8a55f

Browse files
elazarggvanrossum
authored andcommitted
Strip type objects when constructing TypeType (#2832)
Fix #2826
1 parent 7f3f67c commit 3e8a55f

File tree

7 files changed

+18
-6
lines changed

7 files changed

+18
-6
lines changed

mypy/test/testtypegen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def __init__(self) -> None:
8888
self.is_typing = False
8989

9090
def visit_mypy_file(self, f: MypyFile) -> None:
91-
self.is_typing = f.fullname() == 'typing'
91+
self.is_typing = f.fullname() == 'typing' or f.fullname() == 'builtins'
9292
super().visit_mypy_file(f)
9393

9494
def visit_assignment_stmt(self, s: AssignmentStmt) -> None:

mypy/typeanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def visit_unbound_type(self, t: UnboundType) -> Type:
129129
return TypeVarType(sym.tvar_def, t.line)
130130
elif fullname == 'builtins.None':
131131
return NoneTyp()
132-
elif fullname == 'typing.Any':
132+
elif fullname == 'typing.Any' or fullname == 'builtins.Any':
133133
return AnyType()
134134
elif fullname == 'typing.Tuple':
135135
if len(t.args) == 0 and not t.empty_tuple_index:

mypy/types.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ class Instance(Type):
384384

385385
def __init__(self, typ: Optional[mypy.nodes.TypeInfo], args: List[Type],
386386
line: int = -1, column: int = -1, erased: bool = False) -> None:
387+
assert(typ is None or typ.fullname() not in ["builtins.Any", "typing.Any"])
387388
self.type = typ
388389
self.args = args
389390
self.erased = erased
@@ -1146,7 +1147,10 @@ class TypeType(Type):
11461147

11471148
def __init__(self, item: Type, *, line: int = -1, column: int = -1) -> None:
11481149
super().__init__(line, column)
1149-
self.item = item
1150+
if isinstance(item, CallableType) and item.is_type_obj():
1151+
self.item = item.fallback
1152+
else:
1153+
self.item = item
11501154

11511155
def accept(self, visitor: 'TypeVisitor[T]') -> T:
11521156
return visitor.visit_type_type(self)

test-data/unit/check-classes.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3111,3 +3111,11 @@ reveal_type(A.x) # E: Revealed type is 'Any'
31113111
A.f(1) # E: Too many arguments for "f" of "A"
31123112
A().g(1) # E: Too many arguments for "g" of "A"
31133113
[builtins fixtures/classmethod.pyi]
3114+
3115+
[case testMetaclassTypeCallable]
3116+
class M(type):
3117+
x = 5
3118+
3119+
class A(metaclass=M): pass
3120+
reveal_type(type(A).x) # E: Revealed type is 'builtins.int'
3121+

test-data/unit/lib-stub/__builtin__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Any: pass
1+
Any = 0
22

33
class object:
44
def __init__(self):

test-data/unit/lib-stub/builtins.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class Any: pass
1+
Any = 0
22

33
class object:
44
def __init__(self) -> None: pass

test-data/unit/semanal-types.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ MypyFile:1(
736736
f
737737
Args(
738738
Var(a))
739-
def (a: builtins.Any) -> builtins.Any
739+
def (a: Any) -> Any
740740
Block:7(
741741
ReturnStmt:7(
742742
NameExpr(a [l]))))

0 commit comments

Comments
 (0)