Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 1.5.latest] fix error message for empty/None: --warn-error-options handling #7817

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230530-104228.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Fix empty --warn-error-options error message
time: 2023-05-30T10:42:28.382804-04:00
custom:
Author: michelleark
Issue: "7730"
9 changes: 5 additions & 4 deletions core/dbt/cli/option_types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from click import ParamType, Choice

from dbt.config.utils import parse_cli_vars
from dbt.exceptions import ValidationError
from dbt.config.utils import parse_cli_yaml_string
from dbt.exceptions import ValidationError, DbtValidationError, OptionNotYamlDictError

from dbt.helper_types import WarnErrorOptions

Expand All @@ -16,8 +16,9 @@ def convert(self, value, param, ctx):
if not isinstance(value, str):
self.fail(f"Cannot load YAML from type {type(value)}", param, ctx)
try:
return parse_cli_vars(value)
except ValidationError:
param_option_name = param.opts[0] if param.opts else param.name
return parse_cli_yaml_string(value, param_option_name.strip("-"))
except (ValidationError, DbtValidationError, OptionNotYamlDictError):
self.fail(f"String '{value}' is not valid YAML", param, ctx)


Expand Down
2 changes: 1 addition & 1 deletion core/dbt/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ def parse_cli_yaml_string(var_string: str, cli_option_name: str) -> Dict[str, An
return cli_vars
else:
raise OptionNotYamlDictError(var_type, cli_option_name)
except DbtValidationError:
except (DbtValidationError, OptionNotYamlDictError):
fire_event(InvalidOptionYAML(option_name=cli_option_name))
raise
26 changes: 26 additions & 0 deletions tests/unit/test_option_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from click import Option, BadParameter
import pytest

from dbt.cli.option_types import YAML


class TestYAML:
@pytest.mark.parametrize(
"raw_value,expected_converted_value",
[
("{}", {}),
("{'test_var_key': 'test_var_value'}", {"test_var_key": "test_var_value"}),
],
)
def test_yaml_init(self, raw_value, expected_converted_value):
converted_value = YAML().convert(raw_value, Option(["--vars"]), None)
assert converted_value == expected_converted_value

@pytest.mark.parametrize(
"invalid_yaml_str",
["{", ""],
)
def test_yaml_init_invalid_yaml_str(self, invalid_yaml_str):
with pytest.raises(BadParameter) as e:
YAML().convert(invalid_yaml_str, Option(["--vars"]), None)
assert "--vars" in e.value.format_message()