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.
Fix Markdown export under Python optimized mode (-O)
Context
Issue: docling-project/docling-core#460
Markdown export could crash when running Python with optimizations enabled (e.g.
python -OorPYTHONOPTIMIZE=1).Symptom
UnboundLocalError: cannot access local variable 'list_group' where it is not associated with a valueThis surfaced during Markdown serialization of list items.
Root cause
In
docling_core/transforms/serializer/markdown.py(Markdown list item serialization),list_groupwas assigned via the walrus operator inside anassert:assert item.parent and isinstance((list_group := item.parent.resolve(doc)), ListGroup)Under
-O, Python stripsassertstatements entirely, so the assignment never executes. Since Python still treatslist_groupas a local variable in the function, later references tolist_groupraiseUnboundLocalError.(Conceptually the same as Ruff rule RUF018: assignment in assert.)
Fix
assert.list_groupexplicitly (outside ofassert) and guard access with anis not Nonecheck.ListGroup, fall back to the safe Markdown marker-rather than crashing.This preserves existing behavior for valid documents and prevents optimized-mode crashes.
Tests
test/test_markdown_optimized_mode.pywhich spawns a subprocess withsys.executable -Oand callsDoclingDocument.export_to_markdown()on a minimal document that triggers list marker normalization.Notes