Пример #1
0
def test_existing_metadata():
    """
    `typed` does not clobber metadata.
    """

    res = typed(Any, metadata={'test': 'test'})

    assert res.metadata[TYPE_METADATA_KEY] is Any
    assert res.metadata['test'] == 'test'
Пример #2
0
def str_typed_attrs(draw, defaults=None):
    """
    Generate a tuple of an attribute and a strategy that yields strs for that
    attribute.
    """
    default = NOTHING
    if defaults is True or (defaults is None and draw(booleans())):
        default = draw(text())
    return ((typed(unicode, default=default), text()))
Пример #3
0
def float_typed_attrs(draw, defaults=None):
    """
    Generate a tuple of an attribute and a strategy that yields floats for that
    attribute.
    """
    default = attr.NOTHING
    if defaults is True or (defaults is None and draw(booleans())):
        default = draw(floats())
    return ((typed(float, default=default), floats()))
Пример #4
0
def bare_typed_attrs(draw, defaults=None):
    """
    Generate a tuple of an attribute and a strategy that yields values
    appropriate for that attribute.
    """
    default = attr.NOTHING
    if defaults is True or (defaults is None and draw(booleans())):
        default = None
    return ((typed(Any, default=default), just(None)))
Пример #5
0
def dict_of_class(tup):
    nested_cl = tup[1][0]
    nested_cl_args = tup[1][1]
    default = attr.Factory(lambda: {"cls": nested_cl(*nested_cl_args)})
    combined_attrs = list(tup[0])
    combined_attrs.append(
        (typed(Dict[str, nested_cl],
               default=default), just({'cls': nested_cl(*nested_cl_args)})))
    return _create_hyp_class(combined_attrs)
Пример #6
0
def list_of_class(tup):
    nested_cl = tup[1][0]
    nested_cl_args = tup[1][1]
    default = attr.Factory(lambda: [nested_cl(*nested_cl_args)])
    combined_attrs = list(tup[0])
    combined_attrs.append(
        (typed(List[nested_cl],
               default=default), just([nested_cl(*nested_cl_args)])))
    return _create_hyp_class(combined_attrs)
Пример #7
0
def just_class(tup):
    # tup: Tuple[List[Tuple[_CountingAttr, Strategy]],
    #            Tuple[Type, Sequence[Any]]]
    nested_cl = tup[1][0]
    nested_cl_args = tup[1][1]
    default = attr.Factory(lambda: nested_cl(*nested_cl_args))
    combined_attrs = list(tup[0])
    combined_attrs.append(
        (typed(nested_cl, default=default), just(nested_cl(*nested_cl_args))))
    return _create_hyp_class(combined_attrs)
Пример #8
0
def dict_typed_attrs(draw, defaults=None):
    """
    Generate a tuple of an attribute and a strategy that yields dictionaries
    for that attribute. The dictionaries map strings to integers.
    """
    default = attr.NOTHING
    val_strat = dictionaries(keys=text(), values=integers())
    if defaults is True or (defaults is None and draw(booleans())):
        default = draw(val_strat)
    return ((typed(Dict[unicode, int], default=default), val_strat))
Пример #9
0
 class C(object):
     a = typed(Optional[cl])
Пример #10
0
 class C(object):
     a = typed(Union[cl_a, cl_b])