feat: support operator_aliases for short operator names in YAML configs#753
feat: support operator_aliases for short operator names in YAML configs#753Aaditya-git wants to merge 2 commits into
Conversation
Allows users to define short aliases (e.g. BashOperator) in defaults.yml or the dag YAML default section instead of writing full dotted import paths. Full paths continue to work unchanged for backward compatibility. Closes astronomer#58
| operator_aliases: | ||
| - operator_alias: BashOperator | ||
| operator_path: airflow.operators.bash.BashOperator | ||
| - operator_alias: PythonOperator | ||
| operator_path: airflow.operators.python.PythonOperator |
There was a problem hiding this comment.
Could we update this to the current Airflow 3 standard-provider imports, e.g. airflow.providers.standard.operators.bash.BashOperator and airflow.providers.standard.operators.python.PythonOperator, or split the example by Airflow version?
| td.make_task(operator, task_params) | ||
|
|
||
|
|
||
| def test_resolve_operator_alias_returns_full_path(): |
There was a problem hiding this comment.
Please add a DAGFactory-level test for aliases propagated from defaults.yml / global defaults. The current tests inject aliases directly into DagBuilder’s default_config, so they do not test the new propagation code in dagfactory/dagfactory.py.
There was a problem hiding this comment.
It could be something along the lines:
config = {
"test_dag": {
"tasks": [
{
"task_id": "task_1",
"operator": "BashOperator",
"bash_command": "echo 1",
}
]
}
}
td = _DagFactory(config_dict=config)
And confirming the expected operator was instantiated within td.build_dags()
There was a problem hiding this comment.
Acknowledged, and will start working on this.
There was a problem hiding this comment.
The existing alias tests in test_dagbuilder.py inject operator_aliases directly into DagBuilder's default_config, which bypasses the propagation code in dagfactory.py. I missed that the two test files cover different layers. test_dagbuilder.py tests DagBuilder in isolation, while the DAGFactory-level behavior of reading aliases from defaults.yml and copying them into default_config needed a separate test in test_dagfactory.py. Added that now along with the Airflow 3 import path fix.
…dd DAGFactory-level alias propagation test
pankajastro
left a comment
There was a problem hiding this comment.
Could you please document this feature?
| operator_aliases: | ||
| - operator_alias: BashOperator | ||
| operator_path: airflow.providers.standard.operators.bash.BashOperator | ||
| - operator_alias: PythonOperator | ||
| operator_path: airflow.providers.standard.operators.python.PythonOperator |
There was a problem hiding this comment.
I think we add example covering both Airflow 2 and Airflow 3 you can find example https://github.com/astronomer/dag-factory/tree/main/dev/dags
There was a problem hiding this comment.
Thanks @pankajastro. Quick clarification on covering both Airflow 2 and Airflow 3 here: operator_aliases is an alias-to-path map, so a single alias name like BashOperator can only point to one operator_path. That means I can't bind the same alias to both airflow.operators.bash.BashOperator (AF2) and airflow.providers.standard.operators.bash.BashOperator (AF3) in one list.
Which approach would you prefer?
- Keep the AF3 path active and show the AF2 path as an inline comment, or
- Use distinct alias names (e.g. BashOperator for AF3, BashOperatorLegacy for AF2), or
- Something else you had in mind?
Happy to go either way once I know your preference.
There was a problem hiding this comment.
I mean we should add example yml in both Airflow3 and Airflow2 that would in CI as integration test
Summary
Closes #58
Adds support for
operator_aliasesindefaults.yml(or thedefault:section of a dag YAML), so users can write short operator names instead of full dotted import paths.Before:
After:
Full dotted paths continue to work unchanged for backward compatibility.
Changes
dagfactory/dagbuilder.py: added_resolve_operator_alias()static method and call it beforemake_task()inbuild()dagfactory/dagfactory.py: propagateoperator_aliasesfromdefaults.ymlintodefault_configsoDagBuildercan access itdev/dags/defaults.yml: added exampleoperator_aliasesentriestests/test_dagbuilder.py: 5 new tests covering alias resolution, passthrough for full paths, unknown aliases, and end-to-end DAG build with aliasTest plan
test_resolve_operator_alias_returns_full_path: alias resolves to the correct full pathtest_resolve_operator_alias_passthrough_full_path: existing full paths pass through unchangedtest_resolve_operator_alias_unknown_alias_passthrough: unknown alias returns as-is (no error)test_resolve_operator_alias_empty_aliases: empty alias list is safetest_build_dag_with_operator_alias: end-to-end, builds a DAG using a short alias name