Skip to content

fix(critical): resolve three critical runtime bugs in MetadataFigure - #64

Merged
lkdmc merged 1 commit into
mainfrom
fix/critical-bugs
Mar 31, 2026
Merged

fix(critical): resolve three critical runtime bugs in MetadataFigure#64
lkdmc merged 1 commit into
mainfrom
fix/critical-bugs

Conversation

@lkdmc

@lkdmc lkdmc commented Mar 31, 2026

Copy link
Copy Markdown
Collaborator

Summary

Three critical bugs that can crash the build or silently produce wrong output.

1. has_captionNameError when a figure node has no children (__init__.py:779)

has_caption = False was initialised inside the inner for child in node.children: loop body, so it was reset on every iteration and left undefined when node.children is empty. The subsequent if has_caption: then raises NameError. Additionally, the caption node reference (child) after the loop relied on silent loop-variable leakage — fragile and hard to read.

Fix: initialise has_caption = False and caption_child = None before the inner loop; capture the caption node in caption_child and use that variable after the loop.


2. License URL links broken in non-English locales (__init__.py:608, 812, 937)

figure_node["license"] stored the translated display value (e.g. German), but LICENSE_URLS is keyed by English names only. The in LICENSE_URLS look-up therefore always failed for non-English builds, so no license hyperlink was ever generated.

Fix: save the English (pre-translation) key in license_key_en immediately after untranslate_license() and store it as figure_node["license_key"]. _build_attribution_display now uses figure_node.get("license_key", figure_node["license"]) for the URL look-up.


3. ValueError crash in _copyright_from_authoryear (__init__.py:892, 897)

Both datetime.strptime(date_value, "%Y-%m-%d") calls had no exception handling. A date that arrives from a BibTeX year = field (e.g. "2023" or a partial date) and bypasses the directive-level validation crashes the build with an unhandled ValueError.

Fix: wrap both strptime calls in try/except ValueError; when the date cannot be parsed the year is simply omitted from the copyright string rather than raising.

Test plan

  • Build with a non-English Sphinx locale (e.g. language = "de") and confirm license hyperlinks appear in the output HTML.
  • Add a figure with no children (empty figure node) and confirm the build does not raise NameError.
  • Add a BibTeX entry with year = {2023} (no month/day) and use it via :bib:; confirm the build completes and copyright is shown without a year.

https://claude.ai/code/session_01ApsmWPWsKBesMV1rq2VenA

1. has_caption NameError: initialise has_caption and caption_child
   before the inner loop so the variables are always defined even when
   node.children is empty, and stop relying on loop-variable leakage for
   the caption_child reference used after the loop.

2. License URL lookup broken in non-English locales: preserve the
   English (pre-translation) license key in figure_node["license_key"]
   and use it exclusively for LICENSE_URLS lookups in
   _build_attribution_display, so licence hyperlinks are generated
   correctly regardless of the active Sphinx locale.

3. ValueError in _copyright_from_authoryear: wrap both strptime calls
   in try/except ValueError so that a BibTeX date that is not in
   YYYY-MM-DD format (e.g. a bare year from the year= field) does not
   crash the build; the year is simply omitted from the copyright string
   instead.

https://claude.ai/code/session_01ApsmWPWsKBesMV1rq2VenA
@lkdmc
lkdmc requested a review from douden as a code owner March 31, 2026 16:10
@lkdmc
lkdmc merged commit 19ca8e3 into main Mar 31, 2026
3 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