Skip to content

Write multidimensional arrays as nested sequences - #264

Merged
quinnj merged 4 commits into
masterfrom
claude/write-multidim-arrays
Nov 23, 2026
Merged

quinnj merged 4 commits into
masterfrom
claude/write-multidim-arrays

Conversation

@quinnj-bot

@quinnj-bot quinnj-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Why

YAML.write and YAML.write_file print a matrix in Julia syntax, such as [1 2; 3 4]. Loading that output gives a list that holds one string, so the matrix data is lost. Fixes #135.

What changed

  • A multidimensional array is written as nested sequences along its last dimension. A matrix becomes a sequence of its columns, the same nesting that JSON.jl writes. reduce(hcat, ...) on the loaded value rebuilds the matrix.
  • The mapping and sequence writers put any nonempty array on its own lines, not only vectors.
  • The YAML.write docstring states the layout.
  • The writer's Sequence type list now includes every AbstractArray, so a matrix inside a list, as a mapping value, or as a key starts on its own lines.

Scope

  • Writing only. YAML.load still returns nested vectors, because YAML has no matrix type.
  • Tuples, sets, symbols, chars, and Float32 NaN/Inf still go through the generic string fallback. Those are separate fixes.
  • A zero-dimensional array, such as fill(1), is written as the one value it holds, as JSON.jl does. Before, it was written as the string fill(1).

Tradeoffs

Rows were the other choice ([[1, 2], [3, 4]], as NumPy's tolist gives). Columns match JSON.jl and Julia's column-major storage, and reduce(hcat, ...) rebuilds the matrix without a transpose.

Verification

  • The new issue #135 testset has 4 failures on master and passes with the fix. It covers a matrix as a mapping value, a matrix inside a list, a 3-D array, an empty 0×3 matrix, and zero-dimensional arrays at the top level, as mapping values, and as list elements.
  • The full suite passes on Julia 1.12.6 after the rebase. CI runs it on Julia 1.0, 1.6, 1, and nightly.
  • Adversarial re-read: all three writer entry points (top level, mapping value, list element) dispatch to the new method. An empty but shaped array writes [], so the caller's inline-or-block choice matches the output. axes keeps offset-indexed arrays correct. Vectors and ranges still use the vector method.

Rebased on master, which added Sequence and starts_block to the writer. The conflict is resolved by widening Sequence to AbstractArray.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.40%. Comparing base (23f2449) to head (64586b2).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #264      +/-   ##
==========================================
+ Coverage   92.35%   92.40%   +0.04%     
==========================================
  Files          15       15              
  Lines        1767     1778      +11     
==========================================
+ Hits         1632     1643      +11     
  Misses        135      135              

☔ 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.

quinnj and others added 3 commits November 21, 2026 20:21
YAML.write prints a matrix with Julia's string syntax, so loading the
output gives a one-element list holding a string. The test asserts that
matrices and higher-dimensional arrays load back as nested lists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Arrays with two or more dimensions had no writer method, so they fell
through to the generic fallback, which prints Julia's string syntax such
as `[1 2; 3 4]`. That loads back as a list holding one string.

Write such an array as a sequence of its slices along the last dimension,
so a matrix becomes a sequence of its columns. This matches how JSON.jl
writes arrays, and `reduce(hcat, ...)` rebuilds the matrix. The mapping
and sequence writers now treat any nonempty array as a block collection.

Fixes #135

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A zero-dimensional array has no last dimension to slice along, so the
multidimensional writer threw a BoundsError for values such as fill(1).
Write the one value it holds instead, as JSON.jl does, and decide
inline-or-block layout from that value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quinnj-bot
quinnj-bot Bot force-pushed the claude/write-multidim-arrays branch from ad54e87 to 3758e16 Compare November 22, 2026 03:35
Unwrap scalar array keys before deciding how to quote them. Cover multiline and quoted text, symbols, and matrix keys in the round-trip tests.
@quinnj
quinnj merged commit 684ca3a into master Nov 23, 2026
22 checks passed
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.

2d arrays get converted into string representation by write_file

1 participant