test: enforce pyx12 code prefix matches err_handler tree node type - #181
Merged
Conversation
Add unit-test invariants that pyx12 code prefixes match where the error attaches in the err_handler tree: - ELE_* and COMP_* codes must land in err_ele.errors (composites surface as element-level errors). - SEG_* codes must land in err_seg.errors. Also fix the one place that violated this invariant: the historical PR #161 hardcode in apply_segment_errors that stored a literal "8" in err_seg.errors when collapsing element-level validator errors (too-many-elements, syntax violations, mandatory composite missing, etc.) to a spec-correct IK3 code. Replace with new pyx12 code SEG_8_HAS_DATA_ELEMENT_ERRORS (ak/ik="8"); ack output unchanged because the visitor still resolves to "8" via the table, and the original element-level pyx12 code is preserved in the err_str. New tests: pyx12/test/test_error_codes.py (new file): - TableConsistency.test_every_code_has_a_known_prefix - TableConsistency.test_prefix_matches_spec_level (the main invariant the user asked for: every code's name prefix matches its declared ErrorCodeSpec.level) - TableConsistency.test_key_matches_spec_code - TableConsistency.test_ak_and_ik_codes_when_set_are_short_strings - X12CodeForResolution.test_known_code_resolves_to_ik_code - X12CodeForResolution.test_unknown_code_falls_through (envelope-level codes like "025" passthrough) - X12CodeForResolution.test_ak_preferred_when_ik_is_none pyx12/test/test_x12n_document.py (new ErrorCodePrefixPlacement class): - test_walker_seg_errors_have_seg_prefix (uses bad_2010AA_bug) - test_validator_ele_errors_have_ele_or_comp_prefix (uses 834_lui_id_5010_non_ascii) - test_no_raw_x12_code_leaks_into_seg_or_ele_errors (parameterized across 7 fixtures) Updates: - pyx12/error_codes.py: add SEG_8_HAS_DATA_ELEMENT_ERRORS module constant and ERROR_CODES entry. - pyx12/map_if/_segment.py apply_segment_errors: emit SEG_8_HAS_DATA_ELEMENT_ERRORS instead of literal "8". The original element-level pyx12 code (e.g. SEG_3_TOO_MANY_ELEMENTS, COMP_1_MANDATORY_MISSING) is dropped at this collapse point — by design, per PR #161's spec-correctness routing. - pyx12/test/test_map_if.py: ApplySegmentErrorsRouting test updated to expect SEG_8_HAS_DATA_ELEMENT_ERRORS instead of "8". Verification: - pytest pyx12/test/: 597 passed + 7 subtests (581 + 6 from PR 6 + 10 new from this PR) - mypy --strict pyx12: clean (89 source files; new test_error_codes.py) - ruff check + format --check: clean - res997 / resAck / resJson fixtures unchanged: the apply_segment_errors collapse path is not exercised by any existing fixture (regenerator helper run, no diffs). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
azoner
added a commit
that referenced
this pull request
May 6, 2026
Add a preference: when given a non-trivial task, do not start making changes until you have ~95% confidence in what to build. Ask follow-up questions until that bar is met. Catches scope creep and misalignment early — see PR #181 where I added an apply_segment_errors cleanup the user hadn't asked for, framed as "the only invariant violator", but which was a separate behavior change deserving its own decision. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add unit-test invariants that pyx12 code prefixes match where the error attaches in the err_handler tree:
ELE_*andCOMP_*codes must land inerr_ele.errors(composites surface as element-level errors).SEG_*codes must land inerr_seg.errors.Also fix the one place that violated this invariant: the historical PR #161 hardcode in
apply_segment_errorsthat stored a literal\"8\"inerr_seg.errorswhen collapsing element-level validator errors (too-many-elements, syntax violations, mandatory composite missing) to a spec-correct IK3 code. Replace with new pyx12 codeSEG_8_HAS_DATA_ELEMENT_ERRORS(ak/ik=\"8\"); ack output unchanged because the visitor still resolves to\"8\"via the table, and the original element-level pyx12 code is preserved in the err_str.New tests
pyx12/test/test_error_codes.py(new file):TableConsistency.test_every_code_has_a_known_prefixTableConsistency.test_prefix_matches_spec_level— the main invariant: every code's name prefix matches its declaredErrorCodeSpec.levelTableConsistency.test_key_matches_spec_codeTableConsistency.test_ak_and_ik_codes_when_set_are_short_stringsX12CodeForResolution.test_known_code_resolves_to_ik_codeX12CodeForResolution.test_unknown_code_falls_through— envelope-level codes (\"025\") passthroughX12CodeForResolution.test_ak_preferred_when_ik_is_nonepyx12/test/test_x12n_document.py(newErrorCodePrefixPlacementclass):test_walker_seg_errors_have_seg_prefix— usesbad_2010AA_bugtest_validator_ele_errors_have_ele_or_comp_prefix— uses834_lui_id_5010_non_asciitest_no_raw_x12_code_leaks_into_seg_or_ele_errors— parameterized across 7 fixturesUpdates
pyx12/error_codes.py— newSEG_8_HAS_DATA_ELEMENT_ERRORSconstant + ERROR_CODES entrypyx12/map_if/_segment.pyapply_segment_errors— emitSEG_8_HAS_DATA_ELEMENT_ERRORSinstead of literal\"8\"pyx12/test/test_map_if.py—ApplySegmentErrorsRoutingtest updated to expect the new codeTest plan
test_error_codes.py)apply_segment_errorscollapse path is not exercised by any existing fixture (regenerator helper run, no diffs)🤖 Generated with Claude Code