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

Draft: feat(python): allows to use prj. vars #10932

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
feat(python): allows to use prj. vars
  • Loading branch information
devmessias committed Oct 29, 2024
commit 9aa03f873cd45e1bdb3ccd80261422114d8e3e43
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 @@
# 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 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 @@

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 @@
)
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
Loading