One function

write_union in fastavro/fastavro

The author described this change as fix bug in union writing with tuple notation. It counts as a record because the check below fails on the code as it stood at a6f182524 and passes on 0e8fa9051, with nothing else changed between the two runs.

Fix saved2021-02-14
Sharing licenceMIT · LICENSE
Change size+3 2

What the code was meant to do, written into the code itself as a docstring

A union is encoded by first writing a long value indicating the zero-based position within the union of the schema of its value. The value is then encoded per the indicated schema within the union.

The change

77 if isinstance(datum, tuple):
88 (name, datum) = datum
99 for index, candidate in enumerate(schema):
10- if extract_record_type(candidate) == "record":
10+ extracted_type = extract_record_type(candidate)
11+ if extracted_type == "record":
1112 schema_name = candidate["name"]
1213 else:
13- schema_name = candidate
14+ schema_name = extracted_type
1415 if name == schema_name:
1516 best_match_index = index
1617 break

The check that tells the two apart

failpass·tests/test_fastavro.py::test_tuple_writer_picks_correct_union_path

Check file tests/test_fastavro.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before ita6f18252415f23ad1ce06ce019ac2f1d9feef0aa
Broken version dated2021-02-06
Modulefastavro._write_py
Units changedwrite_union
Fingerprint59d4ab4c6d099d0f
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in fastavro/fastavro