Skip to content

Commit 553fb1f

Browse files
rename skip_exit_code to skip_on_exit_code and allow providing multiple codes (#30692)
* rename skip_exit_code to skip_on_exit_code and allow providing multiple codes * replace list type by Container
1 parent 7e1dace commit 553fb1f

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

airflow/operators/python.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import types
2727
import warnings
2828
from abc import ABCMeta, abstractmethod
29+
from collections.abc import Container
2930
from pathlib import Path
3031
from tempfile import TemporaryDirectory
3132
from textwrap import dedent
@@ -471,7 +472,7 @@ class PythonVirtualenvOperator(_BasePythonVirtualenvOperator):
471472
:param expect_airflow: expect Airflow to be installed in the target environment. If true, the operator
472473
will raise warning if Airflow is not installed, and it will attempt to load Airflow
473474
macros when starting.
474-
:param skip_exit_code: If python_callable exits with this exit code, leave the task
475+
:param skip_on_exit_code: If python_callable exits with this exit code, leave the task
475476
in ``skipped`` state (default: None). If set to ``None``, any non-zero
476477
exit code will be treated as a failure.
477478
"""
@@ -494,7 +495,7 @@ def __init__(
494495
templates_dict: dict | None = None,
495496
templates_exts: list[str] | None = None,
496497
expect_airflow: bool = True,
497-
skip_exit_code: int | None = None,
498+
skip_on_exit_code: int | Container[int] | None = None,
498499
**kwargs,
499500
):
500501
if (
@@ -518,7 +519,13 @@ def __init__(
518519
self.python_version = python_version
519520
self.system_site_packages = system_site_packages
520521
self.pip_install_options = pip_install_options
521-
self.skip_exit_code = skip_exit_code
522+
self.skip_on_exit_code = (
523+
skip_on_exit_code
524+
if isinstance(skip_on_exit_code, Container)
525+
else [skip_on_exit_code]
526+
if skip_on_exit_code
527+
else []
528+
)
522529
super().__init__(
523530
python_callable=python_callable,
524531
use_dill=use_dill,
@@ -557,8 +564,8 @@ def execute_callable(self):
557564
try:
558565
result = self._execute_python_callable_in_subprocess(python_path, tmp_path)
559566
except subprocess.CalledProcessError as e:
560-
if self.skip_exit_code and e.returncode == self.skip_exit_code:
561-
raise AirflowSkipException(f"Process exited with code {self.skip_exit_code}. Skipping.")
567+
if e.returncode in self.skip_on_exit_code:
568+
raise AirflowSkipException(f"Process exited with code {e.returncode}. Skipping.")
562569
else:
563570
raise
564571
return result

tests/operators/test_python.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -938,12 +938,15 @@ def test_virtualenv_serializable_context_fields(self, create_task_instance):
938938
"extra_kwargs, actual_exit_code, expected_state",
939939
[
940940
(None, 99, TaskInstanceState.FAILED),
941-
({"skip_exit_code": 100}, 100, TaskInstanceState.SKIPPED),
942-
({"skip_exit_code": 100}, 101, TaskInstanceState.FAILED),
943-
({"skip_exit_code": None}, 0, TaskInstanceState.SUCCESS),
941+
({"skip_on_exit_code": 100}, 100, TaskInstanceState.SKIPPED),
942+
({"skip_on_exit_code": [100]}, 100, TaskInstanceState.SKIPPED),
943+
({"skip_on_exit_code": (100, 101)}, 100, TaskInstanceState.SKIPPED),
944+
({"skip_on_exit_code": 100}, 101, TaskInstanceState.FAILED),
945+
({"skip_on_exit_code": [100, 102]}, 101, TaskInstanceState.FAILED),
946+
({"skip_on_exit_code": None}, 0, TaskInstanceState.SUCCESS),
944947
],
945948
)
946-
def test_skip_exit_code(self, extra_kwargs, actual_exit_code, expected_state):
949+
def test_on_skip_exit_code(self, extra_kwargs, actual_exit_code, expected_state):
947950
def f(exit_code):
948951
if exit_code != 0:
949952
raise SystemExit(exit_code)

0 commit comments

Comments
 (0)