You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "uranusjr (via GitHub)" <gi...@apache.org> on 2025/12/26 22:25:38 UTC

[PR] Try to set priority_weight unconditionally [airflow]

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

   Attempt to fix recent CI issues…


-- 
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] Try to set priority_weight unconditionally [airflow]

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

   See if including my commit fixes the build


-- 
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] Try to set priority_weight unconditionally [airflow]

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


##########
airflow-core/tests/unit/core/test_core.py:
##########
@@ -22,8 +22,8 @@
 import pytest
 
 from airflow._shared.timezones.timezone import datetime
-from airflow.models.baseoperator import BaseOperator
 from airflow.providers.standard.operators.empty import EmptyOperator
+from airflow.sdk import BaseOperator

Review Comment:
   I do not think  we want to refer to "task.sdk" in "airfl - butow-core" package. I am not sure what is a better way - but once we complete task isolation, airflow-core should not import task-sdk (I think)



-- 
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] Try to set priority_weight unconditionally [airflow]

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


##########
devel-common/src/tests_common/test_utils/taskinstance.py:
##########
@@ -0,0 +1,154 @@
+#
+# 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 copy
+from typing import TYPE_CHECKING
+
+from airflow.models.taskinstance import TaskInstance
+
+from tests_common.test_utils.compat import SerializedBaseOperator, SerializedMappedOperator
+from tests_common.test_utils.dag import create_scheduler_dag
+from tests_common.test_utils.version_compat import AIRFLOW_V_3_0_PLUS, AIRFLOW_V_3_2_PLUS
+
+try:
+    from airflow.serialization.serialized_objects import create_scheduler_operator
+except ImportError:
+    create_scheduler_operator = lambda t: t
+
+if TYPE_CHECKING:
+    from uuid import UUID
+
+    from jinja2 import Environment
+
+    from airflow.sdk import Context
+    from airflow.sdk.types import Operator as SdkOperator
+    from airflow.serialization.definitions.mappedoperator import Operator as SerializedOperator
+
+__all__ = ["TaskInstanceWrapper", "create_task_instance", "render_template_fields", "run_task_instance"]
+
+
+class TaskInstanceWrapper:
+    """Compat wrapper for TaskInstance to support ``run()``."""
+
+    def __init__(self, ti: TaskInstance, task: SdkOperator) -> None:
+        self.__dict__.update(__ti=ti, __task=task)
+
+    def __delattr__(self, name):
+        delattr(self.__dict__["__ti"], name)
+
+    def __setattr__(self, name, value):
+        setattr(self.__dict__["__ti"], name, value)
+
+    def __getattr__(self, name):
+        return getattr(self.__dict__["__ti"], name)
+
+    def __copy__(self):
+        return TaskInstanceWrapper(copy.copy(self.__dict__["__ti"]), copy.copy(self.__dict__["__task"]))
+
+    def run(self, **kwargs) -> None:
+        from tests_common.test_utils.taskinstance import run_task_instance
+
+        run_task_instance(self.__dict__["__ti"], self.__dict__["__task"], **kwargs)
+
+    def render_templates(self, **kwargs) -> SdkOperator:
+        from tests_common.test_utils.taskinstance import render_template_fields
+
+        return render_template_fields(self.__dict__["__ti"], self.__dict__["__task"], **kwargs)
+
+
+def create_task_instance(
+    task: SdkOperator | SerializedOperator,
+    *,
+    dag_version_id: UUID,
+    run_id: str | None = None,
+    state: str | None = None,
+    map_index: int = -1,
+    ti_type: type[TaskInstance] = TaskInstance,
+) -> TaskInstance:
+    if isinstance(task, (SerializedBaseOperator, SerializedMappedOperator)):
+        serialized_task = task
+    elif sdk_dag := task.get_dag():
+        serialized_task = create_scheduler_dag(sdk_dag).get_task(task.task_id)
+    else:
+        serialized_task = create_scheduler_operator(task)
+    if AIRFLOW_V_3_0_PLUS:
+        return ti_type(
+            serialized_task,
+            dag_version_id=dag_version_id,
+            run_id=run_id,
+            state=state,
+            map_index=map_index,
+        )
+    return ti_type(  # type: ignore[call-arg]
+        serialized_task,
+        run_id=run_id,
+        state=state,
+        map_index=map_index,
+    )
+
+
+def run_task_instance(
+    ti: TaskInstance,
+    task: SdkOperator,
+    *,
+    ignore_depends_on_past: bool = False,
+    ignore_task_deps: bool = False,
+    ignore_ti_state: bool = False,
+    mark_success: bool = False,
+    session=None,
+):
+    if not AIRFLOW_V_3_2_PLUS:
+        ti.refresh_from_task(task)  # type: ignore[arg-type]
+        ti.run()
+        return ti
+
+    kwargs = {"session": session} if session else {}
+    if not ti.check_and_change_state_before_execution(
+        ignore_depends_on_past=ignore_depends_on_past,
+        ignore_task_deps=ignore_task_deps,
+        ignore_ti_state=ignore_ti_state,
+        mark_success=mark_success,
+        **kwargs,
+    ):

