Skip to content

Commit 960e5e2

Browse files
authored
Fix assignment of 'builtins.type' inferred as 'builtins.object' (#9481)
Fixes #9476
1 parent 765acca commit 960e5e2

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

mypy/typeanal.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,13 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Opt
307307
elif (fullname == 'typing.Type' or
308308
(fullname == 'builtins.type' and self.api.is_future_flag_set('annotations'))):
309309
if len(t.args) == 0:
310-
any_type = self.get_omitted_any(t)
311-
return TypeType(any_type, line=t.line, column=t.column)
310+
if fullname == 'typing.Type':
311+
any_type = self.get_omitted_any(t)
312+
return TypeType(any_type, line=t.line, column=t.column)
313+
else:
314+
# To prevent assignment of 'builtins.type' inferred as 'builtins.object'
315+
# See https://github.com/python/mypy/issues/9476 for more information
316+
return None
312317
type_str = 'Type[...]' if fullname == 'typing.Type' else 'type[...]'
313318
if len(t.args) != 1:
314319
self.fail(type_str + ' must have exactly one type argument', t)

test-data/unit/check-callable.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,14 @@ else:
473473
reveal_type(fn) # N: Revealed type is 'None'
474474

475475
[builtins fixtures/callable.pyi]
476+
477+
[case testBuiltinsTypeAsCallable]
478+
# flags: --python-version 3.7
479+
from __future__ import annotations
480+
481+
reveal_type(type) # N: Revealed type is 'def (x: Any) -> builtins.type'
482+
_TYPE = type
483+
reveal_type(_TYPE) # N: Revealed type is 'def (x: Any) -> builtins.type'
484+
_TYPE('bar')
485+
486+
[builtins fixtures/callable.pyi]

0 commit comments

Comments
 (0)