Skip to content

Commit

Permalink
Update exclusive_primary_alt_value_setting to raise more generalize…
Browse files Browse the repository at this point in the history
…d error
  • Loading branch information
QMalcolm committed Apr 30, 2024
1 parent 0d960b2 commit 41e6d7c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
8 changes: 6 additions & 2 deletions core/dbt/cli/option_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ class WarnErrorOptionsType(YAML):
def convert(self, value, param, ctx):
# this function is being used by param in click
include_exclude = super().convert(value, param, ctx)
exclusive_primary_alt_value_setting(include_exclude, "include", "error")
exclusive_primary_alt_value_setting(include_exclude, "exclude", "warn")
exclusive_primary_alt_value_setting(
include_exclude, "include", "error", "warn_error_options"
)
exclusive_primary_alt_value_setting(
include_exclude, "exclude", "warn", "warn_error_options"
)

return WarnErrorOptions(
include=include_exclude.get("include", []),
Expand Down
12 changes: 8 additions & 4 deletions core/dbt/config/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from dbt.adapters.contracts.connection import QueryComment
from dbt.exceptions import (
DbtProjectError,
DbtWarnErrorOptionsError,
DbtExclusivePropertyUseError,
ProjectContractBrokenError,
ProjectContractError,
DbtRuntimeError,
Expand Down Expand Up @@ -840,12 +840,16 @@ def read_project_flags(project_dir: str, profiles_dir: str) -> ProjectFlags:
# handle collapsing `include` and `error` as well as collapsing `exclude` and `warn`
# for warn_error_options
warn_error_options = project_flags.get("warn_error_options")
exclusive_primary_alt_value_setting(warn_error_options, "include", "error")
exclusive_primary_alt_value_setting(warn_error_options, "exclude", "warn")
exclusive_primary_alt_value_setting(
warn_error_options, "include", "error", "warn_error_options"
)
exclusive_primary_alt_value_setting(
warn_error_options, "exclude", "warn", "warn_error_options"
)

ProjectFlags.validate(project_flags)
return ProjectFlags.from_dict(project_flags)
except (DbtProjectError, DbtWarnErrorOptionsError) as exc:
except (DbtProjectError, DbtExclusivePropertyUseError) as exc:
# We don't want to eat the DbtProjectError for UserConfig to ProjectFlags or
# DbtConfigError for warn_error_options munging
raise exc
Expand Down
12 changes: 8 additions & 4 deletions core/dbt/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dbt.clients import yaml_helper
from dbt_common.events.functions import fire_event
from dbt.events.types import InvalidOptionYAML
from dbt.exceptions import DbtWarnErrorOptionsError, OptionNotYamlDictError
from dbt.exceptions import DbtExclusivePropertyUseError, OptionNotYamlDictError
from dbt_common.exceptions import DbtValidationError


Expand All @@ -26,7 +26,10 @@ def parse_cli_yaml_string(var_string: str, cli_option_name: str) -> Dict[str, An


def exclusive_primary_alt_value_setting(
dictionary: Optional[Dict[str, Any]], primary: str, alt: str
dictionary: Optional[Dict[str, Any]],
primary: str,
alt: str,
parent_config: Optional[str] = None,
) -> None:
"""Munges in place under the primary the options for the primary and alt values
Expand All @@ -42,8 +45,9 @@ def exclusive_primary_alt_value_setting(
alt_options = dictionary.get(alt)

if primary_options and alt_options:
raise DbtWarnErrorOptionsError(
f"Only `{alt}` or `{primary}` can be specified in `warn_error_options`, not both"
where = f" in `{parent_config}`" if parent_config is not None else ""
raise DbtExclusivePropertyUseError(
f"Only `{alt}` or `{primary}` can be specified{where}, not both"
)

if alt_options:
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class DbtProfileError(DbtConfigError):
pass


class DbtWarnErrorOptionsError(DbtConfigError):
class DbtExclusivePropertyUseError(DbtConfigError):
pass


Expand Down
4 changes: 2 additions & 2 deletions tests/unit/config/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from dbt.config.utils import exclusive_primary_alt_value_setting
from dbt.exceptions import DbtWarnErrorOptionsError
from dbt.exceptions import DbtExclusivePropertyUseError


class TestExclusivePrimaryAltValueSetting:
Expand Down Expand Up @@ -31,7 +31,7 @@ def test_alt_set(self, primary_key: str, alt_key: str, value: str):

def test_primary_and_alt_set(self, primary_key: str, alt_key: str, value: str):
test_dict = {primary_key: value, alt_key: value}
with pytest.raises(DbtWarnErrorOptionsError):
with pytest.raises(DbtExclusivePropertyUseError):
exclusive_primary_alt_value_setting(test_dict, primary_key, alt_key)

def test_neither_primary_nor_alt_set(self, primary_key: str, alt_key: str):
Expand Down

0 comments on commit 41e6d7c

Please sign in to comment.