Skip to content

Raise ConstructorError for values that do not fit their explicit tag - #282

Merged
quinnj-bot[bot] merged 3 commits into
masterfrom
claude/wrong-tag-errors
Nov 18, 2026
Merged

quinnj-bot[bot] merged 3 commits into
masterfrom
claude/wrong-tag-errors

Conversation

@quinnj-bot

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

Copy link
Copy Markdown
Contributor

Why

A value whose explicit tag doesn't fit its text throws a Base error with no position:

  • !!bool y throws KeyError.
  • !!int abc, !!float abc, and !!binary 0 throw ArgumentError.
  • !!map x and !!map [1] throw MethodError.
  • !!float text with invalid UTF-8 throws InvalidCharError.

Callers that catch YAML.jl's errors to report bad input miss all of these.

What changed

  • The bool, int, float, and binary constructors now convert their text through one helper, construct_parsed_scalar. When the conversion throws, the helper raises ConstructorError("could not convert \"abc\" to tag:yaml.org,2002:int") at the node.
  • construct_mapping now checks that the node is a mapping and raises ConstructorError otherwise, as construct_sequence and construct_scalar already do. Every mapping path goes through it: the default Dict, a custom dicttype, and !!map.
  • Tests that pinned ArgumentError for !!int 0b102, !!int 0b, !!int 1 2, and the !!float forms rejected by Read only decimal text through the BigFloat fallback for floats #277 now expect ConstructorError.

No issue was filed. Round 4's fuzz found this. !!timestamp is handled separately in the impossible-dates PR.

Verification

  • The new testset "values that do not fit their explicit tag" fails 11 of 11 on master, and passes 11 of 11 here. The four updated assertions fail on master with ArgumentError.
  • The full Pkg.test() passes on Julia 1.12.6 and on Julia 1.0.5 in Docker.
  • Fuzz: 250,000 inputs (seed 5). On master, this group raised 798 errors: 303 ArgumentErrors from binary, 173 MethodErrors from construct_mapping, 120 KeyErrors from bool, 104 ArgumentErrors from parse_int, 97 ArgumentErrors from float, and 1 InvalidCharError. With this fix, there are 0.
  • Performance: I loaded a 1.4 MB document with 20,000 floats and 20,000 bools 7 times per process and took the minimum, alternating master and this branch 3 times each with Julia 1.12.6. The best times were 805.8 ms on master and 785.7 ms here, and the other runs were 810-825 ms on both. That is within the noise of a loaded machine, so the try per scalar costs nothing measurable.
  • Review: the helper catches any exception from the text conversion. The converters are pure functions of the scalar's text, so any failure there means the text doesn't fit the tag. User constructors don't go through the helper, so their exceptions are unchanged. Values that loaded before load the same: the full suite and the spec examples pass.

🤖 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.21%. Comparing base (69f14ef) to head (ad37c11).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #282      +/-   ##
==========================================
+ Coverage   92.17%   92.21%   +0.03%     
==========================================
  Files          15       15              
  Lines        1751     1759       +8     
==========================================
+ Hits         1614     1622       +8     
  Misses        137      137              

☔ 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 18, 2026 07:03
…rError

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A scalar with an explicit tag its text cannot satisfy threw a Base error:
KeyError for '!!bool y', ArgumentError for '!!int abc', '!!float abc',
and '!!binary 0', and InvalidCharError for invalid UTF-8 text. A
collection or scalar tagged '!!map' threw MethodError. The bool, int,
float, and binary constructors now convert their text through one helper
that turns a rejected conversion into a ConstructorError at the node, and
construct_mapping checks the node kind, as construct_sequence and
construct_scalar already do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quinnj-bot
quinnj-bot Bot force-pushed the claude/wrong-tag-errors branch from 487044f to ad37c11 Compare November 18, 2026 14:03
@quinnj-bot
quinnj-bot Bot merged commit 6ccf984 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