Review Comment:
   `check_and_change_state_before_execution` is also just a test artifact -- I don't think that path is hit in Prod



##########
devel-common/src/tests_common/test_utils/taskinstance.py:
##########
@@ -0,0 +1,154 @@
+#
+# 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 copy
+from typing import TYPE_CHECKING
+
+from airflow.models.taskinstance import TaskInstance
+
+from tests_common.test_utils.compat import SerializedBaseOperator, SerializedMappedOperator
+from tests_common.test_utils.dag import create_scheduler_dag
+from tests_common.test_utils.version_compat import AIRFLOW_V_3_0_PLUS, AIRFLOW_V_3_2_PLUS
+
+try:
+    from airflow.serialization.serialized_objects import create_scheduler_operator
+except ImportError:
+    create_scheduler_operator = lambda t: t
+
+if TYPE_CHECKING:
+    from uuid import UUID
+
+    from jinja2 import Environment
+
+    from airflow.sdk import Context
+    from airflow.sdk.types import Operator as SdkOperator
+    from airflow.serialization.definitions.mappedoperator import Operator as SerializedOperator
+
+__all__ = ["TaskInstanceWrapper", "create_task_instance", "render_template_fields", "run_task_instance"]
+
+
+class TaskInstanceWrapper:
+    """Compat wrapper for TaskInstance to support ``run()``."""
+
+    def __init__(self, ti: TaskInstance, task: SdkOperator) -> None:
+        self.__dict__.update(__ti=ti, __task=task)
+
+    def __delattr__(self, name):
+        delattr(self.__dict__["__ti"], name)
+
+    def __setattr__(self, name, value):
+        setattr(self.__dict__["__ti"], name, value)
+
+    def __getattr__(self, name):
+        return getattr(self.__dict__["__ti"], name)
+
+    def __copy__(self):
+        return TaskInstanceWrapper(copy.copy(self.__dict__["__ti"]), copy.copy(self.__dict__["__task"]))
+
+    def run(self, **kwargs) -> None:
+        from tests_common.test_utils.taskinstance import run_task_instance
+
+        run_task_instance(self.__dict__["__ti"], self.__dict__["__task"], **kwargs)
+
+    def render_templates(self, **kwargs) -> SdkOperator:
+        from tests_common.test_utils.taskinstance import render_template_fields
+
+        return render_template_fields(self.__dict__["__ti"], self.__dict__["__task"], **kwargs)
+
+
+def create_task_instance(
+    task: SdkOperator | SerializedOperator,
+    *,
+    dag_version_id: UUID,
+    run_id: str | None = None,
+    state: str | None = None,
+    map_index: int = -1,
+    ti_type: type[TaskInstance] = TaskInstance,
+) -> TaskInstance:
+    if isinstance(task, (SerializedBaseOperator, SerializedMappedOperator)):
+        serialized_task = task
+    elif sdk_dag := task.get_dag():
+        serialized_task = create_scheduler_dag(sdk_dag).get_task(task.task_id)
+    else:
+        serialized_task = create_scheduler_operator(task)
+    if AIRFLOW_V_3_0_PLUS:
+        return ti_type(
+            serialized_task,
+            dag_version_id=dag_version_id,
+            run_id=run_id,
+            state=state,
+            map_index=map_index,
+        )
+    return ti_type(  # type: ignore[call-arg]
+        serialized_task,
+        run_id=run_id,
+        state=state,
+        map_index=map_index,
+    )
+
+
+def run_task_instance(
+    ti: TaskInstance,
+    task: SdkOperator,
+    *,
+    ignore_depends_on_past: bool = False,
+    ignore_task_deps: bool = False,
+    ignore_ti_state: bool = False,
+    mark_success: bool = False,
+    session=None,
+):
+    if not AIRFLOW_V_3_2_PLUS:
+        ti.refresh_from_task(task)  # type: ignore[arg-type]
+        ti.run()
+        return ti
+
+    kwargs = {"session": session} if session else {}
+    if not ti.check_and_change_state_before_execution(
+        ignore_depends_on_past=ignore_depends_on_past,
+        ignore_task_deps=ignore_task_deps,
+        ignore_ti_state=ignore_ti_state,
+        mark_success=mark_success,
+        **kwargs,
+    ):

