fix: coerce None text to empty string in Text element#4231
Open
themavik wants to merge 1 commit intoUnstructured-IO:mainfrom
Open
fix: coerce None text to empty string in Text element#4231themavik wants to merge 1 commit intoUnstructured-IO:mainfrom
themavik wants to merge 1 commit intoUnstructured-IO:mainfrom
Conversation
When a Picture element detected by yolox has only bounding-box coordinates but no OCR text, the `text` attribute is set to `None`. Normalizing this element to an `Image` (which extends `Text`) preserves the `None` value. Subsequent calls to `str(element)` then return `None` instead of a string, violating the type contract (`text: str`) and causing downstream errors. Coerce `None` to the empty string in `Text.__init__` so that `__str__` always returns a valid string. Fixes Unstructured-IO#4084
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.
Summary
Fixes #4084
When
yoloxdetects a Picture element with bounding-box coordinates but no OCR text, thetextattribute is set toNone. When this element is normalized to anImage(which extendsText), theNonevalue is preserved. Subsequent calls tostr(element)then returnNone(the Python object, not the string"None"), violating the type contract (text: str) and causing errors in downstream processing.Changes
In
Text.__init__, coerceNoneto the empty string:This matches the existing defensive pattern in
Element.__init__which setsself.text = self.text if hasattr(self, "text") else "", and ensures__str__always returns a valid string.Test plan
Imageelement withtext=Noneand verifystr(element)returns""instead ofNonepartition_imagewith the yolox model on images that produce Picture annotations without text