Skip to content

Commit ad69407

Browse files
authored
Add validation for extra_args in test helper functions (#2723)
1 parent e1fe42e commit ad69407

4 files changed

Lines changed: 41 additions & 11 deletions

File tree

docs/cli-reference/model-customization.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,9 +2671,9 @@ Generate dataclasses with keyword-only fields (Python 3.10+).
26712671

26722672
The `--keyword-only` flag generates dataclasses where all fields must be
26732673
specified as keyword arguments (kw_only=True). This is only available for
2674-
Python 3.10+. When combined with `--frozen`, it creates immutable dataclasses
2675-
with keyword-only arguments, improving code clarity and preventing positional
2676-
argument errors.
2674+
Python 3.10+. When combined with `--frozen-dataclasses`, it creates immutable
2675+
dataclasses with keyword-only arguments, improving code clarity and preventing
2676+
positional argument errors.
26772677

26782678
**Related:** [`--frozen-dataclasses`](model-customization.md#frozen-dataclasses), [`--output-model-type`](model-customization.md#output-model-type), [`--target-python-version`](model-customization.md#target-python-version)
26792679

tests/main/conftest.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from datamodel_code_generator import DataModelType
2020
from datamodel_code_generator.__main__ import Exit, main
21+
from datamodel_code_generator.arguments import arg_parser
2122
from datamodel_code_generator.util import PYDANTIC_V2
2223
from tests.conftest import (
2324
AssertFileContent,
@@ -133,6 +134,34 @@ def _assert_exit_code(return_code: Exit, expected_exit: Exit, context: str) -> N
133134
pytest.fail(f"Expected exit code {expected_exit!r}, got {return_code!r}\n{context}")
134135

135136

137+
def _get_valid_cli_options() -> frozenset[str]:
138+
"""Get all valid CLI option names from arg_parser."""
139+
valid_options: set[str] = set()
140+
for action in arg_parser._actions:
141+
valid_options.update(action.option_strings)
142+
return frozenset(valid_options)
143+
144+
145+
_VALID_CLI_OPTIONS = _get_valid_cli_options()
146+
147+
148+
def _validate_extra_args(extra_args: Sequence[str] | None) -> None:
149+
"""Validate that all option-like arguments in extra_args are valid CLI options."""
150+
if extra_args is None:
151+
return
152+
invalid_args: list[str] = [
153+
arg
154+
for arg in extra_args
155+
if (
156+
(arg.startswith("--") and "=" not in arg)
157+
or (arg.startswith("-") and not arg.startswith("--") and len(arg) == 2)
158+
)
159+
and arg not in _VALID_CLI_OPTIONS
160+
]
161+
if invalid_args: # pragma: no cover
162+
pytest.fail(f"Invalid CLI options in extra_args: {invalid_args}. Valid options: {sorted(_VALID_CLI_OPTIONS)}")
163+
164+
136165
def _extend_args(
137166
args: list[str],
138167
*,
@@ -148,6 +177,7 @@ def _extend_args(
148177
args.extend(["--output", str(output_path)])
149178
if input_file_type is not None:
150179
args.extend(["--input-file-type", input_file_type])
180+
_validate_extra_args(extra_args)
151181
if extra_args is not None:
152182
args.extend(extra_args)
153183

tests/main/graphql/test_main_graphql.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,9 @@ def test_main_graphql_dataclass_frozen_keyword_only(output_file: Path) -> None:
582582
583583
The `--keyword-only` flag generates dataclasses where all fields must be
584584
specified as keyword arguments (kw_only=True). This is only available for
585-
Python 3.10+. When combined with `--frozen`, it creates immutable dataclasses
586-
with keyword-only arguments, improving code clarity and preventing positional
587-
argument errors.
585+
Python 3.10+. When combined with `--frozen-dataclasses`, it creates immutable
586+
dataclasses with keyword-only arguments, improving code clarity and preventing
587+
positional argument errors.
588588
"""
589589
run_main_and_assert(
590590
input_path=GRAPHQL_DATA_PATH / "simple-star-wars.graphql",
@@ -595,7 +595,7 @@ def test_main_graphql_dataclass_frozen_keyword_only(output_file: Path) -> None:
595595
extra_args=[
596596
"--output-model-type",
597597
"dataclasses.dataclass",
598-
"--frozen",
598+
"--frozen-dataclasses",
599599
"--keyword-only",
600600
"--target-python-version",
601601
"3.10",

tests/main/jsonschema/test_main_jsonschema.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def test_main_jsonschema_dataclass_arguments_with_pydantic(output_file: Path) ->
314314
@pytest.mark.cli_doc(
315315
options=["--keyword-only"],
316316
input_schema="jsonschema/person.json",
317-
cli_args=["--output-model-type", "dataclasses.dataclass", "--frozen", "--keyword-only"],
317+
cli_args=["--output-model-type", "dataclasses.dataclass", "--frozen-dataclasses", "--keyword-only"],
318318
golden_output="main/jsonschema/general_dataclass_frozen_kw_only.py",
319319
related_options=["--frozen-dataclasses", "--output-model-type"],
320320
)
@@ -323,7 +323,7 @@ def test_main_jsonschema_dataclass_frozen_keyword_only(output_file: Path) -> Non
323323
324324
The `--keyword-only` flag generates all dataclass fields as keyword-only,
325325
requiring explicit parameter names when instantiating models. Combined with
326-
`--frozen`, creates immutable models with keyword-only constructors.
326+
`--frozen-dataclasses`, creates immutable models with keyword-only constructors.
327327
"""
328328
run_main_and_assert(
329329
input_path=JSON_SCHEMA_DATA_PATH / "person.json",
@@ -334,7 +334,7 @@ def test_main_jsonschema_dataclass_frozen_keyword_only(output_file: Path) -> Non
334334
extra_args=[
335335
"--output-model-type",
336336
"dataclasses.dataclass",
337-
"--frozen",
337+
"--frozen-dataclasses",
338338
"--keyword-only",
339339
"--target-python-version",
340340
"3.10",
@@ -4005,7 +4005,7 @@ def test_main_jsonschema_reuse_scope_tree_dataclass_frozen(output_dir: Path) ->
40054005
"tree",
40064006
"--output-model-type",
40074007
"dataclasses.dataclass",
4008-
"--frozen",
4008+
"--frozen-dataclasses",
40094009
],
40104010
)
40114011

0 commit comments

Comments
 (0)