Review Comment:
   `check_and_change_state_before_execution` is also just a test artifact -- I don't think that path is hit in Prod / scheduler code



-- 
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] Try to set priority_weight unconditionally [airflow]

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


##########
airflow-core/newsfragments/59780.significant.rst:
##########
@@ -0,0 +1,9 @@
+Methods removed from PriorityWeightStrategy and TaskInstance
+
+On (expreimental) class ``PriorityWeightStrategy``, functions ``serialize()``

Review Comment:
   ```suggestion
   On (experimental) class ``PriorityWeightStrategy``, functions ``serialize()``
   ```



-- 
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] Try to set priority_weight unconditionally [airflow]

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


##########
airflow-core/src/airflow/task/priority_strategy.py:
##########
@@ -41,46 +42,22 @@ class PriorityWeightStrategy(ABC):
     """
 
     @abstractmethod
-    def get_weight(self, ti: TaskInstance):
+    def get_weight(self, ti: TaskInstance) -> int:
         """Get the priority weight of a task."""
-        ...
-
-    @classmethod
-    def deserialize(cls, data: dict[str, Any]) -> PriorityWeightStrategy:
-        """
-        Deserialize a priority weight strategy from data.
-
-        This is called when a serialized DAG is deserialized. ``data`` will be whatever
-        was returned by ``serialize`` during DAG serialization. The default
-        implementation constructs the priority weight strategy without any arguments.
-        """
-        return cls(**data)
-
-    def serialize(self) -> dict[str, Any]:
-        """
-        Serialize the priority weight strategy for JSON encoding.
-
-        This is called during DAG serialization to store priority weight strategy information
-        in the database. This should return a JSON-serializable dict that will be fed into
-        ``deserialize`` when the DAG is deserialized. The default implementation returns
-        an empty dict.
-        """
-        return {}
+        raise NotImplementedError("must be implemented by a subclass")
 
     def __eq__(self, other: object) -> bool:
         """Equality comparison."""
-        if not isinstance(other, type(self)):
-            return False
-        return self.serialize() == other.serialize()
+        return isinstance(other, type(self))
 
-    def __hash__(self):
-        return hash(self.serialize())
+    def __hash__(self) -> int:
+        return hash(None)

Review Comment:
   Naah that explains -- its fine as-is



-- 
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] Try to set priority_weight unconditionally [airflow]

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


##########
airflow-core/src/airflow/task/priority_strategy.py:
##########
@@ -41,46 +42,22 @@ class PriorityWeightStrategy(ABC):
     """
 
     @abstractmethod
-    def get_weight(self, ti: TaskInstance):
+    def get_weight(self, ti: TaskInstance) -> int:
         """Get the priority weight of a task."""
-        ...
-
-    @classmethod
-    def deserialize(cls, data: dict[str, Any]) -> PriorityWeightStrategy:
-        """
-        Deserialize a priority weight strategy from data.
-
-        This is called when a serialized DAG is deserialized. ``data`` will be whatever
-        was returned by ``serialize`` during DAG serialization. The default
-        implementation constructs the priority weight strategy without any arguments.
-        """
-        return cls(**data)
-
-    def serialize(self) -> dict[str, Any]:
-        """
-        Serialize the priority weight strategy for JSON encoding.
-
-        This is called during DAG serialization to store priority weight strategy information
-        in the database. This should return a JSON-serializable dict that will be fed into
-        ``deserialize`` when the DAG is deserialized. The default implementation returns
-        an empty dict.
-        """
-        return {}
+        raise NotImplementedError("must be implemented by a subclass")
 
     def __eq__(self, other: object) -> bool:
         """Equality comparison."""
-        if not isinstance(other, type(self)):
-            return False
-        return self.serialize() == other.serialize()
+        return isinstance(other, type(self))
 
-    def __hash__(self):
-        return hash(self.serialize())
+    def __hash__(self) -> int:
+        return hash(None)

Review Comment:
   Is this intentional, if so worth adding a comment why!



