Replace __getattr__ with explicit collection interface in Dimensions#49
Merged
pnorton-usgs merged 1 commit intodevelopmentfrom May 8, 2026
Merged
Conversation
- 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__
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.
Replace getattr with explicit collection interface in Dimensions
Summary
Removes the implicit
__getattr__delegation to the internal dict inDimensionsand 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 underlyingdict, which:.pop(),.clear(),.update()) that bypass validation logicdict, notDimensions)__setstate__guard to avoid pickling issuesChanges
__getattr__delegation to internalself.__dimensionsdict__contains__— enables'nhru' in dimssyntax__iter__— enablesfor name in dims:iteration__len__— enableslen(dims)keys(),values(),items()methods preserving the existing read-only API used throughout the codebaseexists()to use__contains__internallyndimproperty to use__len__internallyTesting
All 290 tests pass. No behavioral changes for existing callers —
dims.keys(),dims.values(), anddims.items()continue to work as before.