Skip to content

Write tuples, sets, missing, and rationals as YAML values - #273

Merged
quinnj-bot[bot] merged 2 commits into
masterfrom
claude/write-tuples-sets-missing-rationals
Nov 18, 2026
Merged

quinnj-bot[bot] merged 2 commits into
masterfrom
claude/write-tuples-sets-missing-rationals

Conversation

@quinnj-bot

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

Copy link
Copy Markdown
Contributor

Why

YAML.write printed tuples, sets, missing, and rationals as Julia source text, which loads back as a string. YAML.yaml((1, 2)) gave (1, 2), which loads as the string "(1, 2)". A tuple holding "a: b" gave (1, "a: b"), which loads as the mapping Dict("(1, \"a" => "b\")"). missing loaded as the string "missing", and 1//3 as the string "1//3".

What changed

  • Tuples and sets are written as sequences. They load back as vectors. A set is written in its iteration order.
  • missing is written as null (~), like nothing, both as a value and as a key. It loads back as nothing.
  • A rational is written as the nearest float, through the existing float writer. 1//4 loads as 0.25, and 1//0 is written as .inf.
  • The README's writing section says how these types are written.

The writer recognized sequences only as AbstractVector, in the vector method and in the two checks that put a nested collection on its own line. A Sequence union now names the types written as sequences. Both checks use it, and tuples and sets are collected into a vector before the vector writer runs.

Why a float for rationals

YAML has no rational type. A float loads as a number in YAML.jl and in every other reader. The two lossless options load as something that is not a number. One is the string "1//3", which master already produced. The other is a {num: 1, den: 3} mapping, which is what JSON.jl's struct fallback writes. The cost is precision: 1//3 comes back as 0.3333333333333333. rationalize recovers small ratios.

Scope

  • Named tuples are still printed as Julia text. A mapping is the natural form, and it is left for a separate change.
  • Keys that are tuples, sets, or rationals still go through string(key).

Conflict with #264

#264 widens the same two checks to AbstractArray. Whichever PR merges second needs a two-line conflict resolution. Keep Sequence in both checks, and change the definition to const Sequence = Union{AbstractArray,Tuple,AbstractSet}. Keep the vector method on AbstractVector, so #264's AbstractArray method still handles matrices. I checked that resolution locally: #264's issue #135 testset and this PR's testset both pass, and Dict("t" => ([1 2; 3 4], Set([1]))) round-trips.

Verification

  • The new testset fails on master (24 failures and 1 error in 25) and passes here (25 of 25).
  • Full Pkg.test() passes on Julia 1.12.6 and on Julia 1.0.5 (Docker).
  • I wrote 10 values in 3 positions each (top level, dict value, list item), including nested tuples, a set of tuples, and missing inside a dict inside a tuple. All 30 outputs load as the expected values in YAML.jl, Ruby Psych, yq, eemeli yaml, and js-yaml. On master, all five parsers get 0 of 30.
  • Test.detect_ambiguities(YAML) reports 1 ambiguity, the same one master has.
  • Self-review: the new _print methods take Union{Tuple,AbstractSet}, Missing, and Rational. None overlaps an existing method, and Pair is not a Tuple.

🤖 Generated with Claude Code

quinnj and others added 2 commits November 18, 2026 03:15
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The writer printed these with string(), so (1, 2) became the text
"(1, 2)" and loaded back as a string, and a tuple holding "a: b"
loaded as a mapping. Tuples and sets now go through the sequence
writer, missing is written as null like nothing (as a value or a key),
and a rational is written as the nearest float.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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 91.36%. Comparing base (e9e9978) to head (f328d70).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #273      +/-   ##
==========================================
+ Coverage   91.33%   91.36%   +0.02%     
==========================================
  Files          15       15              
  Lines        1709     1713       +4     
==========================================
+ Hits         1561     1565       +4     
  Misses        148      148              

☔ 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-bot
quinnj-bot Bot merged commit 1abd68f into master Nov 18, 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.

1 participant