Skip to content

Commit

Permalink
feat(python): allows to use prj. vars
Browse files Browse the repository at this point in the history
  • Loading branch information
devmessias committed Oct 29, 2024
1 parent 40c350f commit 9aa03f8
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions core/dbt/parser/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
from dbt_common.exceptions.macros import UndefinedMacroError
from dbt_extractor import ExtractionError, py_extract_from_source # type: ignore

dbt_function_key_words = set(["ref", "source", "config", "get"])
dbt_function_full_names = set(["dbt.ref", "dbt.source", "dbt.config", "dbt.config.get"])
dbt_function_key_words = set(["ref", "source", "config", "get", "var"])
dbt_function_full_names = set(
["dbt.ref", "dbt.source", "dbt.config", "dbt.config.get", "dbt.var", "dbt.var.get"]
)


class PythonValidationVisitor(ast.NodeVisitor):
Expand Down Expand Up @@ -115,7 +117,10 @@ def visit_Call(self, node: ast.Call) -> None:
# drop the dot-dbt prefix
func_name = func_name.split(".")[-1]
args, kwargs = self._get_call_literals(node)
self.dbt_function_calls.append((func_name, args, kwargs))
value = node.func.value
# dbt_ctx_obj will be used to get which obj uses the get method: config, var
dbt_ctx_obj = value.attr if hasattr(value, "attr") else None
self.dbt_function_calls.append((func_name, args, kwargs, dbt_ctx_obj))

# no matter what happened above, we should keep visiting the rest of the tree
# visit args and kwargs to see if there's call in it
Expand Down Expand Up @@ -188,7 +193,8 @@ def get_compiled_path(cls, block: FileBlock):
def parse_python_model(self, node, config, context):
config_keys_used = []
config_keys_defaults = []

var_keys_used = []
var_keys_defaults = []
try:
tree = ast.parse(node.raw_code, filename=node.original_file_path)
except SyntaxError as exc:
Expand All @@ -204,8 +210,7 @@ def parse_python_model(self, node, config, context):

dbt_parser = PythonParseVisitor(node)
dbt_parser.visit(tree)

for func, args, kwargs in dbt_parser.dbt_function_calls:
for func, args, kwargs, dbt_ctx_obj in dbt_parser.dbt_function_calls:
if func == "get":
num_args = len(args)
if num_args == 0:
Expand All @@ -220,18 +225,24 @@ def parse_python_model(self, node, config, context):
)
key = args[0]
default_value = args[1] if num_args == 2 else None
config_keys_used.append(key)
config_keys_defaults.append(default_value)
if dbt_ctx_obj == "config":
config_keys_used.append(key)
config_keys_defaults.append(default_value)
elif dbt_ctx_obj == "var":
var_keys_used.append(key)
var_keys_defaults.append(default_value)

Check warning on line 233 in core/dbt/parser/models.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/models.py#L228-L233

Added lines #L228 - L233 were not covered by tests
continue

context[func](*args, **kwargs)

kwargs_config = {}
# this is being used in macro build_config_dict
if config_keys_used:
# this is being used in macro build_config_dict
context["config"](
config_keys_used=config_keys_used,
config_keys_defaults=config_keys_defaults,
)
kwargs_config["config_keys_used"] = config_keys_used
kwargs_config["config_keys_defaults"] = (config_keys_defaults,)

Check warning on line 240 in core/dbt/parser/models.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/models.py#L239-L240

Added lines #L239 - L240 were not covered by tests
if var_keys_used:
kwargs_config["var_keys_used"] = var_keys_used
kwargs_config["var_keys_defaults"] = var_keys_defaults

Check warning on line 243 in core/dbt/parser/models.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/models.py#L242-L243

Added lines #L242 - L243 were not covered by tests
if kwargs_config:
context["config"](**kwargs_config)

Check warning on line 245 in core/dbt/parser/models.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/models.py#L245

Added line #L245 was not covered by tests

def render_update(self, node: ModelNode, config: ContextConfig) -> None:
self.manifest._parsing_info.static_analysis_path_count += 1
Expand Down

0 comments on commit 9aa03f8

Please sign in to comment.