Skip to content

Commit 95f07eb

Browse files
jtdubclaude
andauthored
Add Literal type constraint for cisco_style_text() style parameter (#189) (#240)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent cdda0b3 commit 95f07eb

6 files changed

Lines changed: 26 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Added
1313

14+
- `TextStyle` type alias (`Literal["without_comments", "merged", "with_comments"]`) for
15+
the `style` parameter on `HConfigChild.cisco_style_text()` and
16+
`RemediationReporter.to_text()`, replacing the unconstrained `str` type (#189).
17+
1418
- Performance benchmarks for parsing, remediation, and iteration (#202).
1519
Skipped by default; run with `poetry run pytest -m benchmark -v -s`.
1620

hier_config/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
get_hconfig_from_dump,
77
get_hconfig_view,
88
)
9-
from .models import ChangeDetail, MatchRule, Platform, ReportSummary, TagRule
9+
from .models import ChangeDetail, MatchRule, Platform, ReportSummary, TagRule, TextStyle
1010
from .reporting import RemediationReporter
1111
from .root import HConfig
1212
from .workflows import WorkflowRemediation
@@ -20,6 +20,7 @@
2020
"RemediationReporter",
2121
"ReportSummary",
2222
"TagRule",
23+
"TextStyle",
2324
"WorkflowRemediation",
2425
"get_hconfig",
2526
"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
@@ -181,7 +181,7 @@ def path(self) -> Iterator[str]:
181181

182182
def cisco_style_text(
183183
self,
184-
style: str = "without_comments",
184+
style: TextStyle = "without_comments",
185185
tag: str | None = None,
186186
) -> str:
187187
"""Return a Cisco style formated 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/test_hier_config.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,3 +2187,16 @@ def test_children_extend() -> None:
21872187
assert len(interface1.children) == 2
21882188
assert "description test" in interface1.children
21892189
assert "ip address 192.0.2.1 255.255.255.0" in interface1.children
2190+
2191+
2192+
def test_cisco_style_text_literal_styles(platform_a: Platform) -> None:
2193+
"""Verify cisco_style_text works with each valid TextStyle literal value (#189)."""
2194+
config = get_hconfig(platform_a)
2195+
child = config.add_child("interface Vlan2")
2196+
child.add_child("ip address 10.0.0.1 255.255.255.0")
2197+
2198+
# Each valid style should produce a non-empty string without raising
2199+
for style in ("without_comments", "merged", "with_comments"):
2200+
result = child.cisco_style_text(style=style)
2201+
assert isinstance(result, str)
2202+
assert "interface Vlan2" in result

0 commit comments

Comments
 (0)