Skip to content

Commit aefb3b8

Browse files
jtdubclaude
andauthored
Add Literal type constraint for indented_text() style parameter (#189) (#241)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1568211 commit aefb3b8

6 files changed

Lines changed: 36 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121

2222
### Changed
2323

24+
- Changed `style` parameter on `indented_text()` and `RemediationReporter.to_text()` from `str` to `Literal["without_comments", "merged", "with_comments"]` via new `TextStyle` type alias (#189).
2425
- Renamed `load_hconfig_v2_options` to `load_driver_rules` (#221).
2526
- Renamed `load_hconfig_v2_tags` to `load_tag_rules` (#221).
2627
- Renamed `tags_add()`/`tags_remove()` to `add_tags()`/`remove_tags()` (#216).

hier_config/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
IncompatibleDriverError,
1414
InvalidConfigError,
1515
)
16-
from .models import ChangeDetail, MatchRule, Platform, ReportSummary, TagRule
16+
from .models import ChangeDetail, MatchRule, Platform, ReportSummary, TagRule, TextStyle
1717
from .reporting import RemediationReporter
1818
from .root import HConfig
1919
from .workflows import WorkflowRemediation
@@ -32,6 +32,7 @@
3232
"RemediationReporter",
3333
"ReportSummary",
3434
"TagRule",
35+
"TextStyle",
3536
"WorkflowRemediation",
3637
"get_hconfig",
3738
"get_hconfig_driver",

hier_config/child.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import TYPE_CHECKING, Any
77

88
from .base import HConfigBase
9-
from .models import Instance, MatchRule, SetLikeOfStr
9+
from .models import Instance, MatchRule, SetLikeOfStr, TextStyle
1010

1111
if TYPE_CHECKING:
1212
from collections.abc import Iterable, Iterator
@@ -180,7 +180,7 @@ def path(self) -> Iterator[str]:
180180

181181
def indented_text(
182182
self,
183-
style: str = "without_comments",
183+
style: TextStyle = "without_comments",
184184
tag: str | None = None,
185185
) -> str:
186186
"""Return an indented text line i.e. indentation_level + text ! comments."""

hier_config/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from enum import Enum, auto
2+
from typing import Literal
23

34
from pydantic import BaseModel as PydanticBaseModel
45
from pydantic import ConfigDict, NonNegativeInt, PositiveInt
56

7+
TextStyle = Literal["without_comments", "merged", "with_comments"]
8+
69

710
class BaseModel(PydanticBaseModel):
811
"""Pydantic.BaseModel with a safe config applied."""

hier_config/reporting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from typing import Any
1515

1616
from hier_config.child import HConfigChild
17-
from hier_config.models import ChangeDetail, ReportSummary, TagRule
17+
from hier_config.models import ChangeDetail, ReportSummary, TagRule, TextStyle
1818
from hier_config.root import HConfig
1919

2020

@@ -637,7 +637,7 @@ def to_text(
637637
self,
638638
file_path: str | Path,
639639
*,
640-
style: str = "merged",
640+
style: TextStyle = "merged",
641641
include_tags: Iterable[str] = (),
642642
exclude_tags: Iterable[str] = (),
643643
) -> None:

tests/unit/test_child.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,3 +1255,29 @@ def test_child_dict_key_lookup_with_order_weight() -> None:
12551255
lookup: dict[HConfigChild, str] = {child1: "found"}
12561256
# child2 is equal to child1, so it must find the same dict entry
12571257
assert lookup[child2] == "found"
1258+
1259+
1260+
def test_indented_text_style_literal_values() -> None:
1261+
"""Test that indented_text accepts each valid TextStyle literal value."""
1262+
platform = Platform.CISCO_IOS
1263+
config = get_hconfig(platform)
1264+
child = config.add_child("interface GigabitEthernet0/0")
1265+
child.comments.add("a comment")
1266+
1267+
# without_comments: should NOT include the comment
1268+
result_without = child.indented_text(style="without_comments")
1269+
assert "interface GigabitEthernet0/0" in result_without
1270+
assert "!" not in result_without
1271+
1272+
# with_comments: should include the comment
1273+
result_with = child.indented_text(style="with_comments")
1274+
assert "interface GigabitEthernet0/0" in result_with
1275+
assert "!a comment" in result_with
1276+
1277+
# merged: should include instance count info
1278+
instance = Instance(
1279+
id=1, comments=frozenset(["inst comment"]), tags=frozenset(["tag1"])
1280+
)
1281+
child.instances.append(instance)
1282+
result_merged = child.indented_text(style="merged")
1283+
assert "1 instance" in result_merged

0 commit comments

Comments
 (0)