Skip to content

object literal expression parsed in way that can't be serialized #253

Closed

Description

@tmccombs

Describe the bug

In contexts where a map or object is part of an hcl expression, the load or loads function will deserialize as an invalid interpolation.

Software:

  • OS: linux
  • Python version: 3.13 6
  • python-hcl2 version: 7.3.1

Snippet of HCL2 code causing the unexpected behaviour:

locals {
  foo = var.a ? { id = 1 } : null
}

Expected behavior

I expect to get back a valid hcl expression for local.foo, something like

"${var.a ? {id = 1} : null}"

Actual behavior
but instead hcl2.load gives me:

"${var.a ? {'id': 1} : null}"

Notice how the object is setialized like a python dict, not an hcl object.

Then if I try to use reverse_tranaform on it, I get an exception about an unexpected token for the "'".

Activity

  1. added
    bug
    on Feb 6, 2026
  2. added a commit that references this issue on Feb 6, 2026
    76c2d0b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions