Ejemplo n.º 1
0
def test_strictus_field_required_option():
    f1 = strictus_field()
    assert not f1.required
    assert not f1.clone().required

    f2 = strictus_field(required=True)
    assert f2.required
    assert f2.clone().required
Ejemplo n.º 2
0
def test_unnamed_field_gets_name_from_getter():
    def zzz(self) -> str:
        return "z-value"

    f1 = strictus_field(zzz)
    assert f1.name == "zzz"
    assert f1.type is str

    f2 = strictus_field()(zzz)
    assert f2.name == "zzz"
    assert f2.type is str
Ejemplo n.º 3
0
    class A(strictus):

        v: int = strictus_field(init=True)
        w: int

        # a virtual field that is included in the dict output.
        @strictus_field
        def x(self):
            return "xxx"

        # a virtual field that is NOT included in the dict output
        @strictus_field(dict=False)
        def y(self):
            return "yyy"

        z: int = strictus_field(init=False)
Ejemplo n.º 4
0
    class C(CBase, strictus):
        class Meta:
            init_all = True

        q: B
        r: int = 5
        s: List[int] = strictus_field(default_factory=list)
Ejemplo n.º 5
0
def test_calling_strictus_field_with_func_produces_strictus_field_with_getter(
):
    def x(self, value):
        self._x = value

    f = strictus_field(x)
    assert f.name == "x"
    assert f.getter is x
Ejemplo n.º 6
0
    class C(strictus):
        n: int = 0
        o: int = strictus_field(default=1, dict=False)

        @strictus_field(dict=False)
        def p(self):
            return 2

        @strictus_field
        def q(self):
            return 3
Ejemplo n.º 7
0
def test_calling_strictus_field_instance_registers_getter():
    f = strictus_field(name="x")
    assert f.name == "x"

    def x(self) -> str:
        return "normal-x"

    f(x)
    assert f.getter is x
    assert f.type is str

    lambda_getter = lambda self: "lambda-x"
    f(lambda_getter)

    assert f.getter is lambda_getter

    # Getter's name must match the field name
    def bad_getter(self):
        return "bad-x"

    with pytest.raises(ValueError):
        f(bad_getter)
Ejemplo n.º 8
0
 class B(strictus):
     a: A = strictus_field(default_factory=A)
     a_list: List[A] = strictus_field(default_factory=list)
Ejemplo n.º 9
0
 class A(strictus):
     x: int = strictus_field(read_only=True)
Ejemplo n.º 10
0
 class A(strictus):
     x1: int = strictus_field(dict=False)
     x2: int = strictus_field(dict=False, default=22)
     y1: str = strictus_field(dict=True)
     y2: str = strictus_field(dict=True, default="yy")
     z: float
Ejemplo n.º 11
0
def test_fields_by_default_are_included_in_dict_repr():
    f = strictus_field()
    assert f.dict is True

    g = strictus_field(dict=False)
    assert g.dict is False
Ejemplo n.º 12
0
class ExampleProcess(strictus):
    id: str = None
    status: str = None
    events: ExampleEvents = strictus_field(default_factory=ExampleEvents)
Ejemplo n.º 13
0
 class A(strictus):
     x: int = strictus_field(default=55)
Ejemplo n.º 14
0
 class A(strictus):
     x: int
     y: int
     label: str = strictus_field(default=None, read_only=True)
Ejemplo n.º 15
0
 class B(A):
     y: int = strictus_field(required=True)
     z: int
Ejemplo n.º 16
0
 class A(strictus):
     x: int = strictus_field(required=True)
     y: int
Ejemplo n.º 17
0
 class B(A):
     x: int = strictus_field(default=55,
                             dict=False,
                             read_only=True,
                             init=False)
Ejemplo n.º 18
0
def test_strictus_field_without_func_produces_strictus_field_instance():
    f = strictus_field(name="f", type=int)
    assert isinstance(f, strictus_field)
    assert f.name == "f"
    assert f.type is int