FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: Allow instantiation of type[None] in analyze_type_type_callee by codenamenam · Pull Request #19782 · python/mypy · GitHub

Repository navigation

Merged
11 changes: 11 additions & 0 deletions mypy/checkexpr.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,17 @@ def analyze_type_type_callee(self, item: ProperType, context: Context) -> Type:
return self.analyze_type_type_callee(tuple_fallback(item), context)
if isinstance(item, TypedDictType):
return self.typeddict_callable_from_context(item)
if isinstance(item, NoneType):
# NoneType() returns None, so treat it as a callable that returns None
return CallableType(
arg_types=[],
arg_kinds=[],
arg_names=[],
ret_type=NoneType(),
fallback=self.named_type("builtins.function"),
name=None,
from_type_type=True,
)

self.msg.unsupported_type_type(item, context)
return AnyType(TypeOfAny.from_error)
Expand Down
17 changes: 17 additions & 0 deletions test-data/unit/check-classes.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3866,6 +3866,23 @@ def f(a: Type[Tuple[int, int]]):
a() # E: Cannot instantiate type "type[tuple[int, int]]"
[builtins fixtures/tuple.pyi]

[case testTypeUsingTypeCNoneType]
from typing_extensions import assert_type

NoneType = type(None)
assert_type(type(None)(), None)
assert_type(NoneType(), None)

def f(n: type[None]):
assert_type(n(), None)
n(1) # E: Too many arguments
def g(n: type[NoneType]):
assert_type(n(), None)

f(NoneType)
g(NoneType)
[builtins fixtures/tuple.pyi]

[case testTypeUsingTypeCNamedTuple]
from typing import Type, NamedTuple
N = NamedTuple('N', [('x', int), ('y', int)])
Expand Down
Loading

Back | FazBrowse Home | New Git URL