~chiefnoah/pybare

23a8152673a0e350e3e977751f3aaf3dd14662b1 — Noah Pederson 8 months ago 2c2fad6
Adds some docs
2 files changed, 13 insertions(+), 1 deletions(-)

M bare/encoder.py
M bare/types.py
M bare/encoder.py => bare/encoder.py +4 -0
@@ 12,6 12,10 @@ from collections import UserDict, UserList


class ValidationError(ValueError):
    """
    ValidationError is an error raised by a `Field` type when the `validate` class function
    determines a given value is 'out-of-spec'.
    """
    pass



M bare/types.py => bare/types.py +9 -1
@@ 1,3 1,6 @@
"""
bare.types contains primitive types
"""
from .encoder import (
    Field,
    BareType,


@@ 351,10 354,15 @@ class DataFixed(Field):

class Enum(UInt):
    def __init__(self, enum, *args, **kwargs):
        """Enum defines a BARE enum type

        Validate checks whether the provided value is an `int` and a valid enum member
        :param enum (`enum.Enum`): a standard `Enum` type. Values for enum members *must* be positive ints
        """
        self._enum = enum
        super().__init__(*args, **kwargs)

    def validate(self, value):
    def validate(self, value) -> ValidationMessage:
        if not isinstance(value, int):
            return False, f"type: {type(value)} is not valid for Enum, must be <int>"
        if value < 0: