Prab-27 commented on code in PR #60676:
URL: https://github.com/apache/airflow/pull/60676#discussion_r2703403507


##########
scripts/ci/prek/check_core_imports_in_sdk.py:
##########
@@ -0,0 +1,97 @@
+#!/usr/bin/env python
+#
+# 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,<3.11"
+# dependencies = [
+#   "rich>=13.6.0",
+# ]
+# ///
+from __future__ import annotations
+
+import argparse
+import ast
+import sys
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).parent.resolve()))
+from common_prek_utils import console
+
+
+def check_file_for_core_imports(file_path: Path) -> list[tuple[int, str]]:
+    """Check file for airflow-core imports (anything except airflow.sdk). 
Returns list of (line_num, import_statement)."""
+    try:
+        with open(file_path, encoding="utf-8") as f:
+            source = f.read()
+            tree = ast.parse(source, filename=str(file_path))
+    except (OSError, UnicodeDecodeError, SyntaxError):
+        return []
+
+    mismatches = []
+
+    for node in ast.walk(tree):
+        # for `from airflow.x import y` statements
+        if isinstance(node, ast.ImportFrom):
+            if (
+                node.module
+                and node.module.startswith("airflow.")
+                and not node.module.startswith("airflow.sdk")
+            ):
+                import_names = ", ".join(alias.name for alias in node.names)
+                statement = f"from {node.module} import {import_names}"
+                mismatches.append((node.lineno, statement))
+        # for `import airflow.x` statements
+        elif isinstance(node, ast.Import):
+            for alias in node.names:
+                if alias.name.startswith("airflow.") and not 
alias.name.startswith("airflow.sdk"):
+                    statement = f"import {alias.name}"
+                    if alias.asname:
+                        statement += f" as {alias.asname}"
+                    mismatches.append((node.lineno, statement))
+
+    return mismatches
+
+
+def main():
+    parser = argparse.ArgumentParser(description="Check for core imports in 
task-sdk files")
+    parser.add_argument("files", nargs="*", help="Files to check")
+    args = parser.parse_args()
+
+    if not args.files:
+        return
+
+    files_to_check = [Path(f) for f in args.files if f.endswith(".py")]

Review Comment:
   Wow! I didn’t know about this — I tried running `uv run --script 
task-sdk/src/airflow/sdk/log.py`, and it's building (it has also some --python 
options). 
   
   I felt that UV installs dependencies much faster than Breeze image building. 
Could we also use only UV tools instead of image building ? (like rebuilding 
the image and other things )
   



-- 
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