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

Add better typing in jinja_static.py #10835

Merged
merged 3 commits into from
Oct 11, 2024
Merged
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
52 changes: 19 additions & 33 deletions core/dbt/clients/jinja_static.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import typing
from typing import Any, Dict, List, Optional, Union

import jinja2
Expand All @@ -9,23 +10,29 @@
from dbt_common.tests import test_caching_enabled
from dbt_extractor import ExtractionError, py_extract_from_source # type: ignore

_TESTING_MACRO_CACHE: Optional[Dict[str, Any]] = {}
if typing.TYPE_CHECKING:
from dbt.context.providers import ParseDatabaseWrapper

Check warning on line 14 in core/dbt/clients/jinja_static.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/clients/jinja_static.py#L14

Added line #L14 was not covered by tests


def statically_extract_macro_calls(string, ctx, db_wrapper=None):
_TESTING_MACRO_CACHE: Dict[str, Any] = {}


def statically_extract_macro_calls(
source: str, ctx: Dict[str, Any], db_wrapper: Optional["ParseDatabaseWrapper"] = None
) -> List[str]:
# set 'capture_macros' to capture undefined
env = get_environment(None, capture_macros=True)

global _TESTING_MACRO_CACHE
if test_caching_enabled() and string in _TESTING_MACRO_CACHE:
parsed = _TESTING_MACRO_CACHE.get(string, None)
if test_caching_enabled() and source in _TESTING_MACRO_CACHE:
parsed = _TESTING_MACRO_CACHE.get(source, None)
func_calls = getattr(parsed, "_dbt_cached_calls")
else:
parsed = env.parse(string)
parsed = env.parse(source)
func_calls = tuple(parsed.find_all(jinja2.nodes.Call))

if test_caching_enabled():
_TESTING_MACRO_CACHE[string] = parsed
_TESTING_MACRO_CACHE[source] = parsed
setattr(parsed, "_dbt_cached_calls", func_calls)

standard_calls = ["source", "ref", "config"]
Expand Down Expand Up @@ -69,30 +76,9 @@
return possible_macro_calls


# Call(
# node=Getattr(
# node=Name(
# name='adapter',
# ctx='load'
# ),
# attr='dispatch',
# ctx='load'
# ),
# args=[
# Const(value='test_pkg_and_dispatch')
# ],
# kwargs=[
# Keyword(
# key='packages',
# value=Call(node=Getattr(node=Name(name='local_utils', ctx='load'),
# attr='_get_utils_namespaces', ctx='load'), args=[], kwargs=[],
# dyn_args=None, dyn_kwargs=None)
# )
# ],
# dyn_args=None,
# dyn_kwargs=None
# )
def statically_parse_adapter_dispatch(func_call, ctx, db_wrapper):
def statically_parse_adapter_dispatch(
func_call, ctx: Dict[str, Any], db_wrapper: Optional["ParseDatabaseWrapper"]
) -> List[str]:
possible_macro_calls = []
# This captures an adapter.dispatch('<macro_name>') call.

Expand Down Expand Up @@ -144,7 +130,7 @@

if db_wrapper:
macro = db_wrapper.dispatch(func_name, macro_namespace=macro_namespace).macro
func_name = f"{macro.package_name}.{macro.name}"
func_name = f"{macro.package_name}.{macro.name}" # type: ignore[attr-defined]
possible_macro_calls.append(func_name)
else: # this is only for tests/unit/test_macro_calls.py
if macro_namespace:
Expand Down Expand Up @@ -240,8 +226,8 @@
return unrendered_config


def construct_static_kwarg_value(kwarg):
# Instead of trying to re-assemble complex kwarg value, simply stringify the value
def construct_static_kwarg_value(kwarg) -> str:
# Instead of trying to re-assemble complex kwarg value, simply stringify the value.
# This is still useful to be able to detect changes in unrendered configs, even if it is
# not an exact representation of the user input.
return str(kwarg)
Loading