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

[CT-756] Stringify user-provided messages to {{ log() }} #1

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
10 changes: 8 additions & 2 deletions core/dbt/context/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from dbt.exceptions import (
CompilationException,
MacroReturn,
RuntimeException,
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a more specific/appropriate exception than RuntimeException?

raise_compiler_error,
raise_parsing_error,
disallow_secret_env_var,
Expand Down Expand Up @@ -543,10 +544,11 @@ def zip_strict(*args: Iterable[Any]) -> Iterable[Any]:

@contextmember
@staticmethod
def log(msg: str, info: bool = False) -> str:
def log(msg: Any, info: bool = False) -> str:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""Logs a line to either the log file or stdout.

:param msg: The message to log
:param msg: The message to log. If not a string, then translated into
a string via a call to the python builtin str() function.
:param info: If `False`, write to the log file. If `True`, write to
both the log file and stdout.

Expand All @@ -556,6 +558,10 @@ def log(msg: str, info: bool = False) -> str:
{{ log("Running some_macro: " ~ arg1 ~ ", " ~ arg2) }}
{% endmacro %}"
"""
try:
msg = str(msg)
except Exception as e:
raise RuntimeException("log failed to stringify the given object") from e
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a one-off test for this, since I can't think of a convenient way to induce this within the test case via jinja provided input (given dbt/jinja design, I'd sort of hope not 😆)

image

if info:
fire_event(MacroEventInfo(msg=msg))
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/context_methods/test_cli_var_override.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test__override_vars_global(self, project):


# This one switches to setting a var in 'test'
class TestCLIVarOverridePorject:
class TestCLIVarOverrideProject:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a completely unrelated change to fix this typo. I'm curious what the culture is about bundling in tiny unrelated changes like this. Is it fine or is it expected to be broken out to a separate PR?

@pytest.fixture(scope="class")
def models(self):
return {
Expand Down
55 changes: 55 additions & 0 deletions tests/functional/context_methods/test_log_arbitrary_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is even the appropriate variation of test for this part of the codebase, I found this directory through randomly wandering, and then I copied adjacent files to get it to work.

import os

from dbt.tests.util import run_dbt, get_manifest, run_dbt_and_capture

str_case_sql = """
{{ log('str987654321') }}
select NULL
"""

int_case_sql = """
{{ log(987654321) }}
select NULL
"""

float_case_sql = """
{{ log(9876.54321) }}
select NULL
"""

list_case_sql = """
{{ log([9, 8, 7, 6, 5, 4, 3, 2, 1]) }}
select NULL
"""

dict_case_sql = """
{{ log({9: 8}) }}
select NULL
"""


class TestLogArbitraryTypes:
@pytest.fixture(scope="class")
def models(self):
return {
"str_case.sql": str_case_sql,
"int_case.sql": int_case_sql,
"float_case.sql": float_case_sql,
"list_case.sql": list_case_sql,
"dict_case.sql": dict_case_sql,
}

def test_log(self, project):
# Induce dbt to try scrubbing logs, which fails if not given strings
os.environ["DBT_ENV_SECRET_WHATEVER"] = "1234"
os.environ["DBT_DEBUG"] = "True"
_, log_output = run_dbt_and_capture(["run"])

# These aren't theoretically sound ways to verify the output
# but they should be sufficient for this minor test.
assert " str987654321\n" in log_output
assert " 987654321\n" in log_output
assert " 9876.54321\n" in log_output
assert " [9, 8, 7, 6, 5, 4, 3, 2, 1]\n" in log_output
assert " {9: 8}\n" in log_output