From 1c8c789c58c8856fccc7da502a83c884867a2b97 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Sat, 12 Jun 2021 22:36:52 +0800 Subject: [PATCH] Validate retries value on init for better errors --- airflow/models/baseoperator.py | 8 ++++++ tests/core/test_core.py | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/airflow/models/baseoperator.py b/airflow/models/baseoperator.py index 9328323e5e0ee..7af23d3ed26f2 100644 --- a/airflow/models/baseoperator.py +++ b/airflow/models/baseoperator.py @@ -559,6 +559,14 @@ def __init__( if wait_for_downstream: self.depends_on_past = True + if retries is not None and not isinstance(retries, int): + try: + parsed_retries = int(retries) + except (TypeError, ValueError): + raise AirflowException(f"'retries' type must be int, not {type(retries).__name__}") + self.log.warning("Implicitly converting 'retries' for %s from %r to int", self, retries) + retries = parsed_retries + self.retries = retries self.queue = queue self.pool = Pool.DEFAULT_POOL_NAME if pool is None else pool diff --git a/tests/core/test_core.py b/tests/core/test_core.py index b314272be2088..ee2df3c541767 100644 --- a/tests/core/test_core.py +++ b/tests/core/test_core.py @@ -16,6 +16,7 @@ # specific language governing permissions and limitations # under the License. +import logging import multiprocessing import os import signal @@ -453,3 +454,48 @@ def test_dag_params_and_task_params(self): assert context1['params'] == {'key_1': 'value_1', 'key_2': 'value_2_new', 'key_3': 'value_3'} assert context2['params'] == {'key_1': 'value_1', 'key_2': 'value_2_old'} + + +@pytest.fixture() +def dag(): + return DAG(TEST_DAG_ID, default_args={'owner': 'airflow', 'start_date': DEFAULT_DATE}) + + +def test_operator_retries_invalid(dag): + with pytest.raises(AirflowException) as ctx: + BashOperator( + task_id='test_illegal_args', + bash_command='echo success', + dag=dag, + retries='foo', + ) + assert str(ctx.value) == "'retries' type must be int, not str" + + +def test_operator_retries_coerce(caplog, dag): + with caplog.at_level(logging.WARNING): + BashOperator( + task_id='test_illegal_args', + bash_command='echo success', + dag=dag, + retries='1', + ) + assert caplog.record_tuples == [ + ( + "airflow.operators.bash.BashOperator", + logging.WARNING, + "Implicitly converting 'retries' for from '1' to int", + ), + ] + + +@pytest.mark.parametrize("retries", [None, 5]) +def test_operator_retries(caplog, dag, retries): + with caplog.at_level(logging.WARNING): + BashOperator( + task_id='test_illegal_args', + bash_command='echo success', + dag=dag, + retries=retries, + ) + assert caplog.records == []