refactor(reporting): replace try-except with style checks in footnote generation#793
Merged
chrismaddalena merged 1 commit intoGhostManager:masterfrom Jan 12, 2026
Merged
Conversation
… generation Replace try-except blocks with explicit style existence checks when applying Word footnote styles. This makes the code more readable and eliminates exception handling overhead for the common case. Signed-off-by: marc fuller <[email protected]>
1dbf19d to
ac6ed6f
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #793 +/- ##
=======================================
Coverage 91.43% 91.43%
=======================================
Files 368 368
Lines 20924 20924
=======================================
Hits 19131 19131
Misses 1793 1793 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
chrismaddalena
approved these changes
Jan 12, 2026
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.
Issue
N/A
Description of the Change
This change refactors the footnote generation code in
ghostwriter/modules/reportwriter/richtext/docx.pyto replace try-except blocks with explicit style existence checks.Key changes:
"Footnote Text" in self.doc.stylesand"Footnote Reference" in self.doc.stylesThe refactored code checks whether the "Footnote Text" and "Footnote Reference" styles exist in the Word template before attempting to apply them. If the styles exist, they are applied directly. If not, the code uses fallback XML-based formatting with default values (single line spacing, 10pt font, superscript reference numbers).
Alternate Designs
Try-except approach (previous implementation): The original code attempted to apply styles and caught KeyError exceptions when styles were missing. While this works, it uses exceptions for control flow, which is generally considered less readable and has slight performance overhead.
Pre-check approach (current implementation): The new code checks for style existence using membership testing (
in self.doc.styles) before attempting to apply styles. This makes the intent clearer and eliminates exception handling overhead for the common case.Possible Drawbacks
Verification Process
Testing performed:
Ran all existing footnote tests to verify no regressions:
docker compose -f local.yml run django coverage run manage.py test ghostwriter.modules.reportwriter.tests.test_richtext.test_docx -v 2Result: All 5 footnote tests passed
Verified the code handles both cases:
Confirmed no changes to generated DOCX output or footnote appearance
Release Notes
Refactored footnote generation to use explicit style checks instead of try-except blocks, improving code readability and maintainability without changing functionality.