You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "amoghrajesh (via GitHub)" <gi...@apache.org> on 2026/05/20 05:09:28 UTC

[PR] Prek hook to detect drift in RuntimeTaskInstance and its protocol [airflow]

amoghrajesh opened a new pull request, #67223:
URL: https://github.com/apache/airflow/pull/67223

    <!-- SPDX-License-Identifier: Apache-2.0
         https://www.apache.org/licenses/LICENSE-2.0 -->
   
   <!--
   Thank you for contributing!
   
   Please provide above a brief description of the changes made in this pull request.
   Write a good git commit message following this guide: http://chris.beams.io/posts/git-commit/
   
   Please make sure that your code changes are covered with tests.
   And in case of new features or big changes remember to adjust the documentation.
   
   Feel free to ping (in general) for the review if you do not see reaction for a few days
   (72 Hours is the minimum reaction time you can expect from volunteers) - we sometimes miss notifications.
   
   In case of an existing issue, reference it using one of the following:
   
   * closes: #ISSUE
   * related: #ISSUE
   -->
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   <!--
   If generative AI tooling has been used in the process of authoring this PR, please
   change below checkbox to `[X]` followed by the name of the tool, uncomment the "Generated-by".
   -->
   
   - [ ] Yes (please specify the tool below)
   
   <!--
   Generated-by: [Tool Name] following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   -->
   
   related to: https://github.com/apache/airflow/pull/67216
   
   `RuntimeTaskInstanceProtocol`and `RuntimeTaskInstance` have drifted in the past most recently fixed in https://github.com/apache/airflow/pull/67216. This PR adds a prek hook to catch that automatically.
   
   The hook parses both files via AST and fails if:
   
   - A public member declared in RuntimeTaskInstance is absent from the Protocol
   - A shared method has mismatched positional parameter names (the specific bug #67216 fixed)
   - Members intentionally absent from the Protocol (e.g. bundle_instance, sentry_integration) are covered by an explicit `IMPLEMENTATION_ONLY` allow list with comments. The hook triggers on changes to either file.
   
   
   ---
   
   * Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
   * For fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   * When adding dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   * For significant user-facing changes create newsfragment: `{pr_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments). You can add this file in a follow-up commit after the PR is created so you know the PR number.
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Prek hook to detect drift in RuntimeTaskInstance and its protocol [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #67223:
URL: https://github.com/apache/airflow/pull/67223#discussion_r3494110073


##########
scripts/ci/prek/check_runtime_ti_protocol_sync.py:
##########
@@ -0,0 +1,165 @@
+#!/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",
+# ]
+# ///
+"""Check that RuntimeTaskInstanceProtocol stays in sync with RuntimeTaskInstance.
+
+Parses both the files via AST and checks for:
+  1. Public members declared in RuntimeTaskInstance that are absent from
+     RuntimeTaskInstanceProtocol aka incomplete protocol definition.
+  2. Methods present in both classes whose positional parameter names differ.
+"""
+
+from __future__ import annotations
+
+import ast
+import dataclasses
+import sys
+from pathlib import Path
+
+from common_prek_utils import AIRFLOW_TASK_SDK_SOURCES_PATH, console
+
+IMPL_PATH = AIRFLOW_TASK_SDK_SOURCES_PATH / "airflow" / "sdk" / "execution_time" / "task_runner.py"
+PROTOCOL_PATH = AIRFLOW_TASK_SDK_SOURCES_PATH / "airflow" / "sdk" / "types.py"
+
+IMPL_CLASS = "RuntimeTaskInstance"
+PROTOCOL_CLASS = "RuntimeTaskInstanceProtocol"
+
+# Members present in RuntimeTaskInstance but intentionally absent from the Protocol.
+# Each entry must carry a comment explaining why it is excluded.
+IMPLEMENTATION_ONLY: set[str] = {
+    # Worker internal: the DAG bundle used
+    "bundle_instance",
+    # Observability / Sentry integration string.
+    "sentry_integration",
+    # Internal helper for scheduler-side upstream map index resolution; not
+    # part of the public task execution interface.
+    "get_relevant_upstream_map_indexes",
+}
+
+
+@dataclasses.dataclass
+class ClassMembers:
+    """Public members declared directly in a class body."""
+
+    fields: set[str] = dataclasses.field(default_factory=set)
+    # method name mapped to list of positional param names
+    methods: dict[str, list[str]] = dataclasses.field(default_factory=dict)
+    properties: set[str] = dataclasses.field(default_factory=set)
+
+    @property
+    def all_names(self) -> set[str]:
+        return self.fields | set(self.methods) | self.properties
+
+
+def _is_property(node: ast.FunctionDef | ast.AsyncFunctionDef) -> bool:
+    for dec in node.decorator_list:
+        if isinstance(dec, ast.Name) and dec.id == "property":
+            return True
+        if isinstance(dec, ast.Attribute) and dec.attr == "property":
+            return True
+    return False
+
+
+def _is_public(name: str) -> bool:
+    return not name.startswith("_")
+
+
+def extract_class_members(source_path: Path, class_name: str) -> ClassMembers:
+    tree = ast.parse(source_path.read_text(), filename=str(source_path))
+    members = ClassMembers()
+
+    for node in ast.walk(tree):
+        if not (isinstance(node, ast.ClassDef) and node.name == class_name):
+            continue
+        for item in node.body:
+            if isinstance(item, ast.AnnAssign) and isinstance(item.target, ast.Name):
+                name = item.target.id
+                if _is_public(name):
+                    members.fields.add(name)
+            elif isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef)):
+                name = item.name
+                if not _is_public(name):
+                    continue
+                if _is_property(item):
+                    members.properties.add(name)
+                else:
+                    params = [a.arg for a in item.args.args if a.arg != "self"]

Review Comment:
   Also should we consider classmethods too (`cls`)?



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Prek hook to detect drift in RuntimeTaskInstance and its protocol [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #67223:
URL: https://github.com/apache/airflow/pull/67223#discussion_r3494094459


##########
scripts/ci/prek/check_runtime_ti_protocol_sync.py:
##########
@@ -0,0 +1,165 @@
+#!/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",
+# ]
+# ///
+"""Check that RuntimeTaskInstanceProtocol stays in sync with RuntimeTaskInstance.
+
+Parses both the files via AST and checks for:
+  1. Public members declared in RuntimeTaskInstance that are absent from
+     RuntimeTaskInstanceProtocol aka incomplete protocol definition.
+  2. Methods present in both classes whose positional parameter names differ.
+"""
+
+from __future__ import annotations
+
+import ast
+import dataclasses
+import sys
+from pathlib import Path
+
+from common_prek_utils import AIRFLOW_TASK_SDK_SOURCES_PATH, console
+
+IMPL_PATH = AIRFLOW_TASK_SDK_SOURCES_PATH / "airflow" / "sdk" / "execution_time" / "task_runner.py"
+PROTOCOL_PATH = AIRFLOW_TASK_SDK_SOURCES_PATH / "airflow" / "sdk" / "types.py"
+
+IMPL_CLASS = "RuntimeTaskInstance"
+PROTOCOL_CLASS = "RuntimeTaskInstanceProtocol"
+
+# Members present in RuntimeTaskInstance but intentionally absent from the Protocol.
+# Each entry must carry a comment explaining why it is excluded.
+IMPLEMENTATION_ONLY: set[str] = {
+    # Worker internal: the DAG bundle used
+    "bundle_instance",
+    # Observability / Sentry integration string.
+    "sentry_integration",
+    # Internal helper for scheduler-side upstream map index resolution; not
+    # part of the public task execution interface.
+    "get_relevant_upstream_map_indexes",
+}
+
+
+@dataclasses.dataclass
+class ClassMembers:
+    """Public members declared directly in a class body."""
+
+    fields: set[str] = dataclasses.field(default_factory=set)
+    # method name mapped to list of positional param names
+    methods: dict[str, list[str]] = dataclasses.field(default_factory=dict)
+    properties: set[str] = dataclasses.field(default_factory=set)
+
+    @property
+    def all_names(self) -> set[str]:
+        return self.fields | set(self.methods) | self.properties
+
+
+def _is_property(node: ast.FunctionDef | ast.AsyncFunctionDef) -> bool:
+    for dec in node.decorator_list:
+        if isinstance(dec, ast.Name) and dec.id == "property":
+            return True
+        if isinstance(dec, ast.Attribute) and dec.attr == "property":
+            return True
+    return False
+
+
+def _is_public(name: str) -> bool:
+    return not name.startswith("_")
+
+
+def extract_class_members(source_path: Path, class_name: str) -> ClassMembers:
+    tree = ast.parse(source_path.read_text(), filename=str(source_path))
+    members = ClassMembers()
+
+    for node in ast.walk(tree):
+        if not (isinstance(node, ast.ClassDef) and node.name == class_name):
+            continue
+        for item in node.body:
+            if isinstance(item, ast.AnnAssign) and isinstance(item.target, ast.Name):
+                name = item.target.id
+                if _is_public(name):
+                    members.fields.add(name)
+            elif isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef)):
+                name = item.name
+                if not _is_public(name):
+                    continue
+                if _is_property(item):
+                    members.properties.add(name)
+                else:
+                    params = [a.arg for a in item.args.args if a.arg != "self"]

Review Comment:
   This also needs to check `posonlyargs`. We don’t currently use the syntax anywhere in the main code base, but this could change in the future.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Prek hook to detect drift in RuntimeTaskInstance and its protocol [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on PR #67223:
URL: https://github.com/apache/airflow/pull/67223#issuecomment-4551274111

   Will get back to this when I have b/w next week


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Prek hook to detect drift in RuntimeTaskInstance and its protocol [airflow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #67223:
URL: https://github.com/apache/airflow/pull/67223#issuecomment-5287896192

   This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Prek hook to detect drift in RuntimeTaskInstance and its protocol [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #67223:
URL: https://github.com/apache/airflow/pull/67223#discussion_r3494097613


##########
scripts/ci/prek/check_runtime_ti_protocol_sync.py:
##########
@@ -0,0 +1,165 @@
+#!/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",
+# ]
+# ///
+"""Check that RuntimeTaskInstanceProtocol stays in sync with RuntimeTaskInstance.
+
+Parses both the files via AST and checks for:
+  1. Public members declared in RuntimeTaskInstance that are absent from
+     RuntimeTaskInstanceProtocol aka incomplete protocol definition.
+  2. Methods present in both classes whose positional parameter names differ.
+"""
+
+from __future__ import annotations
+
+import ast
+import dataclasses
+import sys
+from pathlib import Path
+
+from common_prek_utils import AIRFLOW_TASK_SDK_SOURCES_PATH, console
+
+IMPL_PATH = AIRFLOW_TASK_SDK_SOURCES_PATH / "airflow" / "sdk" / "execution_time" / "task_runner.py"
+PROTOCOL_PATH = AIRFLOW_TASK_SDK_SOURCES_PATH / "airflow" / "sdk" / "types.py"
+
+IMPL_CLASS = "RuntimeTaskInstance"
+PROTOCOL_CLASS = "RuntimeTaskInstanceProtocol"
+
+# Members present in RuntimeTaskInstance but intentionally absent from the Protocol.
+# Each entry must carry a comment explaining why it is excluded.
+IMPLEMENTATION_ONLY: set[str] = {
+    # Worker internal: the DAG bundle used
+    "bundle_instance",
+    # Observability / Sentry integration string.
+    "sentry_integration",
+    # Internal helper for scheduler-side upstream map index resolution; not
+    # part of the public task execution interface.
+    "get_relevant_upstream_map_indexes",
+}
+
+
+@dataclasses.dataclass
+class ClassMembers:
+    """Public members declared directly in a class body."""
+
+    fields: set[str] = dataclasses.field(default_factory=set)
+    # method name mapped to list of positional param names
+    methods: dict[str, list[str]] = dataclasses.field(default_factory=dict)
+    properties: set[str] = dataclasses.field(default_factory=set)
+
+    @property
+    def all_names(self) -> set[str]:
+        return self.fields | set(self.methods) | self.properties
+
+
+def _is_property(node: ast.FunctionDef | ast.AsyncFunctionDef) -> bool:
+    for dec in node.decorator_list:
+        if isinstance(dec, ast.Name) and dec.id == "property":
+            return True
+        if isinstance(dec, ast.Attribute) and dec.attr == "property":
+            return True
+    return False

Review Comment:
   Do we need to consider `cached_property` too?



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Prek hook to detect drift in RuntimeTaskInstance and its protocol [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #67223:
URL: https://github.com/apache/airflow/pull/67223#discussion_r3494119356


##########
scripts/ci/prek/check_runtime_ti_protocol_sync.py:
##########
@@ -0,0 +1,165 @@
+#!/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",
+# ]
+# ///
+"""Check that RuntimeTaskInstanceProtocol stays in sync with RuntimeTaskInstance.
+
+Parses both the files via AST and checks for:
+  1. Public members declared in RuntimeTaskInstance that are absent from
+     RuntimeTaskInstanceProtocol aka incomplete protocol definition.
+  2. Methods present in both classes whose positional parameter names differ.
+"""
+
+from __future__ import annotations
+
+import ast
+import dataclasses
+import sys
+from pathlib import Path
+
+from common_prek_utils import AIRFLOW_TASK_SDK_SOURCES_PATH, console
+
+IMPL_PATH = AIRFLOW_TASK_SDK_SOURCES_PATH / "airflow" / "sdk" / "execution_time" / "task_runner.py"
+PROTOCOL_PATH = AIRFLOW_TASK_SDK_SOURCES_PATH / "airflow" / "sdk" / "types.py"
+
+IMPL_CLASS = "RuntimeTaskInstance"
+PROTOCOL_CLASS = "RuntimeTaskInstanceProtocol"
+
+# Members present in RuntimeTaskInstance but intentionally absent from the Protocol.
+# Each entry must carry a comment explaining why it is excluded.
+IMPLEMENTATION_ONLY: set[str] = {
+    # Worker internal: the DAG bundle used
+    "bundle_instance",
+    # Observability / Sentry integration string.
+    "sentry_integration",
+    # Internal helper for scheduler-side upstream map index resolution; not
+    # part of the public task execution interface.
+    "get_relevant_upstream_map_indexes",
+}
+
+
+@dataclasses.dataclass
+class ClassMembers:
+    """Public members declared directly in a class body."""
+
+    fields: set[str] = dataclasses.field(default_factory=set)
+    # method name mapped to list of positional param names
+    methods: dict[str, list[str]] = dataclasses.field(default_factory=dict)
+    properties: set[str] = dataclasses.field(default_factory=set)
+
+    @property
+    def all_names(self) -> set[str]:
+        return self.fields | set(self.methods) | self.properties
+
+
+def _is_property(node: ast.FunctionDef | ast.AsyncFunctionDef) -> bool:
+    for dec in node.decorator_list:
+        if isinstance(dec, ast.Name) and dec.id == "property":
+            return True
+        if isinstance(dec, ast.Attribute) and dec.attr == "property":
+            return True
+    return False
+
+
+def _is_public(name: str) -> bool:
+    return not name.startswith("_")
+
+
+def extract_class_members(source_path: Path, class_name: str) -> ClassMembers:
+    tree = ast.parse(source_path.read_text(), filename=str(source_path))
+    members = ClassMembers()
+
+    for node in ast.walk(tree):

Review Comment:
   From what I can tell, this loop intends to process only top-level members (the `break` on line 108). If so, this can avoid `ast.walk` and use `tree.body` directly instead.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Prek hook to detect drift in RuntimeTaskInstance and its protocol [airflow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #67223: Prek hook to detect drift in RuntimeTaskInstance and its protocol
URL: https://github.com/apache/airflow/pull/67223


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org