Skip to content

Preserve nested conditional values in comprehension elements - #19

Closed
EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/nested-comprehension-conditionals
Closed

EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/nested-comprehension-conditionals

Conversation

@EiNSTeiN-

Copy link
Copy Markdown
Contributor

I am opening this PR to preserve nested conditional values in both arms of a comprehension element. The code below currently loses the entire comprehension and decompiles as return rows on Python 3.14:

def select(rows):
    return [{
        "excluded": ({"count": row["count"],
                      "reasons": loads(row["reasons"]) if row["reasons"] else {}}
                     if row["count"] is not None else {}),
        "archived": str(row["archived"]) if row["archived"] else None,
    } for row in rows]

The outer then arm constructs a dictionary containing another conditional expression. The pure-value scan handles nested conditionals in the else arm but can stop prematurely in the then arm. It also omits dedicated None-test jumps from its recognized conditional-jump family.

The change makes genexpr_ternary_merge recursively prove and skip a nested pure-value region in either arm, retaining the enclosing arm's actual final merge. This preserves the dictionary construction, decoding calls and their failures on the selected arm. A false outer condition still skips the inner decoding call entirely.

The same reconstruction handles nested conditional call arguments and None tests in either arm:

[build(1 if row.inner else 2) if row.outer else 3 for row in rows]
[1 if row.outer else build(2 if row.inner else 3) for row in rows]
[build(1 if row.inner is None else 2) if row.outer
 else build(3 if row.inner is not None else 4) for row in rows]

Conditional expressions begin in Python 2.5. This repair restores these examples on the Python 3.12+ inlined-comprehension path; older comprehension reconstruction still has gaps. docs/LIMITATIONS.md distinguishes the repaired nested-arm case from those remaining older-version limitations.

@ejfkdev

ejfkdev commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Merged via consolidated PR #28 (commit e629b6c). Your fix, fixtures, behavior cases and integration test are all included and verified together: real-world corpus 6411/6411, stdlib corpus 528/528, behavior tests green on 3.9-3.14. Thanks for the detailed reports and repros!

@ejfkdev ejfkdev closed this Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants