-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Open
Open
Copy link
Labels
3.14bugs and security fixesbugs and security fixesdocsDocumentation in the Doc dirDocumentation in the Doc dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
We noticed a behavior change between 3.11 and 3.12. The following code calls Foo.__bool__ once in 3.11 and twice in 3.12. Consequently for this contrived example, the expression evaluates to different results in 3.11 and 3.12.
class Foo:
def __init__(self):
self._a = True
def __bool__(self):
self._a = not self._a
print(f"Foo.__bool__ -> {self._a}")
return self._a
Foo() and "a string" or 42In Python 3.11:
>>> Foo() and "a string" or 42
Foo.__bool__ -> False
42
In Python 3.12 (and 3.13.0b2):
>>> Foo() and "a string" or 42
Foo.__bool__ -> False
Foo.__bool__ -> True
<__main__.Foo object at 0x7f0ae554c1a0>
Is this change intentional?
Note that I'm not necessarily asking to change this. We should arguably change the code to "a string" if Foo() else 42, which evaluates the same in 3.11 and 3.12.
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs
skirpichev, gpshead, Eclips4, ZeroIntensity and fruitoiz
Metadata
Metadata
Assignees
Labels
3.14bugs and security fixesbugs and security fixesdocsDocumentation in the Doc dirDocumentation in the Doc dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
Status
Todo