Skip to content

Keep guarded queries inside a bounded else suite - #14

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

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

Conversation

@EiNSTeiN-

Copy link
Copy Markdown

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.

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.

1 participant