allow ints as default values for floats
MODIFY
fastavro/_schema.pyx
fastavro/_schema.pyx
@@ -176,8 +176,8 @@ cdef _parse_schema(
or (schema == "boolean" and not isinstance(default, bool))
or (schema == "string" and not isinstance(default, str))
or (schema == "bytes" and not isinstance(default, str))
- or (schema == "double" and not isinstance(default, float))
- or (schema == "float" and not isinstance(default, float))
+ or (schema == "double" and not isinstance(default, (int, float)))
+ or (schema == "float" and not isinstance(default, (int, float)))
or (schema == "int" and not isinstance(default, int))
or (schema == "long" and not isinstance(default, int))
):

MODIFY
fastavro/_schema_py.py
fastavro/_schema_py.py
@@ -323,13 +323,14 @@ def _parse_schema(
elif not isinstance(schema, dict):
if schema in PRIMITIVES:
if default is not NO_DEFAULT:
+ # TODO: Consider using the validate functions here
if (
(schema == "null" and default is not None)
or (schema == "boolean" and not isinstance(default, bool))
or (schema == "string" and not isinstance(default, str))
or (schema == "bytes" and not isinstance(default, str))
- or (schema == "double" and not isinstance(default, float))
- or (schema == "float" and not isinstance(default, float))
+ or (schema == "double" and not isinstance(default, (int, float)))
+ or (schema == "float" and not isinstance(default, (int, float)))
or (schema == "int" and not isinstance(default, int))
or (schema == "long" and not isinstance(default, int))
):

MODIFY
tests/test_schema.py
tests/test_schema.py
@@ -1317,3 +1317,20 @@ def test_correct_exception_text_for_default_value_test():
match="Default value <10> must match schema type",
):
fastavro.parse_schema(schema)
+
+
+@pytest.mark.parametrize("field_type,default_value", (("float", 0), ("double", 0)))
+def test_special_case_defaults(field_type, default_value):
+ schema = {
+ "type": "record",
+ "name": "test_special_case_defaults",
+ "fields": [
+ {
+ "name": "field",
+ "type": field_type,
+ "default": default_value,
+ }
+ ],
+ }
+
+ fastavro.parse_schema(schema)

allow ints as default values for floats