Skip to content

Commit dfc1fda

Browse files
authored
Treat NotImplemented as a singleton type (#20601)
1 parent b6b929b commit dfc1fda

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

mypy/typeops.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from mypy.state import state
3535
from mypy.types import (
3636
ELLIPSIS_TYPE_NAMES,
37+
NOT_IMPLEMENTED_TYPE_NAMES,
3738
AnyType,
3839
CallableType,
3940
ExtraAttrs,
@@ -996,8 +997,10 @@ def is_singleton_identity_type(typ: ProperType) -> bool:
996997
if isinstance(typ, NoneType):
997998
return True
998999
if isinstance(typ, Instance):
999-
return (typ.type.is_enum and len(typ.type.enum_members) == 1) or (
1000-
typ.type.fullname in ELLIPSIS_TYPE_NAMES
1000+
return (
1001+
(typ.type.is_enum and len(typ.type.enum_members) == 1)
1002+
or (typ.type.fullname in ELLIPSIS_TYPE_NAMES)
1003+
or (typ.type.fullname in NOT_IMPLEMENTED_TYPE_NAMES)
10011004
)
10021005
if isinstance(typ, LiteralType):
10031006
return typ.is_enum_literal() or isinstance(typ.value, bool)

test-data/unit/check-narrowing.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,7 +2714,14 @@ reveal_type(t.foo) # N: Revealed type is "__main__.C"
27142714
[builtins fixtures/property.pyi]
27152715

27162716
[case testNarrowingNotImplemented]
2717+
# flags: --python-version 3.10
27172718
from __future__ import annotations
2719+
import types
2720+
2721+
def foo(x: types.NotImplementedType | str):
2722+
if x is not NotImplemented:
2723+
reveal_type(x) # N: Revealed type is "builtins.str"
2724+
27182725
from typing_extensions import Self
27192726

27202727
class X:

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ if sys.version_info >= (3, 10):
1919

2020
class UnionType:
2121
def __or__(self, x) -> UnionType: ...
22+
23+
class NotImplementedType: ...

0 commit comments

Comments
 (0)