Skip to content

refactor(reporting): replace try-except with style checks in footnote generation#793

Merged
chrismaddalena merged 1 commit intoGhostManager:masterfrom
marcpfuller:refactor_docx
Jan 12, 2026
Merged

refactor(reporting): replace try-except with style checks in footnote generation#793
chrismaddalena merged 1 commit intoGhostManager:masterfrom
marcpfuller:refactor_docx

Conversation

@marcpfuller
Copy link
Contributor

Issue

N/A

Description of the Change

This change refactors the footnote generation code in ghostwriter/modules/reportwriter/richtext/docx.py to replace try-except blocks with explicit style existence checks.

Key changes:

  • Replaced try-except blocks that caught KeyError exceptions with upfront style existence checks using "Footnote Text" in self.doc.styles and "Footnote Reference" in self.doc.styles
  • Simplified the control flow using clear if/else blocks instead of nested exception handling
  • Maintained all existing functionality including fallback formatting when styles are missing
  • Improved code readability by making the intent explicit rather than relying on exception-based flow control

The 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

  • Minimal: The behavior is identical to the previous implementation
  • Slightly more verbose due to explicit checks, but significantly more readable
  • No known performance drawbacks; may actually be slightly faster by avoiding exception handling

Verification Process

Testing performed:

  1. 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 2

    Result: All 5 footnote tests passed

  2. Verified the code handles both cases:

    • Templates with "Footnote Text" and "Footnote Reference" styles → Styles applied correctly
    • Templates without these styles → Fallback formatting applied (10pt font, single spacing, superscript)
  3. 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.

@marcpfuller marcpfuller self-assigned this Jan 9, 2026
… 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]>
@codecov
Copy link

codecov bot commented Jan 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.43%. Comparing base (3843862) to head (ac6ed6f).
⚠️ Report is 2 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@chrismaddalena chrismaddalena merged commit a85b7e9 into GhostManager:master Jan 12, 2026
8 checks passed
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.

2 participants