Skip to content

Boolean expression performs redundant bool check #3567

Closed

Description

@dimpolo
n = 0

class Test:
    def __bool__(self):
        global n
        n += 1
        return False

Test() and False or False
assert n == 1

CPython optimizes this check in:
https://github.com/python/cpython/blob/54ab9ad312ea53db40e31712454272e1d4c0315f/Python/compile.c#L8708-L8721

Activity

  1. crazymerlyn commented on Sep 7, 2024

    @crazymerlyn
    Contributor

    Looks like the assertion fails again since python 3.12: https://godbolt.org/z/qoKcdvz4T

  2. moreal commented on Feb 14, 2026

    @moreal
    Contributor

    In CPython 3.14, the assertion has started to pass again.

    https://godbolt.org/z/1Genzf9sE

    Image
  3. added a commit that references this issue on Feb 17, 2026
  4. added a commit that references this issue on Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions