Skip to content

Commit

Permalink
restrict behavior flags to firing a warning only the first time (#197)
Browse files Browse the repository at this point in the history
* restrict behavior flags to firing a warning only the first time

* changelog

---------

Co-authored-by: Colin Rogers <[email protected]>
  • Loading branch information
mikealfare and colin-rogers-dbt authored Sep 23, 2024
1 parent 7e2edee commit 5d1d82a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20240920-165607.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: Restrict behavior change warning to firing once per run to avoid noisy warnings
in logs
time: 2024-09-20T16:56:07.951544-04:00
custom:
Author: mikealfare
Issue: "197"
5 changes: 4 additions & 1 deletion dbt_common/behavior_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class BehaviorFlagRendered:
user_overrides: a set of user settings, one of which may be an override on this behavior flag
"""

fired: bool = False

def __init__(self, flag: BehaviorFlag, user_overrides: Dict[str, Any]) -> None:
self._validate(flag)

Expand All @@ -72,8 +74,9 @@ def _validate(flag: BehaviorFlag) -> None:

@property
def setting(self) -> bool:
if self._setting is False:
if self._setting is False and not self.fired:
fire_event(self._behavior_change_event)
self.fired = True
return self._setting

@setting.setter
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_behavior_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,35 @@ def test_behavior_flags_no_behavior_change_event_on_no_warn(event_catcher: Event
def test_behavior_flag_requires_description_or_docs_url(event_catcher: EventCatcher) -> None:
with pytest.raises(DbtInternalError):
Behavior([{"name": "flag_false", "default": False}], {})


def test_behavior_flags_fire_once_per_flag(event_catcher: EventCatcher) -> None:
behavior = Behavior(
[
{"name": "flag_1", "default": False, "description": "This is flag 1."},
{"name": "flag_2", "default": False, "description": "This is flag 2."},
],
{},
)

assert len(event_catcher.caught_events) == 0

# trigger the evaluation for flag_1, an event should fire
if behavior.flag_1:
pass
assert len(event_catcher.caught_events) == 1

# trigger the evaluation for flag_1 again, no event should fire
if behavior.flag_1:
pass
assert len(event_catcher.caught_events) == 1

# trigger the evaluation for flag_2, an event should fire
if behavior.flag_2:
pass
assert len(event_catcher.caught_events) == 2

# trigger the evaluation for flag_1 again, no event should fire
if behavior.flag_1:
pass
assert len(event_catcher.caught_events) == 2

0 comments on commit 5d1d82a

Please sign in to comment.