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

restrict behavior flags to firing a warning only the first time #197

Merged
merged 3 commits into from
Sep 23, 2024
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
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
Loading