Sugggested changes
Scott made various reasonable comments, these changes are in regards to those comments.
MODIFY
benchmark/benchmark.py
benchmark/benchmark.py
@@ -3,9 +3,6 @@ import datetime
import time
from fastavro import writer, reader
-try:
- from datetime.timezone import utc
-except ImportError:
from fastavro._timezone import utc
try:

MODIFY
fastavro/_schema.pyx
fastavro/_schema.pyx
@@ -28,7 +28,7 @@ cpdef inline str extract_logical_type(schema):
return None
-cpdef schema_name(object schema, parent_ns):
+def schema_name(object schema, parent_ns):
name = schema.get('name')
if not name:
return parent_ns, None

MODIFY
fastavro/_validate_common.py
fastavro/_validate_common.py
from collections import namedtuple
-
-try:
- import ujson as json
-except ImportError:
import json
@@ -24,16 +20,8 @@ class ValidationErrorData(namedtuple('ValidationErrorData',
class ValidationError(Exception):
def __init__(self, *errors):
- # message = ', '.join(str(e) for e in errors)
message = json.dumps([str(e) for e in errors],
indent=2,
ensure_ascii=False)
super(ValidationError, self).__init__(message)
self.errors = errors
-
-
-if __name__ == '__main__':
- raise ValidationError(
- ValidationErrorData(10, "string", "test1"),
- ValidationErrorData(10, "bytes", "test1"),
- ValidationErrorData("bad int", "int", "test1.test_obj.test2"))

MODIFY
fastavro/_validate_py.py
fastavro/_validate_py.py
@@ -314,19 +314,17 @@ def validate(datum, schema, field=None, raise_errors=False):
result = validator(datum, schema=schema,
parent_ns=ns_field,
raise_errors=raise_errors)
-
- if record_type in SCHEMA_DEFS and result is None:
+ elif record_type in SCHEMA_DEFS:
result = validate(datum,
schema=SCHEMA_DEFS[record_type],
field=ns_field,
raise_errors=raise_errors)
+ else:
+ raise UnknownType(record_type)
if raise_errors and result is False:
raise ValidationError(ValidationErrorData(datum, schema, ns_field))
- if result is None:
- raise UnknownType(record_type)
-
return result

MODIFY
fastavro/_write.pyx
fastavro/_write.pyx
@@ -16,7 +16,7 @@ from os import urandom
from zlib import compress
from fastavro import const
-from .validate import validate
+from ._validate import validate
from ._six import utob, long, iteritems, mk_bits
from ._read import HEADER_SCHEMA, SYNC_SIZE, MAGIC
from ._schema import (

MODIFY
fastavro/_write_py.py
fastavro/_write_py.py
@@ -17,7 +17,7 @@ from os import urandom, SEEK_SET
from struct import pack
from zlib import compress
-from fastavro._validate_py import validate
+from .validate import validate
from .const import (
MCS_PER_HOUR, MCS_PER_MINUTE, MCS_PER_SECOND, MLS_PER_HOUR, MLS_PER_MINUTE,
MLS_PER_SECOND, DAYS_SHIFT

MODIFY
fastavro/validate.py
fastavro/validate.py
@@ -34,9 +34,9 @@ except ImportError:
from ._validate_common import ValidationErrorData, ValidationError
validate = _validate.validate
-registry_validator = _validate.register_validator
+register_validator = _validate.register_validator
get_validator = _validate.get_validator
validate_many = _validate.validate_many
__all__ = ['ValidationError', 'ValidationErrorData', 'validate',
- 'validate_many', 'registry_validator', 'get_validator']
+ 'validate_many', 'register_validator', 'get_validator']

MODIFY
tests/test_validation.py
tests/test_validation.py
-from fastavro.validate import ValidationError, validate_many
+from fastavro.validate import ValidationError, ValidationErrorData, validate_many
import pytest
schema = {
@@ -24,6 +24,8 @@ schema = {
"name": "missingerror",
"type": "record"
}
+
+
# TODO: Add more test for all types and combinations
@@ -154,3 +156,11 @@ def test_validate_null_in_string_false():
}]
assert validation_boolean(schema, *records) is False
+
+
+def test_validate_error_raises():
+ with pytest.raises(ValidationError):
+ raise ValidationError(
+ ValidationErrorData(10, "string", "test1"),
+ ValidationErrorData(10, "bytes", "test1"),
+ ValidationErrorData("bad int", "int", "test1.test_obj.test2"))

Sugggested changes

Scott made various reasonable comments, these changes are in regards to those comments.