Behavior change for `foo and 1 or 2`: 3.12 newly converts `foo` to bool twice
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
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 42
In 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
- gh-124394
- gh-124723
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the provided Foo.__bool__ reproducer on Python 3.11, 3.12, and 3.13, then review linked PRs gh-124394 and gh-124723 for the current direction. Done means the behavior change is resolved or explicitly documented with regression coverage and the expected result is clear.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100