Skip to content

Commit 03309b4

Browse files
authored
fix: dbt as_bool and as_number filters should return their original input values (#5532)
1 parent 7337133 commit 03309b4

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

sqlmesh/dbt/builtin.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from sqlmesh.dbt.util import DBT_VERSION
2626
from sqlmesh.utils import AttributeDict, debug_mode_enabled, yaml
2727
from sqlmesh.utils.date import now
28-
from sqlmesh.utils.errors import ConfigError, MacroEvalError
28+
from sqlmesh.utils.errors import ConfigError
2929
from sqlmesh.utils.jinja import JinjaMacroRegistry, MacroReference, MacroReturnVal
3030

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

383383

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

390389

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

397395

398396
def _try_literal_eval(value: str) -> t.Any:

tests/dbt/test_transformation.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
PostgresConfig,
6666
)
6767
from sqlmesh.dbt.test import TestConfig
68-
from sqlmesh.utils.errors import ConfigError, MacroEvalError, SQLMeshError
68+
from sqlmesh.utils.errors import ConfigError, SQLMeshError
6969
from sqlmesh.utils.jinja import MacroReference
7070

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

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

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

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

0 commit comments

Comments
 (0)