One function

parse_schema in fastavro/fastavro

The author described this change as 538 fix schema expansion of parsed schemas (#541). It counts as a record because the check below fails on the code as it stood at c3e995721 and passes on 9aa185041, with nothing else changed between the two runs.

Fix saved2021-05-10
Sharing licenceMIT · LICENSE
Change size+4 2

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

Returns a parsed avro schema It is not necessary to call parse_schema but doing so and saving the parsed schema for use later will make future operations faster as the schema will not need to be reparsed. Parameters ---------- schema: dict Input schema named_schemas: dict Dictionary of named schemas to their schema definition expand: bool If true, named schemas will be fully expanded to their true schemas rather than being represented as just the name. This format should be considered an output only and not passed in to other reader/writer functions as it does not conform to the avro specification and will likely cause an exception _write_hint: bool Internal API argument specifying whether or not the __fastavro_parsed marker should be added to the schema _force: bool Internal API argument. If True, the schema will always be parsed even if it has been parsed and has the __fastavro_parsed marker Example:: from fastavro import parse_schema from fastavro import writer parsed_schema = parse_schema(original_schema) with open('weather.avro', 'wb') as out: writer(out, parsed_schema, records) Sometimes you might have two schemas where one schema references another. For the sake of example, let's assume you have a `Parent` schema that references a `Child` schema`. If you were to try to parse the parent schema on its own, you would get an exception because the child schema isn't defined. To accomodate this, we can use the `named_schemas` argument to pass a shared dictionary when parsing both of the schemas. The dictionary will get populated with the necessary schema references to make parsing possible. For example:: from fastavro import parse_schema named_schemas = {} parsed_child = parse_schema(child_schema, named_schemas) parsed_parent = parse_schema(parent_schema, named_schemas)

The change

6060 if named_schemas is None:
6161 named_schemas = {}
6262
63+ if isinstance(schema, dict) and "__fastavro_parsed" in schema:
64+ for key, value in schema["__named_schemas"].items():
65+ named_schemas[key] = value
66+
6367 if _force or expand:
6468 return _parse_schema(schema, "", expand, _write_hint, set(), named_schemas)
6569 elif isinstance(schema, dict) and "__fastavro_parsed" in schema:
66- for key, value in schema["__named_schemas"].items():
67- named_schemas[key] = value
6870 return schema
6971 elif isinstance(schema, list):
7072 # If we are given a list we should make sure that the immediate sub

The check that tells the two apart

failpass·tests/test_schema.py::test_schema_expansion_3

Check file tests/test_schema.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 itc3e9957218a41f8e77f807fc884dde5e40f2f41b
Broken version dated2021-04-16
Modulefastavro._schema_py
Units changedparse_schema
Fingerprintebd14e01f47e9e63
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