Skip to content

Allow assign_field() to descend into a simple (non-repeating) field (#55)#86

Closed
CedricConday wants to merge 1 commit into
johnpaulett:mainfrom
CedricConday:fix/55-assign-field-into-simple-field
Closed

Allow assign_field() to descend into a simple (non-repeating) field (#55)#86
CedricConday wants to merge 1 commit into
johnpaulett:mainfrom
CedricConday:fix/55-assign-field-into-simple-field

Conversation

@CedricConday

Copy link
Copy Markdown

Addresses item 1 of #55

msg = hl7.parse('MSH|^~\\&|field|rep1~rep2|')
seg = msg.segments('MSH')[0]
seg.assign_field('NewRep', 3, 1)
# TypeError: 'str' object does not support item assignment

Cause

A field parsed as a single value is stored as a bare str (e.g. field 3 is Field(['field'])), whereas a repeating field holds Repetition objects. When assign_field() addresses a repetition (or component/subcomponent) of a simple field, field(repeat_num) returns that bare string and the subsequent repetition[:] = [value] slice-assignment raises.

Fix

Before descending, promote a bare string to a Repetition (and, one level down, a single-component repetition to a Component), preserving the existing value — which is the behaviour the issue asks for. Assigning the whole field/repetition still replaces it as before.

Tests

Added test_assign_deeper_into_simple_field covering the reported repetition case plus the component and subcomponent levels:

  • assign_field('NewRep', 3, 1) → field 3 = NewRep
  • assign_field('X', 3, 1, 2) → field 3 = field^X
  • assign_field('Y', 4, 1, 1, 2) → field 4 = rep1&Y~rep2

These fail on main (TypeError) and pass with the fix. Full suite stays green (101 tests); the existing test_assign (assignment into empty fields) is unaffected.

…ohnpaulett#55)

A field parsed as a single value is stored as a bare string, so
assign_field() addressing a repetition/component/subcomponent of it hit
'TypeError: str object does not support item assignment' when it tried to
slice-assign into that string.

Promote a bare string to a Repetition (and likewise a single-component
repetition to a Component) before descending, preserving the existing
value. Addresses item 1 of johnpaulett#55.
@CedricConday CedricConday force-pushed the fix/55-assign-field-into-simple-field branch from adf6ed2 to 4f280eb Compare June 30, 2026 03:49
@CedricConday CedricConday deleted the fix/55-assign-field-into-simple-field branch July 1, 2026 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant