Skip to content

reverse_transform not working with variables of type object #231

Closed

Description

@MargaridaMC

Describe the bug
I'm using this great library to parse a terraform script, manipulate it and write it back to a .tf file.
I've noticed that parsing a string and reconstructing it does not return the original value, when we have a variable block of type object.

Software:

  • OS: macOS
  • Python version 3.13.1
  • python-hcl2 version 7.2.0

Snippet of HCL2 code causing the unexpected behaviour:

import hcl2

original_string = """
    variable "object" {
        type = object({
            key   = string
            value = string
        })
    }
    """

parsed_hcl = hcl2.loads(original_string, with_meta=True)
ast = hcl2.reverse_transform(parsed_hcl)
reconstructed_string = hcl2.writes(ast)

print(reconstructed_string)

# Prints:
# variable "object" {
#   type = object({"key":"string","value":"string"})
# }

Expected behavior

One would expect to get back the original value:

 variable "object" {
        type = object({
            key   = string
            value = string
        })
    }

Exception traceback (if applicable):
N/a

Activity

  1. added
    bug
    on May 9, 2025
  2. added
    deserialization
    on Jun 17, 2025
  3. added
    serialization
    and removed
    deserialization
    on Jul 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugserialization

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions