Skip to content

Commit

Permalink
batch #2 of fixes to issues reported by codacy
Browse files Browse the repository at this point in the history
  • Loading branch information
kkozik-amplify committed Jan 14, 2025
1 parent a18a8b2 commit d9639af
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion hcl2/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, attributes: Optional[dict] = None):
self.attributes = attributes or {}

def block(
self, block_type: str, labels: Optional[List[str]] = None, **attributes: dict
self, block_type: str, labels: Optional[List[str]] = None, **attributes
) -> "Builder":
"""Create a block within this HCL document."""
labels = labels or []
Expand Down
7 changes: 2 additions & 5 deletions hcl2/reconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from typing import List, Dict, Callable, Optional, Union, Any, Tuple

from lark import Lark, Tree
from lark.grammar import Terminal, NonTerminal, Symbol
from lark.grammar import Terminal, Symbol
from lark.lexer import Token, PatternStr, TerminalDef
from lark.reconstruct import Reconstructor, is_iter_empty
from lark.reconstruct import Reconstructor
from lark.tree_matcher import is_discarded_terminal
from lark.visitors import Transformer_InPlace

Expand Down Expand Up @@ -94,15 +94,12 @@ def __default__(self, data, children, meta):
to_write += item
else:
if isinstance(item, Token):
assert Terminal(item.type) == sym, item
# annotate the leaf with the specific rule (data) and
# terminal (sym) it was generated from
to_write.append((data, sym, item))
else:
assert NonTerminal(item.data) == sym, (sym, item)
to_write.append(item)

assert is_iter_empty(iter_args)
return to_write


Expand Down
18 changes: 11 additions & 7 deletions test/unit/test_builder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint:disable=C0116

"""Test building an HCL file from scratch"""

import json
from pathlib import Path
from unittest import TestCase

Expand Down Expand Up @@ -51,21 +52,24 @@ def test_locals_embdedded_condition_tf(self):
def test_locals_embedded_function_tf(self):
builder = hcl2.Builder()

builder.block(
"locals",
function_test='${var.basename}-${var.forwarder_function_name}_${md5("${var.vpc_id}${data.aws_region.current.name}")}',
function_test = (
"${var.basename}-${var.forwarder_function_name}_"
'${md5("${var.vpc_id}${data.aws_region.current.name}")}'
)
builder.block("locals", function_test=function_test)

self.compare_filenames(builder, "locals_embedded_function.tf")

def test_locals_embedded_interpolation_tf(self):
builder = hcl2.Builder()

builder.block(
"locals",
embedded_interpolation='${module.special_constants.aws_accounts["aaa-${local.foo}-${local.bar}"]}/us-west-2/key_foo',
embedded_interpolation = (
"${module.special_constants.aws_accounts"
'["aaa-${local.foo}-${local.bar}"]}/us-west-2/key_foo'
)

builder.block("locals", embedded_interpolation=embedded_interpolation)

self.compare_filenames(builder, "locals_embedded_interpolation.tf")

def test_provider_function_tf(self):
Expand Down

0 comments on commit d9639af

Please sign in to comment.