allow dict subclasses
MODIFY
fastavro/_schema.pyx
fastavro/_schema.pyx
@@ -18,12 +18,10 @@ cpdef inline extract_record_type(schema):
cpdef inline str extract_logical_type(schema):
- cdef dict d_schema
if not isinstance(schema, dict):
return None
- d_schema = schema
- rt = d_schema['type']
- lt = d_schema.get('logicalType')
+ rt = schema['type']
+ lt = schema.get('logicalType')
if lt:
# TODO: Building this string every time is going to be relatively slow.
return '{}-{}'.format(rt, lt)
@@ -43,7 +41,7 @@ def schema_name(schema, parent_ns):
def extract_named_schemas_into_repo(schema, repo, transformer, parent_ns=None):
- if type(schema) == list:
+ if isinstance(schema, list):
for index, enum_schema in enumerate(schema):
namespaced_name = extract_named_schemas_into_repo(
enum_schema,
@@ -55,7 +53,7 @@ def extract_named_schemas_into_repo(schema, repo, transformer, parent_ns=None):
schema[index] = namespaced_name
return
- if type(schema) != dict:
+ if not isinstance(schema, dict):
# If a reference to another schema is an unqualified name, but not one
# of the primitive types, then we should add the current enclosing
# namespace to reference name.

MODIFY
fastavro/_schema_py.py
fastavro/_schema_py.py
@@ -42,7 +42,7 @@ def schema_name(schema, parent_ns):
def extract_named_schemas_into_repo(schema, repo, transformer, parent_ns=None):
- if type(schema) == list:
+ if isinstance(schema, list):
for index, enum_schema in enumerate(schema):
namespaced_name = extract_named_schemas_into_repo(
enum_schema,
@@ -54,7 +54,7 @@ def extract_named_schemas_into_repo(schema, repo, transformer, parent_ns=None):
schema[index] = namespaced_name
return
- if type(schema) != dict:
+ if not isinstance(schema, dict):
# If a reference to another schema is an unqualified name, but not one
# of the primitive types, then we should add the current enclosing
# namespace to reference name.

MODIFY
tests/test_fastavro.py
tests/test_fastavro.py
@@ -1304,3 +1304,38 @@ def test_string_not_treated_as_array():
}]
assert records == roundtrip(schema, records)
+
+
+def test_schema_is_custom_dict_type():
+ """https://github.com/tebeka/fastavro/issues/168"""
+
+ class CustomDict(dict):
+ pass
+
+ schema = {
+ 'type': 'record',
+ 'fields': [{
+ 'name': 'description',
+ "type": [
+ "null",
+ {
+ "type": "array",
+ "items": "string"
+ },
+ "string"
+ ],
+ }],
+ "name": "description",
+ "doc": "A description of the thing."
+ }
+ other_type_schema = CustomDict(schema)
+
+ record = {
+ 'description': 'value',
+ }
+
+ new_file = MemoryIO()
+ fastavro.schemaless_writer(new_file, schema, record)
+ new_file.seek(0)
+ new_record = fastavro.schemaless_reader(new_file, other_type_schema)
+ assert record == new_record

allow dict subclasses