Skip to content

Keep guarded queries inside a bounded else suite - #14

Closed
EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/guarded-else-merge
Closed

EiNSTeiN- wants to merge 2 commits into
ejfkdev:mainfrom
EiNSTeiN-:fix/guarded-else-merge

Conversation

@EiNSTeiN-

Copy link
Copy Markdown
Contributor

I am opening this PR to keep an inner query-result guard inside its enclosing else when both share a merge target. The code below currently moves the guard outside the else, where row can be unassigned:

async def select(owner):
    value = None
    active = owner.active
    if active:
        value = 1
    else:
        row = await owner.fetch()
        if row and row[0] is not None:
            value = float(row[0])
    owner.record(value)
    return value

The affected region currently becomes:

    if active:
        value = 1
    else:
        row = await owner.fetch()
    if row and row[0] is not None:
        value = float(row[0])

On the active branch, the moved guard reads row before it has been assigned.

An inner guard can share the enclosing else's merge target. The block-closing rule currently closes that else before parsing the guard, moving it to the surrounding scope. The fix preserves the else when its start is immediately preceded by an unconditional then-arm jump that proves its finite end.

Variations include synchronous fetches, an attribute source, literal/call/subscript value expressions, subsequent work in the else, and an independent guard outside it. A guard that is actually outside the else keeps that placement. The additional boundary proof applies to the existing Python 3.11+ path.

@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