Skip to content

Commit 73d7fb1

Browse files
Allow instantiation of type[None] (#19782)
Fixes #19660 --------- Co-authored-by: Shantanu Jain <hauntsaninja@gmail.com>
1 parent 3c3d44d commit 73d7fb1

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎mypy/checkexpr.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,17 @@ def analyze_type_type_callee(self, item: ProperType, context: Context) -> Type:
20092009
return self.analyze_type_type_callee(tuple_fallback(item), context)
20102010
if isinstance(item, TypedDictType):
20112011
return self.typeddict_callable_from_context(item)
2012+
if isinstance(item, NoneType):
2013+
# NoneType() returns None, so treat it as a callable that returns None
2014+
return CallableType(
2015+
arg_types=[],
2016+
arg_kinds=[],
2017+
arg_names=[],
2018+
ret_type=NoneType(),
2019+
fallback=self.named_type("builtins.function"),
2020+
name=None,
2021+
from_type_type=True,
2022+
)
20122023

20132024
self.msg.unsupported_type_type(item, context)
20142025
return AnyType(TypeOfAny.from_error)

‎test-data/unit/check-classes.test‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3957,6 +3957,23 @@ def f(a: Type[Tuple[int, int]]):
39573957
a() # E: Cannot instantiate type "type[tuple[int, int]]"
39583958
[builtins fixtures/tuple.pyi]
39593959

3960+
[case testTypeUsingTypeCNoneType]
3961+
from typing_extensions import assert_type
3962+
3963+
NoneType = type(None)
3964+
assert_type(type(None)(), None)
3965+
assert_type(NoneType(), None)
3966+
3967+
def f(n: type[None]):
3968+
assert_type(n(), None)
3969+
n(1) # E: Too many arguments
3970+
def g(n: type[NoneType]):
3971+
assert_type(n(), None)
3972+
3973+
f(NoneType)
3974+
g(NoneType)
3975+
[builtins fixtures/tuple.pyi]
3976+
39603977
[case testTypeUsingTypeCNamedTuple]
39613978
from typing import Type, NamedTuple
39623979
N = NamedTuple('N', [('x', int), ('y', int)])

0 commit comments

Comments
 (0)