Skip to content

Commit 6377f4f

Browse files
committed
fix: Allow instantiation of type[None] in analyze_type_type_callee
1 parent b8ee1f5 commit 6377f4f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

mypy/checkexpr.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,17 @@ def analyze_type_type_callee(self, item: ProperType, context: Context) -> Type:
19261926
return self.analyze_type_type_callee(tuple_fallback(item), context)
19271927
if isinstance(item, TypedDictType):
19281928
return self.typeddict_callable_from_context(item)
1929+
if isinstance(item, NoneType):
1930+
# NoneType() returns None, so treat it as a callable that returns None
1931+
return CallableType(
1932+
arg_types=[],
1933+
arg_kinds=[],
1934+
arg_names=[],
1935+
ret_type=NoneType(),
1936+
fallback=self.named_type("builtins.function"),
1937+
name=None,
1938+
from_type_type=True
1939+
)
19291940

19301941
self.msg.unsupported_type_type(item, context)
19311942
return AnyType(TypeOfAny.from_error)

test-data/unit/check-classes.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3866,6 +3866,18 @@ def f(a: Type[Tuple[int, int]]):
38663866
a() # E: Cannot instantiate type "type[tuple[int, int]]"
38673867
[builtins fixtures/tuple.pyi]
38683868

3869+
[case testTypeUsingTypeCNoneType]
3870+
NoneType = type(None)
3871+
type(None)()
3872+
NoneType()
3873+
def f(n: type[None]):
3874+
n()
3875+
def g(n: type[NoneType]):
3876+
n()
3877+
f(NoneType)
3878+
g(NoneType)
3879+
[out]
3880+
38693881
[case testTypeUsingTypeCNamedTuple]
38703882
from typing import Type, NamedTuple
38713883
N = NamedTuple('N', [('x', int), ('y', int)])

0 commit comments

Comments
 (0)