Add flat BaseChart transformation fields - #555
Closed
palewire wants to merge 1 commit into
Closed
Conversation
Collaborator
Author
|
This is too complicated. I don't want to do it. |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an ergonomic “flat” set of transformation fields on BaseChart (e.g., transpose, column_order, column_format, changes, external data settings) that transparently merge into the existing nested Transform model and serialize into the same metadata.data payload shape expected by the Datawrapper API.
Changes:
- Introduces top-level,
exclude=TruePydantic fields onBaseChartfor common transformation settings and merges them intotransformations(with conflict detection). - Updates serialization to always emit the merged transformation state under
metadata.data. - Adds documentation and unit tests demonstrating flat vs nested transformation usage and conflict behavior.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
datawrapper/charts/base.py |
Adds flat transformation fields and implements merge + conflict validation into the nested Transform before serialization. |
tests/unit/serializers/test_base_transformations.py |
Adds unit tests for flat field serialization, composition with nested Transform, assignment behavior, and conflict validation. |
docs/user-guide/charts/line-charts.md |
Updates examples to use the new high-level column_format shortcut; minor formatting cleanup. |
docs/user-guide/charts/area-charts.md |
Updates example to use transpose=True shortcut instead of nested transformations dict. |
docs/user-guide/advanced/transformations.md |
Adds a dedicated guide explaining flat transformation fields, Transform, and conflict behavior. |
docs/index.md |
Adds the new transformations guide to the documentation index. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+548
to
+554
| def _is_empty_transformation_value(field_name: str, value: Any) -> bool: | ||
| """Return whether a nested transform value represents its unset default.""" | ||
| if field_name in {"column_order", "column_format", "changes"}: | ||
| return not bool(value) | ||
| if field_name in {"external_data"}: | ||
| return value == "" | ||
| return False |
Comment on lines
+99
to
+103
| assert data["column-order"] == [0, 1, 2] | ||
| assert data["column-format"] == {"sales": {"type": "number"}} | ||
|
|
||
|
|
||
| def test_matching_flat_and_nested_transformation_values_are_accepted(): |
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.
Summary
BaseCharttransformation fields (transpose,column_order,column_format,changes, external-data settings, etc.) as a narrow ergonomic layer over the existingTransformmodel.transformations=API intact and merges flat fields into the samemetadata.dataserialization shape Datawrapper expects.dw.Transform(...).Compatibility design
The nested
transformationsmodel remains the source of truth for API-shaped data and round trips. Flat fields areexclude=TruePydantic fields, somodel_dump()does not introduce a second representation; construction and assignment merge them intochart.transformations, andserialize_model()writes the merged result undermetadata.data.Supported flat fields are intentionally limited to the current
Transformmodel:transpose,vertical_header,horizontal_header,column_order,column_format,changes,external_data,use_datawrapper_cdn, andupload_method.Closes #499.
Validation
uv run pytest tests/unit/serializers/test_base_transformations.py -quv run pytest tests/unit/serializers/test_base_transformations.py tests/unit/models/test_transform.py tests/unit/serializers/test_base_serializer.py tests/integration/test_base_metadata_structure.py -quv run ruff check ./datawrapper ./testsuv run ruff format --check ./datawrapper ./testsuv run mypy ./datawrapper --ignore-missing-importsuv run pre-commit run --all-filesuv run pytest— 1127 passed, 3 skippeduv build --sdist --wheel