Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update how PythonSensor returns values from python_callable #28932

Merged
merged 5 commits into from
Jan 19, 2023
Merged

Update how PythonSensor returns values from python_callable #28932

merged 5 commits into from
Jan 19, 2023

Conversation

SoxMax
Copy link
Contributor

@SoxMax SoxMax commented Jan 13, 2023

Have PythonSensor check if python_callable has returned a PokeReturnValue before coercing to boolean

^ Add meaningful description above

Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
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.

@boring-cyborg boring-cyborg bot added the area:core-operators Operators, Sensors and hooks within Core Airflow label Jan 13, 2023
@boring-cyborg
Copy link

boring-cyborg bot commented Jan 13, 2023

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst)
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our pre-commits will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

return PokeReturnValue(bool(return_value))
return return_value if isinstance(return_value, PokeReturnValue) else PokeReturnValue(bool(return_value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need add tests for this scenario for avoid regression

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test to check that the xcom_value in the return from poke() is populated.

Copy link
Contributor

@Taragolis Taragolis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to resolve static checks, please follow this Guide

@@ -41,6 +42,10 @@ def test_python_sensor_raise(self):
with pytest.raises(ZeroDivisionError):
self.run_as_task(lambda: 1 / 0)

def test_python_sensor_xcom(self):
poke_result = self.run_as_operator(fn=lambda: PokeReturnValue(True, "xcom")).poke({})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.run_as_operator test class method use for execute operator and return task for further validations, see usage in:

When you call poke on already created and executed task you actually call it again without provide actual context I would not recommend to do this because it could lead to potential issues in the future if something changes.

Otherwise I suggest two options (It is not limited by this options):

  1. Run as a task and check resulting XCom value
  2. Create task without helpers and manually call poke method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opted to run the poke method without running the operator first. I wasn't sure how to properly get the context to check the xcom after the task is run.

airflow/sensors/python.py Outdated Show resolved Hide resolved
@eladkal eladkal added this to the Airflow 2.5.2 milestone Jan 16, 2023
@Taragolis Taragolis merged commit b0f302e into apache:main Jan 19, 2023
@boring-cyborg
Copy link

boring-cyborg bot commented Jan 19, 2023

Awesome work, congrats on your first merged pull request!

@SoxMax SoxMax deleted the patch-1 branch January 19, 2023 01:12
maggesssss pushed a commit to maggesssss/airflow that referenced this pull request Jan 21, 2023
…8932)

* Update how PythonSensor returns values from python_callable

* test poke returns the xcom value

* update test to only run poke

* reformat based on changes

* use full if rather than ternary
@pierrejeambrun pierrejeambrun added the type:misc/internal Changelog: Misc changes that should appear in change log label Feb 27, 2023
pierrejeambrun pushed a commit that referenced this pull request Mar 6, 2023
* Update how PythonSensor returns values from python_callable

* test poke returns the xcom value

* update test to only run poke

* reformat based on changes

* use full if rather than ternary

(cherry picked from commit b0f302e)
pierrejeambrun pushed a commit that referenced this pull request Mar 7, 2023
* Update how PythonSensor returns values from python_callable

* test poke returns the xcom value

* update test to only run poke

* reformat based on changes

* use full if rather than ternary

(cherry picked from commit b0f302e)
pierrejeambrun pushed a commit that referenced this pull request Mar 8, 2023
* Update how PythonSensor returns values from python_callable

* test poke returns the xcom value

* update test to only run poke

* reformat based on changes

* use full if rather than ternary

(cherry picked from commit b0f302e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:core-operators Operators, Sensors and hooks within Core Airflow type:misc/internal Changelog: Misc changes that should appear in change log
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants