Skip to content

Preserve column order in YAML export (#287) - #644

Merged
claudep merged 1 commit into
jazzband:masterfrom
vineethsaivs:fix/yaml-preserve-column-order
Jun 15, 2026
Merged

Preserve column order in YAML export (#287)#644
claudep merged 1 commit into
jazzband:masterfrom
vineethsaivs:fix/yaml-preserve-column-order

Conversation

@vineethsaivs

Copy link
Copy Markdown
Contributor

Fixes #287.

yaml.safe_dump sorts mapping keys alphabetically by default, so YAML export reordered columns inconsistently with the CSV and JSON exports (which keep the dataset's header order):

import tablib
d = tablib.Dataset()
d.headers = ["name", "id", "zebra", "apple"]
d.append(["x", 1, "z", "a"])

d.csv.splitlines()[0]   # 'name,id,zebra,apple'
d.yaml                  # before: '- {apple: a, id: 1, name: x, zebra: z}'
                        # after:  '- {name: x, id: 1, zebra: z, apple: a}'

This passes sort_keys=False when dumping the dataset and the databook, so YAML keeps the original column order (matching CSV/JSON).

test_yaml_export is updated to expect header order, and a focused regression test (test_yaml_export_preserves_column_order) is added; it fails on master (alphabetical) and passes with this change.


Disclosure: prepared with AI assistance; reviewed by me and I can explain every line.

Comment thread tests/test_tablib.py Outdated
self.assertEqual(output, expected)

def test_yaml_export_preserves_column_order(self):
"""Regression test for #287: YAML export must keep the dataset's column order

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could remove the "Regression test for #287" part, the rest of the patch looks totally good. Thanks!

@codecov

codecov Bot commented Jun 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (7503651) to head (811feda).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #644      +/-   ##
==========================================
+ Coverage   93.14%   93.16%   +0.01%     
==========================================
  Files          29       29              
  Lines        3226     3231       +5     
==========================================
+ Hits         3005     3010       +5     
  Misses        221      221              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hugovk hugovk added the changelog: Fixed For any bug fixes label Jun 14, 2026
yaml.safe_dump sorts mapping keys alphabetically by default, so YAML export
reordered columns (e.g. apple, id, name, zebra) inconsistently with the CSV and
JSON exports, which keep the dataset's header order.

Pass sort_keys=False when dumping the dataset and databook so YAML keeps the
original column order.

Fixes jazzband#287

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vineethsaivs
vineethsaivs force-pushed the fix/yaml-preserve-column-order branch from 6add9c4 to 811feda Compare June 15, 2026 00:10
@vineethsaivs

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I've removed the "Regression test for #287" wording from the docstring; the test now reads as a plain description of the expected behavior.

@claudep claudep left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@claudep
claudep merged commit b8c6606 into jazzband:master Jun 15, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: Fixed For any bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

library re-orders columns when exporting content in YAML

3 participants