Skip to content

Commit 022ff83

Browse files
Reject non-numeric --limit values in airflow dags list-jobs (#72891)
The --limit option had no argparse type, so a value such as "abc" or "-1" was passed straight into the SQLAlchemy query and surfaced as a raw ValueError traceback instead of a usage error. Validating it at parse time gives the standard argparse message and exit code 2, while keeping 0 and positive integers working exactly as before. Co-authored-by: Eason09053360 <[email protected]>
1 parent 49674b7 commit 022ff83

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎airflow-core/src/airflow/cli/cli_config.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def string_lower_type(val):
294294

295295
# list_jobs
296296
ARG_DAG_ID_OPT = Arg(("-d", "--dag-id"), help="The id of the dag")
297-
ARG_LIMIT = Arg(("--limit",), help="Return a limited number of records")
297+
ARG_LIMIT = Arg(("--limit",), type=positive_int(allow_zero=True), help="Return a limited number of records")
298298
job_states = tuple(state.value for state in JobState)
299299
ARG_JOB_STATE = Arg(
300300
("--state",),

‎airflow-core/tests/unit/cli/test_cli_parser.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,15 @@ def test_pools_set_and_delete_reject_output_flag(self, argv):
635635
assert e.value.code == 2
636636
assert "unrecognized arguments: --output json" in stderr.getvalue()
637637

638+
@pytest.mark.parametrize("bad_value", ["abc", "-1"])
639+
def test_dags_list_jobs_rejects_invalid_limit(self, bad_value):
640+
with contextlib.redirect_stderr(StringIO()) as stderr:
641+
parser = cli_parser.get_parser()
642+
with pytest.raises(SystemExit) as e:
643+
parser.parse_args(["dags", "list-jobs", "--limit", bad_value])
644+
assert e.value.code == 2
645+
assert f"argument --limit: invalid positive int value: '{bad_value}'" in stderr.getvalue()
646+
638647
@pytest.mark.parametrize(
639648
"action_cmd",
640649
[

0 commit comments

Comments
 (0)