~chiefnoah/pybare

e602163a4a490f44a6bed099a9949b142d2e5386 — Noah Pederson 8 months ago bf1f2a2
Minor cleanup
2 files changed, 14 insertions(+), 7 deletions(-)

M bare/encoder.py
M bare/test_encoder.py
M bare/encoder.py => bare/encoder.py +3 -5
@@ 164,9 164,7 @@ class Struct(ABC):

    def __init__(self, *args, **kwargs):
        # loop through defined fields, if they have a corresponding kwarg entry, set the value
        for name, field in filter(
            lambda x: isinstance(x[1], (Field, Struct)), self.__class__.__dict__.items()
        ):
        for name, field in self.fields().items():
            if name in kwargs:
                setattr(self, name, kwargs[name])
            else:


@@ 558,7 556,7 @@ class Union(Field):
            else:
                self._members.append(member)
        if value is not None:
            valid, message = self.validate(value)
            valid, _ = self.validate(value)
            if not valid:
                raise ValidationError(
                    f"Attempting to set incorrect value to Union type: {type(value)}"


@@ 641,7 639,7 @@ def _read_varint(fp: typing.BinaryIO, signed=True) -> int:
    while True:
        try:
            b = fp.read(1)[0]
        except IndexError as e:
        except IndexError:
            raise RuntimeError("Not enough bytes in buffer to decode")
        if b < 0x80:
            value = output | b << offset

M bare/test_encoder.py => bare/test_encoder.py +11 -2
@@ 7,6 7,7 @@ import pytest
import enum
import io
import os
import inspect


class Nested(Struct):


@@ 39,6 40,14 @@ def test_example_struct():
    assert ex.testint == 11
    assert ex.teststr == "a test"

class C():
    s = Str()

def test_assign_value():
    o = C()
    o.s = "some string"
    assert getattr(o, 's') == 'some string'
    assert isinstance(inspect.getmembers(C)[-1][1], Str)

class ExampleMap(Map):
    _keytype = Str


@@ 68,9 77,9 @@ def test_map():
        ex.m2 = {"test": "test"}
    with pytest.raises(ValidationError):
        ex.m3["3"] = 3
    map = Map(Str(), Str(), values={"test": "test"})
    map = Map(Str(), Str(), value={"test": "test"})
    assert map.value["test"] == "test"
    map2 = Map(Str(), Str())
    map2 = Map(Str, Str)
    map2.value["another"] = "test"
    assert map2.value["another"] == "test"