Copilot commented on code in PR #73827:
URL: https://github.com/apache/airflow/pull/73827#discussion_r4743784483


##########
task-sdk/src/airflow/sdk/serde/__init__.py:
##########
@@ -377,10 +377,8 @@ def deserialize(o: T | None, full=True, type_hint: Any = 
None) -> object:
         class_version = getattr(cls, "__version__", 0)
         if int(version) > class_version:
             raise TypeError(
-                "serialized version of %s is newer than module version (%s > 
%s)",
-                classname,
-                version,
-                class_version,
+                f"serialized version of {classname} is newer than module 
version "
+                f"({version} > {class_version})"

Review Comment:
   The corrected runtime messages lack regression assertions: for example, 
`TestDeserialize.test_incompatible_version` only matches `"newer than"`, which 
also passes with the old tuple-shaped message. Add assertions for the complete 
rendered messages and interpolated values across the changed SDK, core, and 
provider error paths, so reverting each interpolation fails its corresponding 
test.



##########
scripts/ci/prek/check_exception_format_args.py:
##########
@@ -0,0 +1,187 @@
+#!/usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# /// script
+# requires-python = ">=3.10"
+# dependencies = [
+#   "rich>=13.0.0",
+# ]
+# ///
+"""Check that no ``raise SomeError("... %s ...", value)`` usages are 
introduced.
+
+Exception constructors do not interpolate their arguments the way ``logger``
+calls do -- ``Exception.__init__`` just stores everything in ``args``. So::
+
+    raise AirflowException("TaskInstance %s is not found", ti.task_id)
+
+renders as ``('TaskInstance %s is not found', 'my_task')`` rather than the
+intended sentence, and ``AirflowException.serialize()`` carries that same
+``str(self)`` across the trigger/task boundary. Exceptions whose ``__init__``
+forwards only the message to ``super()`` -- ``google.api_core``'s
+``GoogleAPICallError`` family, for instance -- drop the trailing arguments
+outright, so the identifier the message exists to carry never reaches the user.
+Interpolate the message with an f-string instead.
+
+Detection is AST-based because the pattern routinely spans several lines, and a
+call is only flagged when the number of ``%`` placeholders in the leading 
string
+literal exactly matches the number of trailing arguments -- counted the way
+Python's own ``%`` operator would consume them, flags and width and precision
+included. Anything looser misfires on the ten or so places that already pass a
+literal message alongside unrelated positional parameters, such as
+``TypeError("Could not parse hits.", response)`` in the elasticsearch provider 
or
+SQLAlchemy's ``OperationalError(statement, params, orig)``.
+
+Precision costs some recall, deliberately. The check sees only an exception
+raised inline, so ``err = ValueError("%s", x); raise err`` slips past, as does
+any call whose keyword arguments make the count disagree. Dict-style
+``%(name)s`` and ``*`` widths are skipped outright: both consume arguments in a
+way a plain count cannot describe.
+
+The one place this knowingly parts company with Python is the space flag.
+Python reads the ``% c`` in ``"download is 50% complete"`` as a conversion, so
+a message writing a percentage in English and passing one context object beside
+it would be flagged with nothing to fix. No message in the tree uses ``% d`` or
+``% i`` for its intended purpose, so the flag is dropped from the grammar and
+the English reading wins.
+"""
+
+from __future__ import annotations
+
+import argparse
+import ast
+import re
+from pathlib import Path
+
+from common_prek_utils import AIRFLOW_ROOT_PATH
+from rich.console import Console
+from rich.panel import Panel
+
+console = Console(color_system="standard", width=200)
+
+REPO_ROOT = AIRFLOW_ROOT_PATH
+
+_FORMAT_TOKEN_RE = re.compile(
+    r"""
+    %
+    (?:
+        %                                   # an escaped literal percent
+      | (?P<mapping>\([^)]*\))?            # mapping key, for dict-style 
formatting
+        [#0\-+]*                            # flags, minus the space flag (see 
below)
+        (?:\*|\d+)?                        # minimum field width
+        (?:\.(?:\*|\d+))?                  # precision
+        [hlLqjzt]?                          # length modifier, accepted and 
ignored

Review Comment:
   The regex misses valid zero-precision conversions such as `%.f`, so `raise 
ValueError("took %.f sec", elapsed)` escapes detection, while it incorrectly 
counts unsupported forms such as `%zd`. Allow an empty numeric precision, 
restrict length modifiers to `h`, `l`, and `L`, and add regression cases for 
both forms.



##########
scripts/ci/prek/check_exception_format_args.py:
##########
@@ -0,0 +1,187 @@
+#!/usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# /// script
+# requires-python = ">=3.10"

Review Comment:
   This requirement allows the standalone hook to select Python minors beyond 
Airflow’s current minimum, unlike neighboring hooks such as 
`scripts/ci/prek/check_provider_docs.py:19`, which constrain the interpreter to 
3.10. Please use the repository’s `>=3.10,<3.11` pin so this hook selects the 
current minimum supported minor.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to