This is an automated email from the ASF dual-hosted git repository.

pierrejeambrun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 6171e2bf992 Remove the unreachable 404 from the create Variable 
endpoint (#71245)
6171e2bf992 is described below

commit 6171e2bf9922f21db4729062356eb28963bec978
Author: Jyun-An Chen <[email protected]>
AuthorDate: Fri Aug 28 16:51:26 2026 +0800

    Remove the unreachable 404 from the create Variable endpoint (#71245)
    
    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.
---
 .../src/airflow/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 e48031cd68d..ab753437614 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
@@ -183,14 +183,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(

Reply via email to