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'
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()))
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()))
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)))
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)
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)
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)
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))
class C(object): a = typed(Optional[cl])
class C(object): a = typed(Union[cl_a, cl_b])