-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Introducing prek hook to detect airflow-core imports in task-sdk #60676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
amoghrajesh
merged 5 commits into
apache:main
from
amoghrajesh:prek-hook-to-check-core-imports
Jan 19, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1e021fb
Introducing prek hook to detect airflow-core imports in task-sdk
amoghrajesh f449639
Introducing prek hook to detect airflow-core imports in task-sdk
amoghrajesh d5fae93
catch import airflow.x too
amoghrajesh 8889509
suggestion from Prab-27
amoghrajesh c80058c
suggestion from Prab-27
amoghrajesh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| #!/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: | ||
| 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): | ||
amoghrajesh marked this conversation as resolved.
Show resolved
Hide resolved
amoghrajesh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 | ||
|
|
||
| total_violations = 0 | ||
|
|
||
| for file_path in [Path(f) for f in args.files]: | ||
| mismatches = check_file_for_core_imports(file_path) | ||
| if mismatches: | ||
| console.print(f"[red]{file_path}[/red]:") | ||
| for line_num, statement in mismatches: | ||
| console.print(f" [yellow]Line {line_num}[/yellow]: {statement}") | ||
| total_violations += len(mismatches) | ||
|
|
||
| if total_violations: | ||
| console.print() | ||
| console.print(f"[red]Found {total_violations} core import(s) in task-sdk files[/red]") | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| sys.exit(0) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.