From e911207b39951610c6546fcadab27196f27f0763 Mon Sep 17 00:00:00 2001 From: ColtenOuO Date: Thu, 6 Aug 2026 15:31:24 +0000 Subject: [PATCH] Remove the unreachable 404 from the create Variable endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../api_fastapi/core_api/routes/public/variables.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/airflow-core/src/airflow/api_fastapi/core_api/routes/public/variables.py b/airflow-core/src/airflow/api_fastapi/core_api/routes/public/variables.py index 705880c4898a6..b481db00ea5e6 100644 --- a/airflow-core/src/airflow/api_fastapi/core_api/routes/public/variables.py +++ b/airflow-core/src/airflow/api_fastapi/core_api/routes/public/variables.py @@ -174,14 +174,7 @@ def post_variable( Variable.set(**post_body.model_dump(), session=session) - variable = session.scalar(select(Variable).where(Variable.key == post_body.key).limit(1)) - if variable is None: - raise HTTPException( - status.HTTP_404_NOT_FOUND, - f"Variable with key: `{post_body.key}` was not found", - ) - - return variable + return session.scalars(select(Variable).where(Variable.key == post_body.key)).one() @variables_router.patch(