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",
+}
+
+
[email protected]
+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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to