Skip to content

Commit

Permalink
Merge branch 'main' into er/build-tooling
Browse files Browse the repository at this point in the history
  • Loading branch information
emmyoop authored Jan 24, 2024
2 parents 547b865 + 8d69478 commit a5d8218
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20240122-163546.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Clean up macro contexts.
time: 2024-01-22T16:35:46.907999-05:00
custom:
Author: peterallenwebb
Issue: "35"
10 changes: 10 additions & 0 deletions .github/ISSUE_TEMPLATE/implementation-ticket.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ body:
What is the definition of done for this ticket? Include any relevant edge cases and/or test cases
validations:
required: true
- type: textarea
attributes:
label: Suggested Tests
description: |
Provide scenarios to test. Link to existing similar tests if appropriate.
placeholder: |
1. Test with no version specified in the schema file and use selection logic on a versioned model for a specific version. Expect pass.
2. Test with a version specified in the schema file that is no valid. Expect ParsingError.
validations:
required: true
- type: textarea
attributes:
label: Impact to Other Teams
Expand Down
23 changes: 23 additions & 0 deletions .github/ISSUE_TEMPLATE/slash-command-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Short description

<!-- Describe the scope of the ticket, a high-level implementation approach and any tradeoffs to consider -->

## Acceptance criteria

<!-- What is the definition of done for this ticket? Include any relevant edge cases and/or test cases -->

## Suggested Tests

<!-- Provide scenarios to test. Link to existing similar tests if appropriate. -->

## Impact to Other Teams

<!-- Will this change impact other teams? Include details of the kinds of changes required (new tests, code changes, related tickets) and _add the relevant `Impact:[team]` label_. -->

## Will backports be required?

<!-- Will this change need to be backported to previous versions? Add details, possible blockers to backporting and _add the relevant backport labels `backport 1.x.latest`_ -->

## Context

<!-- Provide the "why", motivation, and alternative approaches considered -- linking to previous refinement issues, spikes, docs as appropriate -->
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ jobs:

steps:
- name: Check out the repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.11'

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci_code_quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ jobs:

steps:
- name: Check out the repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.11'

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ jobs:

steps:
- name: "Check out the repository"
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: "Set up Python ${{ matrix.python-version }}"
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand Down
42 changes: 39 additions & 3 deletions dbt_common/clients/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import os
import tempfile
from ast import literal_eval
from collections import ChainMap
from contextlib import contextmanager
from itertools import chain, islice
from typing import List, Union, Set, Optional, Dict, Any, Iterator, Type, Callable
from typing import Any, Callable, Dict, Iterator, List, Mapping, Optional, Union, Set, Type
from typing_extensions import Protocol

import jinja2 # type: ignore
Expand Down Expand Up @@ -101,6 +102,41 @@ def _compile(self, source, filename):
return super()._compile(source, filename) # type: ignore


class MacroFuzzTemplate(jinja2.nativetypes.NativeTemplate):
environment_class = MacroFuzzEnvironment

def new_context(
self,
vars: Optional[Dict[str, Any]] = None,
shared: bool = False,
locals: Optional[Mapping[str, Any]] = None,
) -> jinja2.runtime.Context:
# This custom override makes the assumption that the locals and shared
# parameters are not used, so enforce that.
if shared or locals:
raise Exception("The MacroFuzzTemplate.new_context() override cannot use the shared or locals parameters.")

parent = ChainMap(vars, self.globals) if self.globals else vars

return self.environment.context_class(self.environment, parent, self.name, self.blocks)

def render(self, *args: Any, **kwargs: Any) -> Any:
if kwargs or len(args) != 1:
raise Exception("The MacroFuzzTemplate.render() override requires exactly one argument.")

ctx = self.new_context(args[0])

try:
return self.environment_class.concat( # type: ignore
self.root_render_func(ctx) # type: ignore
)
except Exception:
return self.environment.handle_exception()


MacroFuzzEnvironment.template_class = MacroFuzzTemplate


class NativeSandboxEnvironment(MacroFuzzEnvironment):
code_generator_class = jinja2.nativetypes.NativeCodeGenerator

Expand Down Expand Up @@ -174,7 +210,7 @@ def render(self, *args, **kwargs):
with :func:`ast.literal_eval`, the parsed value is returned.
Otherwise, the string is returned.
"""
vars = dict(*args, **kwargs)
vars = args[0]

try:
return quoted_native_concat(self.root_render_func(self.new_context(vars)))
Expand Down Expand Up @@ -229,7 +265,7 @@ def get_macro(self):
# make_module is in jinja2.environment. It returns a TemplateModule
module = template.make_module(vars=self.context, shared=False)
macro = module.__dict__[get_dbt_macro_name(name)]
module.__dict__.update(self.context)

return macro

@contextmanager
Expand Down

0 comments on commit a5d8218

Please sign in to comment.