Exemplo n.º 1
0
def test_resolve_init_var():
    assert dataclass_types_and_fields(WithInitVar) == (
        {"a": int},
        (),
        (fields(WithInitVar).a,),
    )
Exemplo n.º 2
0

@mark.parametrize(
    "cls, method, args",
    [
        *(py37 if sys.version_info >= (3, 7) else py36),
        *pep_585,
        (Annotated[int, 42, "42"], Visitor.annotated, [int, (42, "42")]),
        (Any, Visitor.any, []),
        (
            DataclassExample,
            Visitor.dataclass,
            [
                DataclassExample,
                {"a": int, "b": str},
                (fields(DataclassExample).a, fields(DataclassExample).b),
                (),
            ],
        ),
        (EnumExample, Visitor.enum, [EnumExample]),
        (Literal[1, 2], Visitor.literal, [(1, 2)]),
        (
            NamedTupleExample,
            Visitor.named_tuple,
            [NamedTupleExample, {"a": int, "b": str}, {"b": ""}],
        ),
        (NewTypeExample, Visitor.new_type, [NewTypeExample, int]),
        (int, Visitor.primitive, [int]),
        (str, Visitor.primitive, [str]),
        (MyInt, Visitor.subprimitive, [MyInt, int]),
        (Tuple[str, int], Visitor.tuple, [(str, int)]),
Exemplo n.º 3
0
    @validator
    def a_gt_10(self):
        if self.a <= 10:
            yield "error"

    @validator
    def a_lt_100(self):
        if self.a >= 100:
            raise ValueError("error2")

    @validator
    def non_trivial(self):
        non_trivial(self)


validator_field = cast(Field, fields(Data).with_validator)


def non_trivial(data: Data):
    return data.c == data.b


def test_get_validators():
    assert get_validators(Data) == (Data.a_gt_10, Data.a_lt_100,
                                    Data.non_trivial)


def test_validator_descriptor():
    # Class field is descriptor
    val: Validator = Data.a_gt_10
    assert val.dependencies == {"a"}
Exemplo n.º 4
0
 def check_parity_equivalent(self):
     if (self.parity == Parity.EVEN) != (self.number % 2 == 0):
         yield fields(self).number, "number doesn't respect parity"
Exemplo n.º 5
0
 def check_ips_in_subnet(self):
     for index, ip in enumerate(self.ips):
         if ip not in self.subnet:
             # yield <error path>, <error message>
             yield (fields(self).ips, index), "ip not in subnet"
Exemplo n.º 6
0
 def values_dont_exceed_bounds(self):
     min_bound, max_bound = self.bounds
     for index, value in enumerate(self.values):
         if not min_bound <= value <= max_bound:
             yield (fields(self).values, index), "value exceeds bounds"
Exemplo n.º 7
0
 def bounds_are_sorted(self):
     min_bound, max_bound = self.bounds
     if min_bound > max_bound:
         yield fields(self).bounds, "bounds are not sorted"