Skip to content

Fix write validation against empty transformations#1118

Merged
LucaMarconato merged 3 commits intomainfrom
fix/validate-against-empty-transformations
May 7, 2026
Merged

Fix write validation against empty transformations#1118
LucaMarconato merged 3 commits intomainfrom
fix/validate-against-empty-transformations

Conversation

@LucaMarconato
Copy link
Copy Markdown
Member

@LucaMarconato LucaMarconato commented May 7, 2026

The parsers add transformations to empty elements, but after parsing, if a user calls remove_transformation(element, remove_all=True), the transformations dict is set to {} rather than None, bypassing the is-None guard in all three IO writers.

The fix is general: we now call the validators before writing, and we check the presence of transformations in the validator.

LucaMarconato and others added 2 commits May 7, 2026 10:06
…tions

After remove_transformation(element, remove_all=True) the transformations
dict is set to {} rather than None, bypassing the is-None guard in all three
IO writers. Changed the check to `not transformations` so both None and empty
dicts are caught, and added a parametrized regression test covering images,
multiscale images, labels, multiscale labels, shapes, and points.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Instead of guarding in each IO writer, call get_model(element) (which
already dispatches to the right schema and runs validate()) at the start
of _write_element() for all non-table spatial elements.

Also fix the is-None guards in all three validate() methods to use
`not transformations` / `not data.attrs.get(key)` so that an empty
dict {} is caught in addition to None.

The IO-level guards added in the previous commit are removed since they
are now superseded by the model-level check; assert statements are kept
to narrow the type for mypy.

The regression test is updated to reflect the correct production scenario:
element is already inside a SpatialData object when its transformations are
removed in-place, so the error fires during write() not at construction.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.03%. Comparing base (5ead6cc) to head (8c42cbe).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1118      +/-   ##
==========================================
+ Coverage   91.97%   92.03%   +0.05%     
==========================================
  Files          51       51              
  Lines        7750     7757       +7     
==========================================
+ Hits         7128     7139      +11     
+ Misses        622      618       -4     
Files with missing lines Coverage Δ
src/spatialdata/_core/spatialdata.py 91.95% <100.00%> (+0.02%) ⬆️
src/spatialdata/_io/io_points.py 100.00% <100.00%> (+2.27%) ⬆️
src/spatialdata/_io/io_raster.py 93.22% <100.00%> (+1.04%) ⬆️
src/spatialdata/_io/io_shapes.py 96.15% <100.00%> (+1.21%) ⬆️
src/spatialdata/models/__init__.py 100.00% <ø> (ø)
src/spatialdata/models/models.py 87.97% <100.00%> (+0.16%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Instead of guarding in each IO writer, call validate_element(element)
(a new public helper in spatialdata.models that delegates to get_model)
at the start of _write_element() for all non-table spatial elements.

Validation changes in models.py:
- RasterSchema._check_transforms_present: two explicit checks —
  one for None (key absent) and one for empty dict, with separate messages
- ShapesModel.validate / PointsModel.validate: same split into two checks
- asserts in IO files are kept solely for mypy type-narrowing, each
  annotated with a comment explaining that validate_element() guarantees
  the invariant at runtime

New public API:
- spatialdata.models.validate_element(e) raises ValueError if the element
  fails schema validation; documented in docs/api/models_utils.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@LucaMarconato LucaMarconato marked this pull request as ready for review May 7, 2026 10:31
@LucaMarconato LucaMarconato enabled auto-merge (squash) May 7, 2026 10:32
@LucaMarconato LucaMarconato merged commit ef757d1 into main May 7, 2026
9 checks passed
@LucaMarconato LucaMarconato deleted the fix/validate-against-empty-transformations branch May 7, 2026 10:43
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