Skip to content

Commit

Permalink
Fix: Rename try methods to strict (dbt-labs#5477) (dbt-labs#5478)
Browse files Browse the repository at this point in the history
* rename strict methods

* add changelog

Co-authored-by: Jeremy Yeo <jeremyyeo@users.noreply.github.com>
  • Loading branch information
2 people authored and Axel Goblet committed Sep 16, 2022
1 parent 68f98a2 commit 608ae7e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20220715-231148.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: Rename try to strict for more intuitiveness
time: 2022-07-15T23:11:48.327928+12:00
custom:
Author: jeremyyeo
Issue: "5475"
PR: "5477"
22 changes: 9 additions & 13 deletions core/dbt/context/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,19 +474,17 @@ def _set(value: Iterable[Any], default: Any = None) -> Optional[Set[Any]]:

@contextmember
@staticmethod
def try_set(value: Iterable[Any]) -> Set[Any]:
"""The `try_set` context method can be used to convert any iterable
def set_strict(value: Iterable[Any]) -> Set[Any]:
"""The `set_strict` context method can be used to convert any iterable
to a sequence of iterable elements that are unique (a set). The
difference to the `set` context method is that the `try_set` method
difference to the `set` context method is that the `set_strict` method
will raise an exception on a TypeError.
:param value: The iterable
:param default: A default value to return if the `value` argument
is not an iterable
Usage:
{% set my_list = [1, 2, 2, 3] %}
{% set my_set = try_set(my_list) %}
{% set my_set = set_strict(my_list) %}
{% do log(my_set) %} {# {1, 2, 3} #}
"""
try:
Expand All @@ -497,7 +495,7 @@ def try_set(value: Iterable[Any]) -> Set[Any]:
@contextmember("zip")
@staticmethod
def _zip(*args: Iterable[Any], default: Any = None) -> Optional[Iterable[Any]]:
"""The `try_zip` context method can be used to used to return
"""The `zip` context method can be used to used to return
an iterator of tuples, where the i-th tuple contains the i-th
element from each of the argument iterables.
Expand All @@ -518,21 +516,19 @@ def _zip(*args: Iterable[Any], default: Any = None) -> Optional[Iterable[Any]]:

@contextmember
@staticmethod
def try_zip(*args: Iterable[Any]) -> Iterable[Any]:
"""The `try_zip` context method can be used to used to return
def zip_strict(*args: Iterable[Any]) -> Iterable[Any]:
"""The `zip_strict` context method can be used to used to return
an iterator of tuples, where the i-th tuple contains the i-th
element from each of the argument iterables. The difference to the
`zip` context method is that the `try_zip` method will raise an
`zip` context method is that the `zip_strict` method will raise an
exception on a TypeError.
:param *args: Any number of iterables
:param default: A default value to return if `*args` is not
iterable
Usage:
{% set my_list_a = [1, 2] %}
{% set my_list_b = ['alice', 'bob'] %}
{% set my_zip = try_zip(my_list_a, my_list_b) | list %}
{% set my_zip = zip_strict(my_list_a, my_list_b) | list %}
{% do log(my_set) %} {# [(1, 'alice'), (2, 'bob')] #}
"""
try:
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ def assert_has_keys(required_keys: Set[str], maybe_keys: Set[str], ctx: Dict[str
"fromyaml",
"toyaml",
"set",
"try_set",
"set_strict",
"zip",
"try_zip",
"zip_strict",
"log",
"run_started_at",
"invocation_id",
Expand Down
18 changes: 9 additions & 9 deletions tests/functional/context_methods/test_builtin_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
{% macro validate_set() %}
{% set set_result = set([1, 2, 2, 3, 'foo', False]) %}
{{ log("set_result: " ~ set_result) }}
{% set try_set_result = try_set([1, 2, 2, 3, 'foo', False]) %}
{{ log("try_set_result: " ~ try_set_result) }}
{% set set_strict_result = set_strict([1, 2, 2, 3, 'foo', False]) %}
{{ log("set_strict_result: " ~ set_strict_result) }}
{% endmacro %}
"""

Expand All @@ -18,17 +18,17 @@
{% set list_b = ['foo', 'bar'] %}
{% set zip_result = zip(list_a, list_b) | list %}
{{ log("zip_result: " ~ zip_result) }}
{% set try_zip_result = try_zip(list_a, list_b) | list %}
{{ log("try_zip_result: " ~ try_zip_result) }}
{% set zip_strict_result = zip_strict(list_a, list_b) | list %}
{{ log("zip_strict_result: " ~ zip_strict_result) }}
{% endmacro %}
"""

models__set_exception_sql = """
{% set try_set_result = try_set(1) %}
{% set set_strict_result = set_strict(1) %}
"""

models__zip_exception_sql = """
{% set try_set_result = try_zip(1) %}
{% set zip_strict_result = zip_strict(1) %}
"""


Expand All @@ -46,18 +46,18 @@ def test_builtin_set_function(self, project):
# The order of the set isn't guaranteed so we can't check for the actual set in the logs
assert "set_result: " in log_output
assert "False" in log_output
assert "try_set_result: " in log_output
assert "set_strict_result: " in log_output

def test_builtin_zip_function(self, project):
_, log_output = run_dbt_and_capture(["--debug", "run-operation", "validate_zip"])

expected_zip = [(1, "foo"), (2, "bar")]
assert f"zip_result: {expected_zip}" in log_output
assert f"try_zip_result: {expected_zip}" in log_output
assert f"zip_strict_result: {expected_zip}" in log_output


class TestContextBuiltinExceptions:
# Assert compilation errors are raised with try_ equivalents
# Assert compilation errors are raised with _strict equivalents
def test_builtin_function_exception(self, project):
write_file(models__set_exception_sql, project.project_root, "models", "raise.sql")
with pytest.raises(CompilationException):
Expand Down

0 comments on commit 608ae7e

Please sign in to comment.