Skip to content

Commit 78118d0

Browse files
refactor: explicit ordered tuple in _classify_patch_error
The previous loop iterated _PatchErrorKind in enum definition order, making the ordering load-bearing but invisible to future maintainers. Replace with an explicit _ordered tuple inside the function. The tuple literal documents that ordering is intentional: more-specific substrings must precede any shorter ones that are a prefix of them. Adding a new kind now requires a deliberate insertion at the right position, not just a new enum member.
1 parent 0065ec2 commit 78118d0

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

src/yamltrip/document.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,10 +651,22 @@ def _classify_patch_error(err: PatchError) -> _PatchErrorKind:
651651
652652
All yamlpatch error-message substrings are confined here so that
653653
callers can branch on the enum rather than matching raw strings.
654+
655+
The tuple is ordered explicitly. More specific substrings must appear
656+
before any that are a prefix of them (e.g. EXPECTED_MAPPING before a
657+
hypothetical EXPECTED) so that substring matching is unambiguous.
654658
"""
655659
msg = str(err)
656-
for kind in _PatchErrorKind:
657-
if kind.value and kind.value in msg:
660+
_ordered = (
661+
_PatchErrorKind.FLOW_SEQUENCE,
662+
_PatchErrorKind.NOT_A_SEQUENCE,
663+
_PatchErrorKind.BLOCK_SEQUENCE_EXPECTED,
664+
_PatchErrorKind.NON_MAPPING_ROUTE,
665+
_PatchErrorKind.EXPECTED_MAPPING,
666+
_PatchErrorKind.UNEXPECTED_NODE,
667+
)
668+
for kind in _ordered:
669+
if kind.value in msg:
658670
return kind
659671
return _PatchErrorKind.UNKNOWN
660672

0 commit comments

Comments
 (0)