Make DataArray a descriptor to enforce Data.data type invariant#32
Merged
Make DataArray a descriptor to enforce Data.data type invariant#32
Conversation
Co-authored-by: gb119 <[email protected]> Agent-Logs-Url: https://github.com/stonerlab/Stoner-PythonCode/sessions/e8730203-2d7b-408a-a939-f0e0f2aaba43
- Add __set_name__, __get__, __set__ descriptor methods to DataArray - Replace @Property data/@data.setter in DataFilePropertyMixin with class-level `data = DataArray([])` descriptor assignment - Update isproperty() in tools/tests.py to also recognise data descriptors (objects with __get__ and __set__) not just property instances Co-authored-by: gb119 <[email protected]> Agent-Logs-Url: https://github.com/stonerlab/Stoner-PythonCode/sessions/e8730203-2d7b-408a-a939-f0e0f2aaba43
Copilot
AI
changed the title
[WIP] Refactor Data class to use DataArray as descriptor
Make DataArray a descriptor to enforce Data.data type invariant
Mar 20, 2026
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.
Data.datawas enforced as aDataArrayinstance only via a hand-rolled@propertysetter. ConvertingDataArrayitself into a Python data descriptor moves that responsibility into the type, making the invariant self-enforcing for all subclasses.Changes
Stoner/core/array.py— Added the descriptor protocol toDataArray:__set_name__: records_attr_name = 'data'and_private_name = '_data'when the descriptor is installed on a class__get__: returnsnp.atleast_2d(obj._data)viaobject.__getattribute__(avoids allocating a default on every access)__set__: contains the normalization logic previously in the property setter — 1-D → 2-D column, scalar → 2-D, >2-D raisesValueError, preservessetas/column headers when column count is unchangedStoner/core/property.py— Replaces@property data/@data.setterinDataFilePropertyMixinwith a single class-level descriptor assignment:Stoner/tools/tests.py—isproperty()now recognises data descriptors (any class attribute with both__get__and__set__) in addition topropertyinstances, sinceDataArrayis no longer aproperty.Behaviour
The private
_dataattribute and all directobj._data._setasaccesses throughout the codebase are unaffected.✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.