Skip to content
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
18 changes: 8 additions & 10 deletions sqlmesh/dbt/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from sqlmesh.dbt.util import DBT_VERSION
from sqlmesh.utils import AttributeDict, debug_mode_enabled, yaml
from sqlmesh.utils.date import now
from sqlmesh.utils.errors import ConfigError, MacroEvalError
from sqlmesh.utils.errors import ConfigError
from sqlmesh.utils.jinja import JinjaMacroRegistry, MacroReference, MacroReturnVal

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -381,18 +381,16 @@ def do_zip(*args: t.Any, default: t.Optional[t.Any] = None) -> t.Optional[t.Any]
return default


def as_bool(value: str) -> bool:
result = _try_literal_eval(value)
if isinstance(result, bool):
return result
raise MacroEvalError(f"Failed to convert '{value}' into boolean.")
def as_bool(value: t.Any) -> t.Any:
# dbt's jinja TEXT_FILTERS just return the input value as is
# https://github.com/dbt-labs/dbt-common/blob/main/dbt_common/clients/jinja.py#L559
return value


def as_number(value: str) -> t.Any:
result = _try_literal_eval(value)
if isinstance(value, (int, float)) and not isinstance(result, bool):
return result
raise MacroEvalError(f"Failed to convert '{value}' into number.")
# dbt's jinja TEXT_FILTERS just return the input value as is
# https://github.com/dbt-labs/dbt-common/blob/main/dbt_common/clients/jinja.py#L559
return value


def _try_literal_eval(value: str) -> t.Any:
Expand Down
8 changes: 3 additions & 5 deletions tests/dbt/test_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
PostgresConfig,
)
from sqlmesh.dbt.test import TestConfig
from sqlmesh.utils.errors import ConfigError, MacroEvalError, SQLMeshError
from sqlmesh.utils.errors import ConfigError, SQLMeshError
from sqlmesh.utils.jinja import MacroReference

pytestmark = [pytest.mark.dbt, pytest.mark.slow]
Expand Down Expand Up @@ -1751,12 +1751,10 @@ def test_as_filters(sushi_test_project: Project):
context = sushi_test_project.context

assert context.render("{{ True | as_bool }}") == "True"
with pytest.raises(MacroEvalError, match="Failed to convert 'invalid' into boolean."):
context.render("{{ 'invalid' | as_bool }}")
assert context.render("{{ 'valid' | as_bool }}") == "valid"

assert context.render("{{ 123 | as_number }}") == "123"
with pytest.raises(MacroEvalError, match="Failed to convert 'invalid' into number."):
context.render("{{ 'invalid' | as_number }}")
assert context.render("{{ 'valid' | as_number }}") == "valid"

assert context.render("{{ None | as_text }}") == ""

Expand Down