Convert the if elif for to hcl to switch case#54
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the to_hcl function in terracumber/tfvars_cleaner.py by converting an if-elif-else chain to Python's structural pattern matching (match-case statement), which was introduced in Python 3.10. The refactoring modernizes the code while maintaining functional equivalence.
Changes:
- Converted if-elif-else type checking chain to match-case statement using structural pattern matching
- Updated the function docstring to reflect the use of structural pattern matching
- Removed some inline comments during the refactoring
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if isinstance(value, dict): | ||
| lines.append(f"{indent}{key} = {{\n{formatted_value}\n{indent}}}") | ||
| else: | ||
| lines.append(f"{indent}{key} = {formatted_value}") |
There was a problem hiding this comment.
The helpful comment explaining the dict block formatting logic was removed during refactoring. Consider preserving the comment 'If the value is a dictionary, we format it as a block: key = { ... }' as it explains the formatting decision, which may not be immediately obvious to future maintainers.
| for key, value in obj.items(): | ||
| # Recursively format the value | ||
| formatted_value = to_hcl(value, indent_level + 1) | ||
| match obj: |
There was a problem hiding this comment.
The use of structural pattern matching (match-case) requires Python 3.10 or later. Consider adding a python_requires='>=3.10' constraint in setup.py to prevent installation on incompatible Python versions, as the codebase now depends on this feature.
What does this PR?
Code improvement of to hcl.
Still need to be verified