-- 
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] TaskInstance unused method cleanup [airflow]

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


##########
airflow-core/tests/unit/core/test_core.py:
##########
@@ -22,8 +22,8 @@
 import pytest
 
 from airflow._shared.timezones.timezone import datetime
-from airflow.models.baseoperator import BaseOperator
 from airflow.providers.standard.operators.empty import EmptyOperator
+from airflow.sdk import BaseOperator

Review Comment:
   Actually - I have not seen this is a test. There is an easy way - move this to "devel-common"



-- 
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] Try to set priority_weight unconditionally [airflow]

Posted by "Dev-iL (via GitHub)" <gi...@apache.org>.
Dev-iL commented on code in PR #59835:
URL: https://github.com/apache/airflow/pull/59835#discussion_r2649537250


##########
airflow-core/newsfragments/59780.significant.rst:
##########
@@ -0,0 +1,4 @@
+Usused methods removed from (experimental) PriorityWeightStrategy
+
+Functions ``serialize`` and ``deserialize`` were never used anywhere, and have
+been removed from the class. They should not be relied in in user code.

Review Comment:
   nit:
   ```suggestion
   been removed from the class. They should not be relied on in user code.
   ```



-- 
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] Try to set priority_weight unconditionally [airflow]

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


##########
devel-common/src/tests_common/test_utils/taskinstance.py:
##########
@@ -0,0 +1,154 @@
+#
+# 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 copy
+from typing import TYPE_CHECKING
+
+from airflow.models.taskinstance import TaskInstance
+
+from tests_common.test_utils.compat import SerializedBaseOperator, SerializedMappedOperator
+from tests_common.test_utils.dag import create_scheduler_dag
+from tests_common.test_utils.version_compat import AIRFLOW_V_3_0_PLUS, AIRFLOW_V_3_2_PLUS
+
+try:
+    from airflow.serialization.serialized_objects import create_scheduler_operator
+except ImportError:
+    create_scheduler_operator = lambda t: t
+
+if TYPE_CHECKING:
+    from uuid import UUID
+
+    from jinja2 import Environment
+
+    from airflow.sdk import Context
+    from airflow.sdk.types import Operator as SdkOperator
+    from airflow.serialization.definitions.mappedoperator import Operator as SerializedOperator
+
+__all__ = ["TaskInstanceWrapper", "create_task_instance", "render_template_fields", "run_task_instance"]
+
+
+class TaskInstanceWrapper:
+    """Compat wrapper for TaskInstance to support ``run()``."""
+
+    def __init__(self, ti: TaskInstance, task: SdkOperator) -> None:
+        self.__dict__.update(__ti=ti, __task=task)
+
+    def __delattr__(self, name):
+        delattr(self.__dict__["__ti"], name)
+
+    def __setattr__(self, name, value):
+        setattr(self.__dict__["__ti"], name, value)
+
+    def __getattr__(self, name):
+        return getattr(self.__dict__["__ti"], name)
+
+    def __copy__(self):
+        return TaskInstanceWrapper(copy.copy(self.__dict__["__ti"]), copy.copy(self.__dict__["__task"]))
+
+    def run(self, **kwargs) -> None:
+        from tests_common.test_utils.taskinstance import run_task_instance
+
+        run_task_instance(self.__dict__["__ti"], self.__dict__["__task"], **kwargs)
+
+    def render_templates(self, **kwargs) -> SdkOperator:
+        from tests_common.test_utils.taskinstance import render_template_fields
+
+        return render_template_fields(self.__dict__["__ti"], self.__dict__["__task"], **kwargs)
+
+
+def create_task_instance(
+    task: SdkOperator | SerializedOperator,
+    *,
+    dag_version_id: UUID,
+    run_id: str | None = None,
+    state: str | None = None,
+    map_index: int = -1,
+    ti_type: type[TaskInstance] = TaskInstance,
+) -> TaskInstance:
+    if isinstance(task, (SerializedBaseOperator, SerializedMappedOperator)):
+        serialized_task = task
+    elif sdk_dag := task.get_dag():
+        serialized_task = create_scheduler_dag(sdk_dag).get_task(task.task_id)
+    else:
+        serialized_task = create_scheduler_operator(task)
+    if AIRFLOW_V_3_0_PLUS:
+        return ti_type(
+            serialized_task,
+            dag_version_id=dag_version_id,
+            run_id=run_id,
+            state=state,
+            map_index=map_index,
+        )
+    return ti_type(  # type: ignore[call-arg]
+        serialized_task,
+        run_id=run_id,
+        state=state,
+        map_index=map_index,
+    )
+
+
+def run_task_instance(
+    ti: TaskInstance,
+    task: SdkOperator,
+    *,
+    ignore_depends_on_past: bool = False,
+    ignore_task_deps: bool = False,
+    ignore_ti_state: bool = False,
+    mark_success: bool = False,
+    session=None,
+):
+    if not AIRFLOW_V_3_2_PLUS:
+        ti.refresh_from_task(task)  # type: ignore[arg-type]
+        ti.run()
+        return ti
+
+    kwargs = {"session": session} if session else {}
+    if not ti.check_and_change_state_before_execution(
+        ignore_depends_on_past=ignore_depends_on_past,
+        ignore_task_deps=ignore_task_deps,
+        ignore_ti_state=ignore_ti_state,
+        mark_success=mark_success,
+        **kwargs,
+    ):

