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


##########
scripts/ci/prek/check_core_imports_in_sdk.py:
##########
@@ -25,44 +25,24 @@
 from __future__ import annotations
 
 import argparse
-import ast
 import sys
 from pathlib import Path
 
-from common_prek_utils import console
+from common_prek_utils import find_import_violations, report_import_violations
+
+NOCHECK_CODE = "SDK002"
 
 
 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:
-        source = file_path.read_text(encoding="utf-8")
-        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
+    return find_import_violations(
+        file_path,
+        is_violating_module=lambda module: (
+            module.startswith("airflow.") and not 
module.startswith("airflow.sdk")
+        ),
+        nocheck_code=NOCHECK_CODE,
+        check_plain_imports=True,
+    )

Review Comment:
   The PR description says the inline suppression marker is `# nocheck: 
core-imports`, but the implementation/test suite use `# noqa: SDK002` (and for 
the other hook `# noqa: SDK001`). Please align the PR description (and/or any 
user-facing docs/help text) with the actual marker syntax so users don’t try 
the wrong comment and think the hook is broken.



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