Skip to content

Make DataArray a descriptor to enforce Data.data type invariant#32

Merged
gb119 merged 3 commits intostablefrom
copilot/stable-make-dataarray-descriptor
Mar 20, 2026
Merged

Make DataArray a descriptor to enforce Data.data type invariant#32
gb119 merged 3 commits intostablefrom
copilot/stable-make-dataarray-descriptor

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 20, 2026

Data.data was enforced as a DataArray instance only via a hand-rolled @property setter. Converting DataArray itself 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 to DataArray:

    • __set_name__: records _attr_name = 'data' and _private_name = '_data' when the descriptor is installed on a class
    • __get__: returns np.atleast_2d(obj._data) via object.__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 raises ValueError, preserves setas/column headers when column count is unchanged
  • Stoner/core/property.py — Replaces @property data / @data.setter in DataFilePropertyMixin with a single class-level descriptor assignment:

    data = DataArray([])
  • Stoner/tools/tests.pyisproperty() now recognises data descriptors (any class attribute with both __get__ and __set__) in addition to property instances, since DataArray is no longer a property.

Behaviour

d = Data()
d.data = np.ones((10, 3))          # plain ndarray → automatically wrapped
assert isinstance(d.data, DataArray)  # always True

d.data = np.arange(5)              # 1-D → stored as (5, 1) column
assert d.data.shape == (5, 1)

d.data = np.ones((3, 3, 3))        # raises ValueError (>2-D)

The private _data attribute and all direct obj._data._setas accesses 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.

Copilot AI and others added 2 commits March 20, 2026 20:36
- 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
Copilot AI requested a review from gb119 March 20, 2026 20:49
@gb119 gb119 changed the base branch from main to stable March 20, 2026 20:53
@gb119 gb119 marked this pull request as ready for review March 20, 2026 21:00
@gb119 gb119 merged commit ddee79d into stable Mar 20, 2026
6 of 10 checks passed
@gb119 gb119 deleted the copilot/stable-make-dataarray-descriptor branch March 20, 2026 21: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.

2 participants