Review Comment:
   Yeah it’s in the removal pipeline 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] Try to set priority_weight unconditionally [airflow]

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

   I think some e2e (related) tests fail with this change:
   
   <img width="1422" height="185" alt="Screenshot 2025-12-29 at 22 55 26" src="https://github.com/user-attachments/assets/e1eb8c97-9889-4760-ac99-1f4019137ffe" />
   


-- 
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] Try to set priority_weight unconditionally [airflow]

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


##########
devel-common/src/tests_common/test_utils/taskinstance.py:
##########
@@ -0,0 +1,154 @@
+#
+# 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 copy
+from typing import TYPE_CHECKING
+
+from airflow.models.taskinstance import TaskInstance
+
+from tests_common.test_utils.compat import SerializedBaseOperator, SerializedMappedOperator
+from tests_common.test_utils.dag import create_scheduler_dag
+from tests_common.test_utils.version_compat import AIRFLOW_V_3_0_PLUS, AIRFLOW_V_3_2_PLUS
+
+try:
+    from airflow.serialization.serialized_objects import create_scheduler_operator
+except ImportError:
+    create_scheduler_operator = lambda t: t
+
+if TYPE_CHECKING:
+    from uuid import UUID
+
+    from jinja2 import Environment
+
+    from airflow.sdk import Context
+    from airflow.sdk.types import Operator as SdkOperator
+    from airflow.serialization.definitions.mappedoperator import Operator as SerializedOperator
+
+__all__ = ["TaskInstanceWrapper", "create_task_instance", "render_template_fields", "run_task_instance"]
+
+
+class TaskInstanceWrapper:
+    """Compat wrapper for TaskInstance to support ``run()``."""
+
+    def __init__(self, ti: TaskInstance, task: SdkOperator) -> None:
+        self.__dict__.update(__ti=ti, __task=task)
+
+    def __delattr__(self, name):
+        delattr(self.__dict__["__ti"], name)
+
+    def __setattr__(self, name, value):
+        setattr(self.__dict__["__ti"], name, value)
+
+    def __getattr__(self, name):
+        return getattr(self.__dict__["__ti"], name)
+
+    def __copy__(self):
+        return TaskInstanceWrapper(copy.copy(self.__dict__["__ti"]), copy.copy(self.__dict__["__task"]))
+
+    def run(self, **kwargs) -> None:
+        from tests_common.test_utils.taskinstance import run_task_instance
+
+        run_task_instance(self.__dict__["__ti"], self.__dict__["__task"], **kwargs)
+
+    def render_templates(self, **kwargs) -> SdkOperator:
+        from tests_common.test_utils.taskinstance import render_template_fields
+
+        return render_template_fields(self.__dict__["__ti"], self.__dict__["__task"], **kwargs)
+
+
+def create_task_instance(
+    task: SdkOperator | SerializedOperator,
+    *,
+    dag_version_id: UUID,
+    run_id: str | None = None,
+    state: str | None = None,
+    map_index: int = -1,
+    ti_type: type[TaskInstance] = TaskInstance,
+) -> TaskInstance:
+    if isinstance(task, (SerializedBaseOperator, SerializedMappedOperator)):
+        serialized_task = task
+    elif sdk_dag := task.get_dag():
+        serialized_task = create_scheduler_dag(sdk_dag).get_task(task.task_id)
+    else:
+        serialized_task = create_scheduler_operator(task)
+    if AIRFLOW_V_3_0_PLUS:
+        return ti_type(
+            serialized_task,
+            dag_version_id=dag_version_id,
+            run_id=run_id,
+            state=state,
+            map_index=map_index,
+        )
+    return ti_type(  # type: ignore[call-arg]
+        serialized_task,
+        run_id=run_id,
+        state=state,
+        map_index=map_index,
+    )
+
+
+def run_task_instance(
+    ti: TaskInstance,
+    task: SdkOperator,
+    *,
+    ignore_depends_on_past: bool = False,
+    ignore_task_deps: bool = False,
+    ignore_ti_state: bool = False,
+    mark_success: bool = False,
+    session=None,
+):
+    if not AIRFLOW_V_3_2_PLUS:
+        ti.refresh_from_task(task)  # type: ignore[arg-type]
+        ti.run()
+        return ti
+
+    kwargs = {"session": session} if session else {}
+    if not ti.check_and_change_state_before_execution(
+        ignore_depends_on_past=ignore_depends_on_past,
+        ignore_task_deps=ignore_task_deps,
+        ignore_ti_state=ignore_ti_state,
+        mark_success=mark_success,
+        **kwargs,
+    ):
+        return ti
+
+    from airflow.sdk.definitions.dag import _run_task
+
+    taskrun_result = _run_task(ti=ti, task=task)
+    if not taskrun_result:
+        return None
+    if error := taskrun_result.error:
+        raise error from error

