Skip to content

Commit

Permalink
Add --disable-warnings (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Jan 20, 2023
1 parent 933ca10 commit a019450
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ usage: datamodel-codegen [-h] [--input INPUT] [--url URL]
[--target-python-version {3.6,3.7,3.8,3.9,3.10,3.11}]
[--wrap-string-literal] [--validation]
[--use-double-quotes] [--encoding ENCODING] [--debug]
[--version]
[--disable-warnings] [--version]
options:
-h, --help show this help message and exit
Expand Down Expand Up @@ -434,6 +434,7 @@ options:
be used without this option.
--encoding ENCODING The encoding of input and output (default: cp1252)
--debug show debug message
--disable-warnings disable warnings
--version show version
```

Expand Down
20 changes: 14 additions & 6 deletions datamodel_code_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import locale
import signal
import sys
import warnings
from argparse import ArgumentParser, FileType, Namespace
from collections import defaultdict
from enum import IntEnum
Expand Down Expand Up @@ -388,6 +389,9 @@ def sig_int_handler(_: int, __: Any) -> None: # pragma: no cover
arg_parser.add_argument(
'--debug', help='show debug message', action='store_true', default=None
)
arg_parser.add_argument(
'--disable-warnings', help='disable warnings', action='store_true', default=None
)
arg_parser.add_argument('--version', help='show version', action='store_true')


Expand Down Expand Up @@ -475,21 +479,23 @@ def _validate_use_annotated(cls, values: Dict[str, Any]) -> Dict[str, Any]:
@classmethod
def _validate_use_union_operator(cls, values: Dict[str, Any]) -> Dict[str, Any]:
if values.get('use_union_operator'):
target_python_version: PythonVersion = values.get(
'target_python_version', PythonVersion.PY_37
target_python_version: PythonVersion = PythonVersion(
values.get('target_python_version', PythonVersion.PY_37.value)
)
if not target_python_version.has_union_operator:
warn(
f"`--use-union-operator` can not be used with `--target-python_version` {target_python_version.value}.\n"
f"`--target-python_version` {PythonVersion.PY_310.value} will be used."
)
if not values.get('disable_warnings'):
warn(
f"`--use-union-operator` can not be used with `--target-python_version` {target_python_version.value}.\n"
f"`--target-python_version` {PythonVersion.PY_310.value} will be used."
)
values['target_python_version'] = PythonVersion.PY_310
return values

input: Optional[Union[Path, str]]
input_file_type: InputFileType = InputFileType.Auto
output: Optional[Path]
debug: bool = False
disable_warnings: bool = False
target_python_version: PythonVersion = PythonVersion.PY_37
base_class: str = DEFAULT_BASE_CLASS
custom_template_dir: Optional[Path]
Expand Down Expand Up @@ -606,6 +612,8 @@ def main(args: Optional[Sequence[str]] = None) -> Exit:
if config.debug: # pragma: no cover
enable_debug_message()

if config.disable_warnings:
warnings.simplefilter('ignore')
extra_template_data: Optional[DefaultDict[str, Dict[str, Any]]]
if config.extra_template_data is None:
extra_template_data = None
Expand Down
5 changes: 3 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ usage: datamodel-codegen [-h] [--input INPUT] [--url URL]
[--empty-enum-field-name EMPTY_ENUM_FIELD_NAME]
[--capitalise-enum-members]
[--special-field-name-prefix SPECIAL_FIELD_NAME_PREFIX]
[--remove-special-field-name-prefix]
[--remove-special-field-name-prefix]
[--use-subclass-enum] [--class-name CLASS_NAME]
[--use-title-as-name]
[--custom-template-dir CUSTOM_TEMPLATE_DIR]
Expand All @@ -326,7 +326,7 @@ usage: datamodel-codegen [-h] [--input INPUT] [--url URL]
[--target-python-version {3.6,3.7,3.8,3.9,3.10,3.11}]
[--wrap-string-literal] [--validation]
[--use-double-quotes] [--encoding ENCODING] [--debug]
[--version]
[--disable-warnings] [--version]
options:
-h, --help show this help message and exit
Expand Down Expand Up @@ -434,6 +434,7 @@ options:
be used without this option.
--encoding ENCODING The encoding of input and output (default: cp1252)
--debug show debug message
--disable-warnings disable warnings
--version show version
```

Expand Down
43 changes: 43 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4593,3 +4593,46 @@ def test_main_json_snake_case_field():
)
with pytest.raises(SystemExit):
main()


@pytest.mark.filterwarnings('error')
def test_main_disable_warnings_config(capsys: CaptureFixture):
with TemporaryDirectory() as output_dir:
output_file: Path = Path(output_dir) / 'output.py'
return_code: Exit = main(
[
'--input',
str(JSON_SCHEMA_DATA_PATH / 'person.json'),
'--output',
str(output_file),
'--input-file-type',
'jsonschema',
'--use-union-operator',
'--target-python-version',
'3.6',
'--disable-warnings',
]
)
captured = capsys.readouterr()
assert return_code == Exit.OK
assert captured.err == ''


@pytest.mark.filterwarnings('error')
def test_main_disable_warnings(capsys: CaptureFixture):
with TemporaryDirectory() as output_dir:
output_file: Path = Path(output_dir) / 'output.py'
return_code: Exit = main(
[
'--input',
str(JSON_SCHEMA_DATA_PATH / 'all_of_with_object.json'),
'--output',
str(output_file),
'--input-file-type',
'jsonschema',
'--disable-warnings',
]
)
captured = capsys.readouterr()
assert return_code == Exit.OK
assert captured.err == ''

0 comments on commit a019450

Please sign in to comment.