Skip to content

Commit

Permalink
Do joint simplification of identical elements
Browse files Browse the repository at this point in the history
Idea: If we have a list containing the same element twice some tests
will only fail if those values are identical. We'd still like to be
able to replace them with a simpler identical value.

I've been apologising for the lack of this feature for ages, so it's
a little embarrassing how easy it was to add. The reason for that is
that the template approach makes this much easier because we can
guarantee that types are hashable and that object identity is not
a problem (e.g. previously if we'd been simplifying two lists that
were value but not reference equal we'd have to worry about copying
them so that they stayed that way).
  • Loading branch information
DRMacIver committed Mar 21, 2015
1 parent a7a100c commit 57a529e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/hypothesis/searchstrategy/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ def simplify(self, x):
del z[i]
yield tuple(z)

same_valued_indices = {}
for i, value in enumerate(x):
same_valued_indices.setdefault(value, []).append(i)
for indices in same_valued_indices.values():
if len(indices) > 1:
value = x[indices[0]]
for simpler in self.element_strategy.simplify(value):
copy = list(x)
for i in indices:
copy[i] = simpler
yield tuple(copy)

def to_basic(self, value):
check_type(tuple, value)
if self.element_strategy is None:
Expand Down

0 comments on commit 57a529e

Please sign in to comment.