Skip to content

Commit

Permalink
Remove Print and use event system (#10131)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenyuLInx authored May 21, 2024
1 parent ad61200 commit eb71ad9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20240516-153913.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: Fix json format log and --quiet for ls and jinja print by converting print call
to fire events
time: 2024-05-16T15:39:13.896723-07:00
custom:
Author: ChenyuLInx
Issue: "8756"
4 changes: 3 additions & 1 deletion core/dbt/context/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from dbt_common.context import get_invocation_context
from dbt_common.events.contextvars import get_node_info
from dbt_common.events.functions import fire_event, get_invocation_id
from dbt_common.events.types import PrintEvent
from dbt_common.exceptions.macros import MacroReturn

# See the `contexts` module README for more information on how contexts work
Expand Down Expand Up @@ -683,7 +684,8 @@ def print(msg: str) -> str:
"""

if get_flags().PRINT:
print(msg)
# No formatting, still get to stdout when --quiet is used
fire_event(PrintEvent(msg=msg))
return ""

@contextmember()
Expand Down
1 change: 1 addition & 0 deletions core/dbt/events/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,7 @@ def message(self) -> str:


class ListCmdOut(InfoLevel):
# No longer in use, switching to Z051 PrintEvent in dbt-common
def code(self) -> str:
return "Z049"

Expand Down
11 changes: 4 additions & 7 deletions core/dbt/task/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
SourceDefinition,
UnitTestDefinition,
)
from dbt.events.types import ListCmdOut, NoNodesSelected
from dbt.flags import get_flags
from dbt.events.types import NoNodesSelected
from dbt.graph import ResourceTypeSelector
from dbt.node_types import NodeType
from dbt.task.base import resource_types_from_args
from dbt.task.runnable import GraphRunnableTask
from dbt.task.test import TestSelector
from dbt_common.events.contextvars import task_contextvars
from dbt_common.events.functions import fire_event, warn_or_error
from dbt_common.events.types import PrintEvent
from dbt_common.exceptions import DbtInternalError, DbtRuntimeError


Expand Down Expand Up @@ -172,11 +172,8 @@ def output_results(self, results):
"""Log, or output a plain, newline-delimited, and ready-to-pipe list of nodes found."""
for result in results:
self.node_results.append(result)
if get_flags().LOG_FORMAT == "json":
fire_event(ListCmdOut(msg=result))
else:
# Cleaner to leave as print than to mutate the logger not to print timestamps.
print(result)
# No formatting, still get to stdout when --quiet is used
fire_event(PrintEvent(msg=result))
return self.node_results

@property
Expand Down
2 changes: 1 addition & 1 deletion core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"minimal-snowplow-tracker>=0.0.2,<0.1",
"dbt-semantic-interfaces>=0.5.1,<0.6",
# Minor versions for these are expected to be backwards-compatible
"dbt-common>=1.0.4,<2.0",
"dbt-common>=1.1.0,<2.0",
"dbt-adapters>=1.1.1,<2.0",
# ----
# Expect compatibility with all new versions of these packages, so lower bounds only.
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/task/test_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from argparse import Namespace
from unittest.mock import patch

from dbt.flags import get_flags, set_from_args
from dbt.task.list import ListTask
from dbt_common.events.types import PrintEvent


def test_list_output_results():
set_from_args(Namespace(models=None), {})
task = ListTask(get_flags(), None, None)
results = ["node1", "node2", "node3"]
expected_node_results = ["node1", "node2", "node3"]

with patch("dbt.task.list.fire_event") as mock_fire_event:
node_results = task.output_results(results)

assert node_results == expected_node_results
# assert called with PrintEvent type object and message 'node1', 'node2', 'node3'
for call_args in mock_fire_event.call_args_list:
assert isinstance(call_args[0][0], PrintEvent)
assert call_args[0][0].msg in expected_node_results

0 comments on commit eb71ad9

Please sign in to comment.