Skip to content

Commit

Permalink
Support Installed code via python callable string (#221)
Browse files Browse the repository at this point in the history
i want to be able to define a path to a custom function that i am
installing into my airflow environment. This will allow me to test my
dag locally without having to define a path and simply refer to the
package path instead.

Example

```yaml
        branch_by_bq_load_status:
            operator: airflow.operators.python.BranchPythonOperator
            python_callable: custom.path.to.func

```

---------
  • Loading branch information
john-drews authored Oct 16, 2024
1 parent e474a3a commit bf9272c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions dagfactory/dagbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ def make_task(operator: str, task_params: Dict[str, Any]) -> BaseOperator:
# Airflow 2.0 doesn't allow these to be passed to operator
del task_params["python_callable_name"]
del task_params["python_callable_file"]
elif isinstance(task_params["python_callable"], str):
task_params["python_callable"]: Callable = import_string(task_params["python_callable"])

# Check for the custom success and failure callables in SqlSensor. These are considered
# optional, so no failures in case they aren't found. Note: there's no reason to
Expand Down
13 changes: 12 additions & 1 deletion tests/test_dagbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,18 @@ def test_make_python_operator():
assert callable(actual.python_callable)
assert isinstance(actual, PythonOperator)


def test_make_python_operator_with_callable_str():
td = dagbuilder.DagBuilder("test_dag", DAG_CONFIG, DEFAULT_CONFIG)
operator = "airflow.operators.python_operator.PythonOperator"
task_params = {
"task_id": "test_task",
"python_callable": "builtins.print",
}
actual = td.make_task(operator, task_params)
assert actual.task_id == "test_task"
assert callable(actual.python_callable)
assert isinstance(actual, PythonOperator)

def test_make_python_operator_missing_param():
td = dagbuilder.DagBuilder("test_dag", DAG_CONFIG, DEFAULT_CONFIG)
operator = "airflow.operators.python_operator.PythonOperator"
Expand Down

0 comments on commit bf9272c

Please sign in to comment.