amoghrajesh commented on code in PR #71212:
URL: https://github.com/apache/airflow/pull/71212#discussion_r3747471092


##########
scripts/ci/prek/check_sdk_imports_in_core.py:
##########
@@ -42,23 +89,122 @@ def check_file_for_sdk_imports(file_path: Path) -> 
list[tuple[int, str]]:
     )
 
 
-def main():
-    parser = argparse.ArgumentParser(description="Check for SDK imports in 
airflow-core files")
-    parser.add_argument("files", nargs="*", help="Files to check")
-    args = parser.parse_args()
+class SdkImportsAllowlistManager(AllowlistManager):
+    def __init__(self, allowlist_file: Path) -> None:
+        super().__init__(allowlist_file, repo_root=REPO_ROOT)
 
-    if not args.files:
-        return
+    def is_excluded(self, path: Path) -> bool:
+        return not EXCLUDED_DIR_NAMES.isdisjoint(path.parts)
 
-    report_import_violations(
-        args.files,
-        check_func=check_file_for_sdk_imports,
-        violation_label="SDK import(s) in core files",
-        nocheck_code=NOCHECK_CODE,
-        only_python_files=True,
+    def iter_files(self) -> Iterable[Path]:
+        return (path for path in CORE_SRC_ROOT.rglob("*.py") if not 
self.is_excluded(path))
+
+    def check(self, files: list[Path], allowlist: dict[str, int]) -> int:
+        return super().check([path for path in files if not 
self.is_excluded(path)], allowlist)
+
+    def count_occurrences(self, path: Path) -> int:
+        return len(check_file_for_sdk_imports(path))

Review Comment:
   Fixed. Added a `_can_parse()` check (tries `ast.parse`) and filter 
unparseable files out before they ever reach count_occurrences, in check(), 
generate_for(), and iter_files(). A file that fails to parse now gets skipped 
with a warning instead of silently counting as 0 and tightening/deleting its 
allowlist entry.



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