Skip to content

Replace __getattr__ with explicit collection interface in Dimensions#49

Merged
pnorton-usgs merged 1 commit intodevelopmentfrom
refactor/dimensions-explicit-collection-interface
May 8, 2026
Merged

Replace __getattr__ with explicit collection interface in Dimensions#49
pnorton-usgs merged 1 commit intodevelopmentfrom
refactor/dimensions-explicit-collection-interface

Conversation

@pnorton-usgs
Copy link
Copy Markdown
Member

Replace getattr with explicit collection interface in Dimensions

Summary

Removes the implicit __getattr__ delegation to the internal dict in Dimensions and replaces it with explicit, documented collection methods. This makes the class interface discoverable, type-checkable, and prevents accidental mutation of internal state.

Motivation

The previous __getattr__ silently forwarded any unknown attribute access to the underlying dict, which:

  • Exposed mutating methods (.pop(), .clear(), .update()) that bypass validation logic
  • Broke IDE autocomplete and static analysis (mypy couldn't see available methods)
  • Produced confusing error messages on typos (errors came from dict, not Dimensions)
  • Required a special __setstate__ guard to avoid pickling issues

Changes

  • Remove __getattr__ delegation to internal self.__dimensions dict
  • Add __contains__ — enables 'nhru' in dims syntax
  • Add __iter__ — enables for name in dims: iteration
  • Add __len__ — enables len(dims)
  • Add explicit keys(), values(), items() methods preserving the existing read-only API used throughout the codebase
  • Update exists() to use __contains__ internally
  • Update ndim property to use __len__ internally

Testing

All 290 tests pass. No behavioral changes for existing callers — dims.keys(), dims.values(), and dims.items() continue to work as before.

- Remove __getattr__ delegation to internal dict (prevents implicit
  exposure of mutating dict methods, fixes IDE support and pickling)
- Add __contains__ for 'name in dims' syntax
- Add __iter__ for 'for name in dims:' iteration
- Add __len__ for len(dims)
- Add explicit keys(), values(), items() methods to preserve existing
  API used throughout the codebase
- Update exists() to use __contains__
- Update ndim to use __len__
@pnorton-usgs pnorton-usgs self-assigned this May 8, 2026
@pnorton-usgs pnorton-usgs merged commit cf90886 into development May 8, 2026
7 checks passed
@pnorton-usgs pnorton-usgs deleted the refactor/dimensions-explicit-collection-interface branch May 8, 2026 20:00
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