Skip to content

Commit

Permalink
Merge pull request #1085 from fishtown-analytics/fix/more-helpful-inv…
Browse files Browse the repository at this point in the history
…alid-refs

Fix/more helpful invalid refs (#1080)
  • Loading branch information
beckjake authored Oct 23, 2018
2 parents fe43895 + 6702d5e commit a317456
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions dbt/clients/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ def parse(self, parser):
return node


def _is_dunder_name(name):
return name.startswith('__') and name.endswith('__')


def create_macro_capture_env(node):

class ParserMacroCapture(jinja2.Undefined):
Expand All @@ -194,17 +198,37 @@ class ParserMacroCapture(jinja2.Undefined):
def __init__(self, hint=None, obj=None, name=None,
exc=None):
super(jinja2.Undefined, self).__init__()

self.node = node
self.name = name
self.package_name = node.get('package_name')

def __getattr__(self, name):

# jinja uses these for safety, so we have to override them.
# see https://github.com/pallets/jinja/blob/master/jinja2/sandbox.py#L332-L339 # noqa
if name in ['unsafe_callable', 'alters_data']:
return False
self.unsafe_callable = False
self.alters_data = False

def __deepcopy__(self, memo):
path = os.path.join(self.node.get('root_path'),
self.node.get('original_file_path'))

logger.debug(
'A ParserMacroCapture has been deecopy()d, invalid reference '
'to "{}" in node {}.{} (source path: {})'
.format(self.name, self.node.get('package_name'),
self.node.get('name'),
path))

dbt.exceptions.raise_compiler_error(
'dbt has detected at least one invalid reference in {}.{}. '
'Check logs for more information'
.format(self.node.get('package_name'), self.node.get('name'))
)

def __getattr__(self, name):
if name == 'name' or _is_dunder_name(name):
raise AttributeError(
"'{}' object has no attribute '{}'"
.format(type(self).__name__, name)
)

self.package_name = self.name
self.name = name
Expand Down

0 comments on commit a317456

Please sign in to comment.