def _(func): trace_func = trace(func) TwoLevelDispatcher.register(self, *types, **kwargs)(trace_func) # return func instead trace_func here so that # chained register didn't get wrapped multiple # times return func
def foo_dispatchers(): foo = TwoLevelDispatcher('foo', doc='Test dispatcher foo') foo_m = Dispatcher('foo_m', doc='Control dispatcher foo_m') @foo.register(A1, B1) @foo_m.register(A1, B1) def foo0(x, y): return 0 @foo.register(A1, B2) @foo_m.register(A1, B2) def foo1(x, y): return 1 @foo.register(A2, B1) @foo_m.register(A2, B1) def foo2(x, y): return 2 @foo.register(A2, B2) @foo_m.register(A2, B2) def foo3(x, y): return 3 @foo.register( (A1, A2), ) @foo_m.register( (A1, A2), ) def foo4(x): return 4 return foo, foo_m
def test_ambiguities_warning(): bar = TwoLevelDispatcher('bar') bar.register(A1, B1)(lambda a, b: 0) bar.register(A1, B2)(lambda a, b: 1) bar.register(A2, B1)(lambda a, b: 2) with pytest.warns(AmbiguityWarning, match=".*Consider.*\n\n.*(A2, B2).*"): bar.reorder()
def test_ambiguities_no_warning(): bar = TwoLevelDispatcher('bar') bar.register(A1, B1)(lambda a, b: 0) bar.register(A1, B2)(lambda a, b: 1) bar.register(A2, B1)(lambda a, b: 2) bar.register(A2, B2)(lambda a, b: 3) with warnings.catch_warnings(): warnings.simplefilter("error") bar.reorder()
def test_ambiguities_no_warning(): bar = TwoLevelDispatcher('bar') bar.register(A1, B1)(lambda a, b: 0) bar.register(A1, B2)(lambda a, b: 1) bar.register(A2, B1)(lambda a, b: 2) bar.register(A2, B2)(lambda a, b: 3) with pytest.warns(None) as warnings: bar.reorder() assert len(warnings) == 0