Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1593,4 +1593,11 @@ repos:
additional_dependencies: ['rich>=12.4.4', 'ruff==0.12.1']
files: ^providers/.*/src/airflow/providers/.*version_compat.*\.py$
require_serial: true
- id: provider-version-compat
name: Check for correct version_compat imports in providers
entry: ./scripts/ci/pre_commit/check_provider_version_compat.py
language: system
types: [python]
files: ^providers/.*/src/airflow/providers/.*\.py$
require_serial: true
## ONLY ADD PRE-COMMITS HERE THAT REQUIRE CI IMAGE
2 changes: 2 additions & 0 deletions contributing-docs/08_static_code_checks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ require Breeze Docker image to be built locally.
+-----------------------------------------------------------+--------------------------------------------------------+---------+
| prevent-deprecated-sqlalchemy-usage | Prevent deprecated sqlalchemy usage | |
+-----------------------------------------------------------+--------------------------------------------------------+---------+
| provider-version-compat | Check for correct version_compat imports in providers | * |
+-----------------------------------------------------------+--------------------------------------------------------+---------+
| pylint | pylint | |
+-----------------------------------------------------------+--------------------------------------------------------+---------+
| python-no-log-warn | Check if there are no deprecate log warn | |
Expand Down
8 changes: 4 additions & 4 deletions dev/breeze/doc/images/output_static-checks.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion dev/breeze/doc/images/output_static-checks.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bfb0d23efe62297165a4aacc0cddcfb1
c3e2295a3b4cfc4073a1c8bea4ee4f3b
1 change: 1 addition & 0 deletions dev/breeze/src/airflow_breeze/pre_commit_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"mypy-task-sdk",
"pretty-format-json",
"prevent-deprecated-sqlalchemy-usage",
"provider-version-compat",
"pylint",
"python-no-log-warn",
"replace-bad-characters",
Expand Down
73 changes: 73 additions & 0 deletions scripts/ci/pre_commit/check_provider_version_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/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.
from __future__ import annotations

import re
import sys
from pathlib import Path

IMPORT_RE = re.compile(r"^from airflow\.providers\.([a-zA-Z0-9_]+)\.version_compat import (.+)$")


def get_provider_from_path(path: Path):
"""Extract provider name from file path."""
try:
parts = path.parts
idx = parts.index("providers")
return parts[idx + 1]
except (ValueError, IndexError):
return None


def check_and_fix_file(path: Path):
provider = get_provider_from_path(path)

changed = False
lines = path.read_text().splitlines()
new_lines = []
for line in lines:
m = IMPORT_RE.match(line.strip())
if m:
imported_provider, rest = m.groups()
if imported_provider != provider:
print(
f"[pre-commit] {path}: Import from wrong provider: {imported_provider} (should be {provider})"
)
# auto fix: rewrite the import correctly
line = f"from airflow.providers.{provider}.version_compat import {rest}"
changed = True
new_lines.append(line)
if changed:
path.write_text("\n".join(new_lines) + "\n")
return not changed


def main():
failed = False
for filename in sys.argv[1:]:
path = Path(filename)
if path.suffix == ".py":
if not check_and_fix_file(path):
failed = True
if failed:
print("[pre-commit] Fixed some imports. Please re-add and commit.")
sys.exit(1)


if __name__ == "__main__":
main()