Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builds() and from_type() should introspect attrs-classes #954

Closed
Zac-HD opened this issue Oct 27, 2017 · 2 comments
Closed

builds() and from_type() should introspect attrs-classes #954

Zac-HD opened this issue Oct 27, 2017 · 2 comments
Assignees
Labels
new-feature entirely novel capabilities or strategies

Comments

@Zac-HD
Copy link
Member

Zac-HD commented Oct 27, 2017

There's heaps of metadata on the attributes, and we could certainly infer a strategy for attributes with an "instance_of" validator. Even with python-attrs/attrs#249, special handling would still be valuable because we could filter by any other validators.

This issue brought to you by realizing that I wanted this feature in a project I'm playing with - in that case it's going to be faster to hard-code a strategy, but it would definitely be used as well as nice to have 😄

@CodyKochmann
Copy link
Contributor

Ive had to reroute a few projects because I was using that type of layout but didn't have a clean way to test specifically this so definitely +1

@Zac-HD Zac-HD added the new-feature entirely novel capabilities or strategies label Oct 29, 2017
@Zac-HD
Copy link
Member Author

Zac-HD commented Apr 26, 2018

Some notes:

  • attrs supports extensions by providing Attribute objects.
  • attr.fields(SomeClass) returns a tuple of Attributes for SomeClass
  • An Attribute may have a default of a value or a factory; and may also have a type. These are easy to construct a strategy for.
  • In the unlikely event that default==NOTHING and type is None but convert or converter (at most one may be non-None) is a callable with type annotations, we may be able to infer a strategy that way.
  • Converters may also be wrapped in converters.optional, which makes None a valid value.

This all becomes more "interesting" once we start to consider validators.

  • Built-in validators are great for introspection. optional as for converters. An instance_of validator is an older way of specifying type. An in_ validator is a perfect feeder for sampled_from.
  • Multiple validators can be given as a list or wrapped in and_; each are run in order so we'd need to unwrap them.
  • Validators run on the state-so-far, and may depend on multiple fields, so we'll need to have some logic that converts validation errors to a reject() call (as is done for Django models)

All together, this probably means we'll write a new module hypothesis.searchstrategy.attrs and function from_attrs_class, then if isinstance(thing, type) and attr.has(thing) return from_attrs_class(thing, *args, **kwargs) - as for builds itself and e.g. Django models, we want to allow users to set sub-strategies explicitly - at the top of builds (and from_type).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-feature entirely novel capabilities or strategies
Projects
None yet
Development

No branches or pull requests

2 participants