From 71a8e897514cfcf4dcab379aafe43720dbf0c1b7 Mon Sep 17 00:00:00 2001 From: jeremyyeo Date: Fri, 15 Jul 2022 15:22:51 +1200 Subject: [PATCH 1/2] rename strict methods --- core/dbt/context/base.py | 22 ++++++++----------- test/unit/test_context.py | 4 ++-- .../context_methods/test_builtin_functions.py | 18 +++++++-------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/core/dbt/context/base.py b/core/dbt/context/base.py index 4a71388f81d..b7ebaf8468f 100644 --- a/core/dbt/context/base.py +++ b/core/dbt/context/base.py @@ -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: @@ -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. @@ -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: diff --git a/test/unit/test_context.py b/test/unit/test_context.py index 91cea11b3f6..ce6436204a7 100644 --- a/test/unit/test_context.py +++ b/test/unit/test_context.py @@ -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", diff --git a/tests/functional/context_methods/test_builtin_functions.py b/tests/functional/context_methods/test_builtin_functions.py index 12c6f96a195..a44826d4a27 100644 --- a/tests/functional/context_methods/test_builtin_functions.py +++ b/tests/functional/context_methods/test_builtin_functions.py @@ -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 %} """ @@ -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) %} """ @@ -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): From 0d1b197b01a9258252cdc47b25e9c45527ccf06d Mon Sep 17 00:00:00 2001 From: jeremyyeo Date: Fri, 15 Jul 2022 23:12:02 +1200 Subject: [PATCH 2/2] add changelog --- .changes/unreleased/Fixes-20220715-231148.yaml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changes/unreleased/Fixes-20220715-231148.yaml diff --git a/.changes/unreleased/Fixes-20220715-231148.yaml b/.changes/unreleased/Fixes-20220715-231148.yaml new file mode 100644 index 00000000000..f887f80a441 --- /dev/null +++ b/.changes/unreleased/Fixes-20220715-231148.yaml @@ -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"