def test_will_use_instance_if_no_static(): f = ExtMethod() @f.extend(object) def foo(x): return x assert f(int) == int
def test_will_use_tightest_class(): f = ExtMethod() @f.extend(object) def foo(): return 0 @f.extend(int) def bar(): return 1 assert f(object) == 0 assert f(str) == 0 assert f(int) == 1
def test_can_add_static(): f = ExtMethod() @f.extend_static(object) def fs(x): return 1 @f.extend_static(int) def fi(x): return 2 assert f(object) == 1 assert f(int) == 2 assert f(str) == 1
def test_will_use_tightest_class(): f = ExtMethod() @f.extend(object) def foo(x): return 0 @f.extend(int) def bar(x): return 1 assert f(object()) == 0 assert f(u'') == 0 assert f(10) == 1
def test_will_error_on_missing(): f = ExtMethod() with pytest.raises(NotImplementedError): f(int)
"""This is a module for functions I consider to be designed to work around Python doing entirely the wrong thing. You can imagine how grumpy I was when I wrote it. """ from hypothesis.internal.compat import text_type, binary_type, integer_types import math from hypothesis.internal.extmethod import ExtMethod equality = ExtMethod() primitives = [int, float, bool, type, text_type, binary_type ] + list(integer_types) @equality.extend(object) def generic_equality(x, y, fuzzy): try: if len(x) != len(y): return False except (TypeError, AttributeError): pass ix = None iy = None try: ix = iter(x) iy = iter(y) except TypeError: pass
def from_basic(self, data): return self.base_strategy.from_basic(data) def simplifiers(self, random, template): return self.base_strategy.simplifiers(random, template) def templates_for(strat): return TemplatesStrategy(strat) class Rejected(Exception): pass mess_with_basic_data = ExtMethod() def mutate_basic(basic, random): if not random.randint(0, 2): if isinstance(basic, text_type): return list(basic) elif isinstance(basic, integer_types): try: return float(basic) except OverflowError: return -basic else: return text_type(repr(basic)) return mess_with_basic_data(basic, random)
from hypothesis.internal.utils.fixers import actually_equal from hypothesis.internal.compat import text_type, binary_type from hypothesis.internal.extmethod import ExtMethod from hypothesis.types import RandomWithSeed hash_everything_method = ExtMethod() @hash_everything_method.extend(int) @hash_everything_method.extend(float) @hash_everything_method.extend(complex) @hash_everything_method.extend(binary_type) @hash_everything_method.extend(text_type) @hash_everything_method.extend(bool) @hash_everything_method.extend(RandomWithSeed) def normal_hash(x): return hash(x) @hash_everything_method.extend(dict) def dict_hash(x): base = hash(type(x).__name__) for t in x.items(): base ^= hash_everything(t) return base @hash_everything_method.extend(type) def type_hash(x): return hash(x.__name__)