One function

write_record in fastavro/fastavro

The author described this change as fix bug where strict options were not catching extra fields. It counts as a record because the check below fails on the code as it stood at 073da50af and passes on f8979588a, with nothing else changed between the two runs.

Fix saved2022-09-09
Sharing licenceMIT · LICENSE
Change size+3 6

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

A record is encoded by encoding the values of its fields in the order that they are declared. In other words, a record is encoded as just the concatenation of the encodings of its fields. Field values are encoded per their schema.

The change

33 that they are declared. In other words, a record is encoded as just the
44 concatenation of the encodings of its fields. Field values are encoded per
55 their schema."""
6- if (options.get("strict") or options.get("strict_allow_default")) and len(
7- datum
8- ) > len(schema["fields"]):
9- field_names = [field["name"] for field in schema["fields"]]
10- extras = ", ".join(set(datum) - set(field_names))
6+ extras = set(datum) - set(field["name"] for field in schema["fields"])
7+ if (options.get("strict") or options.get("strict_allow_default")) and extras:
118 raise ValueError(
12- f"record contains more fields than the schema specifies: {extras}"
9+ f'record contains more fields than the schema specifies: {", ".join(extras)}'
1310 )
1411 for field in schema["fields"]:
1512 name = field["name"]

The check that tells the two apart

failpass·tests/test_fastavro.py::test_strict_allow_default_bug

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 it073da50afaec729f66caea357538417118ae7619
Broken version dated2022-08-29
Modulefastavro._write_py
Units changedwrite_record
Fingerprintccd8df198d76624a
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