Remove the unreachable 404 from the create Variable endpoint - #71245
Open
ColtenOuO wants to merge 1 commit into
Open
Remove the unreachable 404 from the create Variable endpoint#71245ColtenOuO wants to merge 1 commit into
ColtenOuO wants to merge 1 commit into
Conversation
The branch guards a read-back of the row Variable.set() upserted moments earlier through the same session, so it cannot be reached. It exists only to narrow a type: SQLAlchemy 2 declares Session.scalar() as returning Optional, and a raise is what convinces mypy the value is not None. Paying for that with an HTTP status is the problem. A 404 on a create endpoint tells a caller the variable they just created was not found, which left the endpoint choosing between publishing a response it can never return and leaving its spec incomplete. Asking the session for exactly one row states the same invariant where it belongs — in the query — so neither control flow nor a status code is needed to express it.
ColtenOuO
requested review from
bugraoz93,
choo121600,
ephraimbuddy,
henry3260,
jason810496,
pierrejeambrun,
rawwar and
shubhamraj-git
as code owners
August 6, 2026 15:40
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sumarry
Split out of the #71011 review.
Remove the unreachable 404 from the create Variable endpoint
The branch cannot be reached
Variable.set()upserts the row through the same session the read-back then queries, so theread cannot come up empty. The status is also wrong on its own terms: a
404on a createendpoint tells a caller the variable they just created was not found.
That left the endpoint with a choice between publishing a response it can never return and
leaving its OpenAPI spec incomplete — which is what surfaced during #71011.
Why it could not simply be deleted
It was added in #56813 as part of the
SQLAlchemy 2 typing cleanup. SQLAlchemy types the method as returning an optional:
So
variableisVariable | None, and returning it fails:The
raiseis what fixes that — it never returns, so mypy narrows the value toVariablebelow it. Dropping the branch on its own puts the original error back.
The change
ScalarResult.one()is typed-> _R, not-> Optional[_R], because its contract already is"exactly one row, or raise". The invariant moves into the query instead of being asserted by
control flow, so nothing has to narrow a type and no HTTP status is spent doing it. If the
invariant were ever violated,
NoResultFoundsurfaces as a500, which is the honest answerfor a write that silently did not happen.
.limit(1)goes with it:Variable.keyisunique=True, so.one()is already exact andthe limit would only contradict it.
Behaviour
Unchanged — the removed branch was unreachable. No spec change either: the
404was neverdeclared in
responses=, so the generated OpenAPI spec and UI client are untouched.