Review Comment:
   nit
   ```py
   if error := taskrun_result.error:
       raise error
   ```
   
   or if you want to suppress it
   ```py
   if error := taskrun_result.error:
       raise error from None
   ```



-- 
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] Try to set priority_weight unconditionally [airflow]

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

   #protm


-- 
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] Try to set priority_weight unconditionally [airflow]

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


##########
airflow-core/src/airflow/task/priority_strategy.py:
##########
@@ -41,46 +42,22 @@ class PriorityWeightStrategy(ABC):
     """
 
     @abstractmethod
-    def get_weight(self, ti: TaskInstance):
+    def get_weight(self, ti: TaskInstance) -> int:
         """Get the priority weight of a task."""
-        ...
-
-    @classmethod
-    def deserialize(cls, data: dict[str, Any]) -> PriorityWeightStrategy:
-        """
-        Deserialize a priority weight strategy from data.
-
-        This is called when a serialized DAG is deserialized. ``data`` will be whatever
-        was returned by ``serialize`` during DAG serialization. The default
-        implementation constructs the priority weight strategy without any arguments.
-        """
-        return cls(**data)
-
-    def serialize(self) -> dict[str, Any]:
-        """
-        Serialize the priority weight strategy for JSON encoding.
-
-        This is called during DAG serialization to store priority weight strategy information
-        in the database. This should return a JSON-serializable dict that will be fed into
-        ``deserialize`` when the DAG is deserialized. The default implementation returns
-        an empty dict.
-        """
-        return {}
+        raise NotImplementedError("must be implemented by a subclass")
 
     def __eq__(self, other: object) -> bool:
         """Equality comparison."""
-        if not isinstance(other, type(self)):
-            return False
-        return self.serialize() == other.serialize()
+        return isinstance(other, type(self))
 
-    def __hash__(self):
-        return hash(self.serialize())
+    def __hash__(self) -> int:
+        return hash(None)

Review Comment:
   These classes don’t have any attributes so there’s nothing to hash (every instance is equal to another of the same class). This is one of the most common ways to produce a whatever value in this case. The other would be `hash(())`; would that be easier to understand?
   
   Honestly if I’m to implement this from scratch I’d omit both `__eq__` and `__hash__` in the first place (they are not used anywhere), but since thye have been implemented, I figured it’s better to keep them.



-- 
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] TaskInstance unused method cleanup [airflow]

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


##########
airflow-core/tests/unit/core/test_core.py:
##########
@@ -22,8 +22,8 @@
 import pytest
 
 from airflow._shared.timezones.timezone import datetime
-from airflow.models.baseoperator import BaseOperator
 from airflow.providers.standard.operators.empty import EmptyOperator
+from airflow.sdk import BaseOperator

Review Comment:
   I don't think there's a good way no to avoid `airflow.sdk` in tests at the moment since you need to build dags to test against. Maybe someday we'll be able to improve this, especially when we allow writing dags in other languages.



-- 
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] TaskInstance unused method cleanup [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr merged PR #59835:
URL: https://github.com/apache/airflow/pull/59835


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