Skip to content

force_operation_parentheses adds no parentheses inside an already-parenthesized expression #342

Description

@livingstaccato

Summary

ExprTermRule.serialize sets inside_parentheses to
self.parentheses or context.inside_parentheses
(hcl2/rules/expressions.py:103 at v8.1.3), so the flag stays set for the
whole subtree. _wrap_into_parentheses (:37) reads it at :47 as "my
immediate parent already wrapped me" and declines to wrap. Anything under a parenthesized
ancestor therefore loses the parentheses the option exists to add.

Reproduction

options = SerializationOptions(force_operation_parentheses=True)

hcl2.loads("a = b + c * d\n",   serialization_options=options)["a"]  # '${b + (c * d)}'
hcl2.loads("a = (b + c * d)\n", serialization_options=options)["a"]  # '${(b + c * d)}'

The second should contain (c * d) as the first does.

Suggested shape of the fix

Drop or context.inside_parentheses. Each inner ExprTermRule re-sets the
flag from its own self.parentheses, so ((b + c) * d) still comes out right.


This issue, and the investigation behind it, were produced by an AI assistant (Claude) working on behalf of the author. Please review with that provenance in mind.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions