use internal _validate to prevent unneeded schema parsing
MODIFY
fastavro/_write.pyx
fastavro/_write.pyx
@@ -14,7 +14,7 @@ import zlib
from fastavro import const
from ._logical_writers import LOGICAL_WRITERS
-from ._validation import validate
+from ._validation import _validate
from ._six import utob, long, iteritems, appendable
from ._read import HEADER_SCHEMA, SYNC_SIZE, MAGIC, reader
from ._schema import extract_record_type, extract_logical_type, parse_schema
@@ -231,7 +231,7 @@ cdef write_union(bytearray fo, datum, schema):
best_match_index = -1
most_fields = -1
for index, candidate in enumerate(schema):
- if validate(datum, candidate, raise_errors=False):
+ if _validate(datum, candidate, raise_errors=False):
if extract_record_type(candidate) == 'record':
candidate_fields = set(
f["name"] for f in candidate["fields"]
@@ -512,7 +512,7 @@ cdef class Writer(object):
self.fo = fo
self.schema = parse_schema(schema)
- self.validate_fn = validate if validator is True else validator
+ self.validate_fn = _validate if validator is True else validator
self.io = MemoryIO()
self.block_count = 0
self.sync_interval = sync_interval

MODIFY
fastavro/_write_py.py
fastavro/_write_py.py
@@ -14,7 +14,7 @@ import zlib
from .io.binary_encoder import BinaryEncoder
from .io.json_encoder import AvroJSONEncoder
-from .validation import validate
+from .validation import _validate
from .six import utob, MemoryIO, iteritems, appendable
from .read import HEADER_SCHEMA, SYNC_SIZE, MAGIC, reader
from .logical_writers import LOGICAL_WRITERS
@@ -151,7 +151,7 @@ def write_union(encoder, datum, schema):
best_match_index = -1
most_fields = -1
for index, candidate in enumerate(schema):
- if validate(datum, candidate, raise_errors=False):
+ if _validate(datum, candidate, raise_errors=False):
if extract_record_type(candidate) == 'record':
candidate_fields = set(
f["name"] for f in candidate["fields"]
@@ -360,7 +360,7 @@ class GenericWriter(object):
metadata=None,
validator=None):
self.schema = parse_schema(schema)
- self.validate_fn = validate if validator is True else validator
+ self.validate_fn = _validate if validator is True else validator
self.metadata = metadata or {}
if isinstance(schema, dict):

MODIFY
fastavro/validation.py
fastavro/validation.py
@@ -4,6 +4,9 @@ except ImportError:
from . import _validation_py as _validation
from ._validate_common import ValidationErrorData, ValidationError
+# Private API
+_validate = _validation._validate
+
# Public API
validate = _validation.validate
validate_many = _validation.validate_many

use internal _validate to prevent unneeded schema parsing (#426)