Skip to content

Commit 7c3b481

Browse files
authored
Handle deprecated boolean cast of NotImplemented (#2831)
1 parent e3f5fb5 commit 7c3b481

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ What's New in astroid 4.0.0?
77
============================
88
Release date: TBA
99

10+
* Handle deprecated `bool(NotImplemented)` cast in const nodes.
11+
1012
* Add support for boolean truthiness constraints (`x`, `not x`) in inference.
1113

1214
Closes pylint-dev/pylint#9515

astroid/nodes/node_classes.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from astroid import decorators, protocols, util
2121
from astroid.bases import Instance, _infer_stmts
22-
from astroid.const import _EMPTY_OBJECT_MARKER, Context
22+
from astroid.const import _EMPTY_OBJECT_MARKER, PY314_PLUS, Context
2323
from astroid.context import CallContext, InferenceContext, copy_context
2424
from astroid.exceptions import (
2525
AstroidBuildingError,
@@ -2166,8 +2166,12 @@ def bool_value(self, context: InferenceContext | None = None):
21662166
"""Determine the boolean value of this node.
21672167
21682168
:returns: The boolean value of this node.
2169-
:rtype: bool
2169+
:rtype: bool or Uninferable
21702170
"""
2171+
# bool(NotImplemented) is deprecated; it raises TypeError starting from Python 3.14
2172+
# and returns True for versions under 3.14
2173+
if self.value is NotImplemented:
2174+
return util.Uninferable if PY314_PLUS else True
21712175
return bool(self.value)
21722176

21732177
def _infer(

tests/test_inference.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2984,6 +2984,14 @@ def __bool__(self):
29842984
inferred = next(instance.infer())
29852985
self.assertIs(inferred.bool_value(), util.Uninferable)
29862986

2987+
def test_bool_value_not_implemented(self) -> None:
2988+
node = extract_node("""NotImplemented""")
2989+
inferred = next(node.infer())
2990+
if PY314_PLUS:
2991+
self.assertIs(inferred.bool_value(), util.Uninferable)
2992+
else:
2993+
self.assertIs(inferred.bool_value(), True)
2994+
29872995
def test_infer_coercion_rules_for_floats_complex(self) -> None:
29882996
ast_nodes = extract_node(
29892997
"""

0 commit comments

Comments
 (0)