Skip to content

Refactor MetaData class: reduce duplication and improve code quality#52

Merged
pnorton-usgs merged 6 commits into
developmentfrom
refactor/metadata-reduce-duplication
May 11, 2026
Merged

Refactor MetaData class: reduce duplication and improve code quality#52
pnorton-usgs merged 6 commits into
developmentfrom
refactor/metadata-reduce-duplication

Conversation

@pnorton-usgs
Copy link
Copy Markdown
Member

Refactor MetaData class: reduce duplication and improve code quality

Summary

Major refactoring of metadata.py to reduce code duplication, modernize type hints, fix a latent bug, and improve readability.

Changes

Reduce duplication (largest change)

  • Extract __filter_by_version() helper for version/deprecation filtering logic that was repeated in 5 of 6 parsing methods
  • Extract __extract_common() helper for dimensions/modules/requires extraction repeated in 4 methods
  • Extract __extract_valid_values() helper for valid values parsing repeated in 2 methods
  • Remove ~40 lines of commented-out dead code
  • Net reduction of ~90 lines while preserving identical behavior

Modernize type hints

  • Add from __future__ import annotations
  • Replace typing.Dict with built-in dict
  • Replace Union[str, Version] with str | Version
  • Remove unused typing imports

Replace implicit exception handling with explicit None checks

  • Add __find_text() helper that safely returns element text or None
  • Replace all elem.find(ev).text patterns wrapped in try/except AttributeError with explicit if text is None checks
  • Makes control flow clearer and easier to reason about

Fix is_fixed bool parsing

  • Replace bool() with a lambda that checks for '1', 'True', 'true'
  • bool('0') and bool('False') both return True in Python, which would be incorrect if the XML ever uses those values
  • Defensive fix (current XML only uses '1' for fixed dimensions)

Minor cleanup

  • Remove unnecessary # type: ignore on xml.etree.ElementTree import
  • Add __repr__ method for debugging
  • Move outside_elem module-level dict to _OUTSIDE_ELEM class constant
  • Move NEW_DTYPE and NEW_PARAM_DTYPE dicts to constants.py alongside other type-mapping dicts
  • Add proper Sphinx-style docstring to __init__ and metadata property

Testing

All 290 tests pass. Metadata output verified structurally identical across versions 4, 5.2.1.1, 6.0.0, and 60.0.

- Extract __filter_by_version() helper for version/deprecation filtering
  logic that was repeated in 5 of 6 parsing methods
- Extract __extract_common() helper for dimensions/modules/requires
  extraction repeated in 4 methods
- Extract __extract_valid_values() helper for valid values parsing
  repeated in 2 methods
- Remove ~40 lines of commented-out dead code
- No behavioral changes; all metadata output is structurally identical
- Add 'from __future__ import annotations' for runtime compatibility
- Replace typing.Dict with built-in dict
- Replace Union[str, Version] with str | Version
- Remove unused typing imports (Dict, Optional, Union)
- Remove unnecessary '# type: ignore' on xml.etree.ElementTree import
- Add __repr__ method for debugging
- Move outside_elem module-level dict to _OUTSIDE_ELEM class constant
  for better encapsulation (was only used internally)
- Add __find_text() helper that safely returns element text or None
- Replace all 'elem.find(ev).text' patterns wrapped in try/except
  AttributeError with explicit None checks using __find_text()
- Makes control flow clearer: missing elements are handled with
  'if text is None' rather than relying on exception handling
- No behavioral changes
- Replace bool() with a lambda that checks for '1', 'True', 'true'
- bool('0') and bool('False') both return True in Python, which would
  be incorrect if the XML ever uses those values
- Current XML only uses '1' for fixed dimensions and omits the element
  otherwise, so this is a defensive fix
- Move NEW_DTYPE and NEW_PARAM_DTYPE from metadata.py to constants.py
  alongside the other type-mapping dicts (NEW_PTYPE_TO_DTYPE, etc.)
- Add proper Sphinx-style docstring to __init__
- Add docstring to metadata property
@pnorton-usgs pnorton-usgs self-assigned this May 11, 2026
@pnorton-usgs pnorton-usgs merged commit 6636cef into development May 11, 2026
7 checks passed
@pnorton-usgs pnorton-usgs deleted the refactor/metadata-reduce-duplication branch May 11, 2026 16:54
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.

1 participant