You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "kaxil (via GitHub)" <gi...@apache.org> on 2025/03/25 15:01:28 UTC
[PR] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
kaxil opened a new pull request, #48244:
URL: https://github.com/apache/airflow/pull/48244
closes https://github.com/apache/airflow/issues/45743
<!--
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.
-->
<!--
Thank you for contributing! 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 committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2017553496
##########
providers/databricks/tests/unit/databricks/sensors/test_databricks_partition.py:
##########
@@ -91,11 +91,6 @@ def test_poke(self, mock_poke, sensor_poke_result, expected_poke_result):
mock_poke.return_value = sensor_poke_result
assert self.partition_sensor.poke({}) == expected_poke_result
- @pytest.mark.db_test
Review Comment:
Useless test
##########
providers/databricks/tests/unit/databricks/sensors/test_databricks_sql.py:
##########
@@ -77,11 +77,6 @@ def test_poke(self, mock_poke, sensor_poke_result, expected_poke_result):
mock_poke.return_value = sensor_poke_result
assert self.sensor.poke({}) == expected_poke_result
- @pytest.mark.db_test
Review Comment:
Useless test
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2017575183
##########
task-sdk/src/airflow/sdk/definitions/baseoperator.py:
##########
@@ -1555,6 +1555,46 @@ def render_template_fields(
jinja_env = self.get_template_env()
self._do_render_template_fields(self, self.template_fields, context, jinja_env, set())
+ def pre_execute(self, context: Any):
+ """Execute right before self.execute() is called."""
+ if self._pre_execute_hook is None:
+ return
+ from airflow.sdk.execution_time.callback_runner import create_executable_runner
+ from airflow.sdk.execution_time.context import context_get_outlet_events
+
+ create_executable_runner(
+ self._pre_execute_hook,
+ context_get_outlet_events(context),
+ logger=self.log,
+ ).run(context)
Review Comment:
Done
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil merged PR #48244:
URL: https://github.com/apache/airflow/pull/48244
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2018091814
##########
task-sdk/src/airflow/sdk/definitions/baseoperator.py:
##########
@@ -1549,6 +1549,26 @@ def render_template_fields(
jinja_env = self.get_template_env()
self._do_render_template_fields(self, self.template_fields, context, jinja_env, set())
+ def pre_execute(self, context: Any):
+ """Execute right before self.execute() is called."""
Review Comment:
`pass` is moot since we have docstrings. All the following are same :)
```python
def x(): ...
def x():
pass
def x():
"""
```
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2015152572
##########
task-sdk/tests/task_sdk/execution_time/test_task_runner.py:
##########
@@ -974,7 +974,7 @@ def test_get_context_with_ti_context_from_server(self, create_runtime_ti, mock_s
"ti": runtime_ti,
"dag_run": dr,
"data_interval_end": timezone.datetime(2024, 12, 1, 1, 0, 0),
- "data_interval_start": timezone.datetime(2024, 12, 1, 0, 0, 0),
+ "data_interval_start": timezone.datetime(2024, 12, 1, 1, 0, 0),
Review Comment:
Hmm 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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2018092267
##########
task-sdk/src/airflow/sdk/definitions/baseoperator.py:
##########
@@ -1549,6 +1549,26 @@ def render_template_fields(
jinja_env = self.get_template_env()
self._do_render_template_fields(self, self.template_fields, context, jinja_env, set())
+ def pre_execute(self, context: Any):
+ """Execute right before self.execute() is called."""
+
+ def execute(self, context: Context) -> Any:
+ """
+ Derive when creating an operator.
+
+ The main method to execute the task. Context is the same dictionary used as when rendering jinja templates.
+
+ Refer to get_template_context for more context.
+ """
+ raise NotImplementedError()
+
+ def post_execute(self, context: Any, result: Any = None):
+ """
+ Execute right after self.execute() is called.
+
+ It is passed the execution context and any results returned by the operator.
+ """
Review Comment:
`pass` is moot since we have docstrings. All the following are same :)
```python
def x(): ...
def x():
pass
def x():
"""
```
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2015868489
##########
task-sdk/tests/task_sdk/execution_time/test_task_runner.py:
##########
@@ -974,7 +974,7 @@ def test_get_context_with_ti_context_from_server(self, create_runtime_ti, mock_s
"ti": runtime_ti,
"dag_run": dr,
"data_interval_end": timezone.datetime(2024, 12, 1, 1, 0, 0),
- "data_interval_start": timezone.datetime(2024, 12, 1, 0, 0, 0),
+ "data_interval_start": timezone.datetime(2024, 12, 1, 1, 0, 0),
Review Comment:
This is because of the changes to `create_runtime_ti` fixture. Previously it had hard-coded values.
https://github.com/apache/airflow/blob/965a1cd7f25867ce7430397d58249a0d05e77ebb/task-sdk/tests/conftest.py#L362-L363
I changed that to dynamically generate those values:
https://github.com/apache/airflow/blob/23f5210290579504a79296a95e383f7967953838/task-sdk/tests/conftest.py#L405-L411
This was part of my adding a new `run_task` fixture:
https://github.com/apache/airflow/blob/23f5210290579504a79296a95e383f7967953838/task-sdk/tests/conftest.py#L459-L475
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2018096147
##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -385,16 +385,14 @@ def get_first_reschedule_date(self, context: Context) -> datetime | None:
if TYPE_CHECKING:
assert isinstance(response, TaskRescheduleStartDate)
- start_date = response.start_date
- log.debug("First reschedule date from supervisor: %s", start_date)
-
- return start_date
+ return response.start_date
Review Comment:
The date can be `None` too so I have removed it. Might add it back in related changes but since CI is green I would like to merge it
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2012320349
##########
task-sdk/src/airflow/sdk/definitions/sensors/base.py:
##########
@@ -127,10 +114,6 @@ class BaseSensorOperator(BaseOperator):
_is_sensor: bool = True
- # Adds one additional dependency for all sensor operators that checks if a
- # sensor task instance can be rescheduled.
- deps = BaseOperator.deps | {ReadyToRescheduleDep()}
Review Comment:
This is only needed on the Scheduler side and is handled below:
https://github.com/apache/airflow/blob/0a846b6d274408c744117d3dda7a2367cb079fc1/airflow-core/src/airflow/serialization/serialized_objects.py#L1432-L1438
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2012312097
##########
airflow-core/src/airflow/sensors/README.md:
##########
Review Comment:
I don't think we need a README for this info!
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2017550088
##########
task-sdk/src/airflow/sdk/definitions/baseoperator.py:
##########
@@ -1555,6 +1555,46 @@ def render_template_fields(
jinja_env = self.get_template_env()
self._do_render_template_fields(self, self.template_fields, context, jinja_env, set())
+ def pre_execute(self, context: Any):
+ """Execute right before self.execute() is called."""
+ if self._pre_execute_hook is None:
+ return
+ from airflow.sdk.execution_time.callback_runner import create_executable_runner
+ from airflow.sdk.execution_time.context import context_get_outlet_events
+
+ create_executable_runner(
+ self._pre_execute_hook,
+ context_get_outlet_events(context),
+ logger=self.log,
+ ).run(context)
Review Comment:
Will remove
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2015131826
##########
task-sdk/src/airflow/sdk/definitions/baseoperator.py:
##########
@@ -1555,6 +1555,46 @@ def render_template_fields(
jinja_env = self.get_template_env()
self._do_render_template_fields(self, self.template_fields, context, jinja_env, set())
+ def pre_execute(self, context: Any):
+ """Execute right before self.execute() is called."""
+ if self._pre_execute_hook is None:
+ return
+ from airflow.sdk.execution_time.callback_runner import create_executable_runner
+ from airflow.sdk.execution_time.context import context_get_outlet_events
+
+ create_executable_runner(
+ self._pre_execute_hook,
+ context_get_outlet_events(context),
+ logger=self.log,
+ ).run(context)
Review Comment:
Need to remove the `create_executable_runner` calls in task_runner so this is not wrapped twice. (Or alternatively we could just remove the call here.)
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2012312998
##########
airflow-core/src/airflow/serialization/serialized_objects.py:
##########
@@ -726,7 +726,7 @@ def serialize(
return var.to_dict()
elif isinstance(var, MappedOperator):
return cls._encode(SerializedBaseOperator.serialize_mapped_operator(var), type_=DAT.OP)
- elif isinstance(var, BaseOperator):
+ elif isinstance(var, TaskSDKBaseOperator):
Review Comment:
Moved changes in this file to https://github.com/apache/airflow/pull/48241
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2017551207
##########
task-sdk/tests/conftest.py:
##########
@@ -261,155 +259,3 @@ def _make_context_dict(
return context.model_dump(exclude_unset=True, mode="json")
return _make_context_dict
-
-
-@pytest.fixture
Review Comment:
All of these isn't required since it is moved to `devel-common/src/tests_common/pytest_plugin.py`
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2018078217
##########
airflow-core/tests/unit/serialization/test_dag_serialization.py:
##########
@@ -750,6 +748,13 @@ def validate_deserialized_task(
else:
assert serialized_task.resources == task.resources
+ # `deps` are set in the Scheduler's BaseOperator as that is where we need to evaluate deps
+ # so only serialized tasks that are sensors should have the ReadyToRescheduleDep.
+ if task._is_sensor:
Review Comment:
Cool yes makes sense
##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -385,16 +385,14 @@ def get_first_reschedule_date(self, context: Context) -> datetime | None:
if TYPE_CHECKING:
assert isinstance(response, TaskRescheduleStartDate)
- start_date = response.start_date
- log.debug("First reschedule date from supervisor: %s", start_date)
-
- return start_date
+ return response.start_date
Review Comment:
Can we keep the log?
##########
task-sdk/src/airflow/sdk/definitions/baseoperator.py:
##########
@@ -1549,6 +1549,26 @@ def render_template_fields(
jinja_env = self.get_template_env()
self._do_render_template_fields(self, self.template_fields, context, jinja_env, set())
+ def pre_execute(self, context: Any):
+ """Execute right before self.execute() is called."""
Review Comment:
Add a pass to be explicit?
##########
providers/databricks/tests/unit/databricks/sensors/test_databricks_partition.py:
##########
@@ -91,11 +91,6 @@ def test_poke(self, mock_poke, sensor_poke_result, expected_poke_result):
mock_poke.return_value = sensor_poke_result
assert self.partition_sensor.poke({}) == expected_poke_result
- @pytest.mark.db_test
Review Comment:
Yeah!
##########
providers/http/tests/unit/http/sensors/test_http.py:
##########
@@ -317,8 +319,25 @@ def test_get_response_check(self):
)
op.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)
+ @pytest.mark.skipif(not AIRFLOW_V_3_0_PLUS, reason="Test only for Airflow 3.0+")
+ @mock.patch("airflow.providers.http.hooks.http.Session", FakeSession)
+ def test_sensor(self, run_task):
+ sensor = HttpSensor(
+ task_id="http_sensor_check",
+ http_conn_id="http_default",
+ endpoint="/search",
+ request_params={"client": "ubuntu", "q": "airflow", "date": "{{ds}}"},
+ headers={},
+ response_check=lambda response: f"apache/airflow/{DEFAULT_DATE:%Y-%m-%d}" in response.text,
+ poke_interval=5,
+ timeout=15,
+ dag=self.dag,
+ )
+ run_task(sensor)
Review Comment:
Should we assert something? I know we are testing if it works, so not sure
##########
task-sdk/src/airflow/sdk/definitions/baseoperator.py:
##########
@@ -1549,6 +1549,26 @@ def render_template_fields(
jinja_env = self.get_template_env()
self._do_render_template_fields(self, self.template_fields, context, jinja_env, set())
+ def pre_execute(self, context: Any):
+ """Execute right before self.execute() is called."""
+
+ def execute(self, context: Context) -> Any:
+ """
+ Derive when creating an operator.
+
+ The main method to execute the task. Context is the same dictionary used as when rendering jinja templates.
+
+ Refer to get_template_context for more context.
+ """
+ raise NotImplementedError()
+
+ def post_execute(self, context: Any, result: Any = None):
+ """
+ Execute right after self.execute() is called.
+
+ It is passed the execution context and any results returned by the operator.
+ """
Review Comment:
Add a pass to be explicit?
--
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] Move `BaseSensorOperator` to TaskSDK definitions [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #48244:
URL: https://github.com/apache/airflow/pull/48244#discussion_r2018090816
##########
providers/http/tests/unit/http/sensors/test_http.py:
##########
@@ -317,8 +319,25 @@ def test_get_response_check(self):
)
op.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)
+ @pytest.mark.skipif(not AIRFLOW_V_3_0_PLUS, reason="Test only for Airflow 3.0+")
+ @mock.patch("airflow.providers.http.hooks.http.Session", FakeSession)
+ def test_sensor(self, run_task):
+ sensor = HttpSensor(
+ task_id="http_sensor_check",
+ http_conn_id="http_default",
+ endpoint="/search",
+ request_params={"client": "ubuntu", "q": "airflow", "date": "{{ds}}"},
+ headers={},
+ response_check=lambda response: f"apache/airflow/{DEFAULT_DATE:%Y-%m-%d}" in response.text,
+ poke_interval=5,
+ timeout=15,
+ dag=self.dag,
+ )
+ run_task(sensor)
Review Comment:
Naah, just that there is no exception raised is enough -- also retain previous test behaviour
--
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