python-hl7 icon indicating copy to clipboard operation
python-hl7 copied to clipboard

Inconsistent support for flat Segment sequences within Message

Open abkslm opened this issue 5 months ago • 0 comments

When instantiating a Message, a sequence can be passed to the constructor, but this sequence must be formed in a very specific way, else the Message instance is functionally unusable (though will still be converted to a valid HL7 message when called within str(), and will still be accepted by remote MLLP systems when sent via the built-in client).

It appears that the sequence format that the Message expects to hold would be of/similar to this format :

Message(
	sequence=[
		Segment(
			sequence=[
				Field(
					sequence=[str]
					# OR
					[Repetition(
						sequence=[
							Component(sequence=[str])
						]
					)]
				)
			]
		)
	]
)						

Intuitively, it should be possible to pass a list[str] to Segment(sequence=) in the same way that it can be with Field() (especially with segments like “MSH” where nearly all fields are typically single-level). However, if a list[str] is passed to Segment instead of a list[Field], several of the Message class’s methods break—they appear to be reliant on Message.segments(), which expects the internal sequence be represented as a list[list[…]]. Message.extract_field() is an example of this; interestingly, Segment.extract_field() does work regardless of how the Segment is instatiated.

It would be strongly preferred if:

  • Constructors for all Container subclasses above the lowest level detect if a list[str] is passed in sequence=, then automatically parse to a compatible structure (much safer).
  • Methods like Message.segments() are adjusted to remove reliance on nested sequences.

abkslm avatar Jul 24 '25 21:07 abkslm