Skip to content

Fix bare except clause in alembic/util/messaging.py - #1830

Open
koteshyelamati wants to merge 1 commit into
sqlalchemy:mainfrom
koteshyelamati:main
Open

Fix bare except clause in alembic/util/messaging.py#1830
koteshyelamati wants to merge 1 commit into
sqlalchemy:mainfrom
koteshyelamati:main

Conversation

@koteshyelamati

Copy link
Copy Markdown

Replace bare except: with except Exception: in the status() context manager.

The status() context manager prints a status message and a completion/failure indicator. Its bare except: catches BaseException, meaning KeyboardInterrupt or SystemExit during the operation would trigger the failure message and then re-raise — but the intent is clearly to catch operational exceptions, not signals. except Exception: is the correct type here.

@MohammedAlkindi MohammedAlkindi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran this on Windows 11 (10.0.26200), CPython 3.13.13, against main at c116cbc0. Failing-test set is identical to base (13 tests, the test_post_write and ScriptNamingTest timezone clusters, both failing on main here) — no regressions, none fixed.

One thing worth deciding deliberately before this lands, because it is a behaviour change rather than a pure lint fix. The bare except: is inside a context manager that re-raises immediately:

@contextmanager
def status(status_msg, newline=False, quiet=False):
    msg(status_msg + " ...", newline, flush=True, quiet=quiet)
    try:
        yield
    except:
        if not quiet:
            write_outstream(sys.stdout, "  FAILED\n")
        raise

Because it re-raises unconditionally, this is one of the few genuinely defensible bare excepts — it swallows nothing. What it catches is everything, deliberately, so that the status line gets closed out. Narrowing to except Exception: means KeyboardInterrupt and SystemExit no longer print FAILED, so a Ctrl-C during a migration now leaves the status line dangling at Running upgrade ... with no terminator, where today it prints FAILED.

That may well be what you want — arguably Ctrl-C is not a failure and shouldn't be labelled one. But it is a user-visible change to interrupt handling, not a no-op cleanup, so it seemed worth surfacing rather than letting it ride in as an E722 fix.

If the intent is purely to satisfy the linter while keeping the behaviour, except BaseException: does that exactly. If the intent is that interrupts should stop being reported as failures, then this patch is right as written and might deserve a line in the changelog, since anyone scripting against that output would see it change.

Either way the one-line diff is correct in isolation and I have no objection to it landing — I just did not want the interrupt case to go unnoticed.

@CaselIT
CaselIT requested a review from zzzeek August 15, 2026 21:42
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