Skip to content

Commit

Permalink
Add nested backend support where one backend can rely on another via …
Browse files Browse the repository at this point in the history
…a default.
  • Loading branch information
hameerabbasi committed Jun 30, 2019
1 parent d179d70 commit 604446a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions uarray/_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ def inner(*args, **kwargs):
result = options.backend.__ua_function__(inner, a, kw)

if result is NotImplemented:
result = try_default(a, kw, options, errors)
with set_backend(options.backend, only=True, coerce=options.coerce):

This comment has been minimized.

Copy link
@peterbell10

peterbell10 Jun 30, 2019

Collaborator

Isn't the set_backend on line 164 redundant now.

This comment has been minimized.

Copy link
@hameerabbasi

hameerabbasi Jul 1, 2019

Author Collaborator

I’ll change that.

This comment has been minimized.

Copy link
@hameerabbasi

hameerabbasi Jul 1, 2019

Author Collaborator

removed this one.

result = try_default(a, kw, options, errors)

if result is not NotImplemented:
break
Expand Down Expand Up @@ -309,7 +310,7 @@ def skip_backend(backend):
--------
set_backend: A context manager that allows setting of backends.
"""
skip = _get_skipped_backends(backend.domain)
skip = _get_skipped_backends(backend.__ua_domain__)
new = set(skip.get())
new.add(backend)
token = skip.set(new)
Expand Down
Empty file added uarray/tests/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions uarray/tests/test_uarray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import uarray as ua


class Backend:
__ua_domain__ = "ua_tests"


def test_nestedbackend():
obj = object()
be_outer = Backend()
be_outer.__ua_function__ = lambda f, a, kw: obj

mm1 = ua.generate_multimethod(lambda: (), lambda a, kw, d: (a, kw), "ua_tests")

def default(*a, **kw):
return mm1(*a, **kw)

mm2 = ua.generate_multimethod(
lambda: (), lambda a, kw, d: (a, kw), "ua_tests", default=default
)

be_inner = Backend()

def be2_ua_func(f, a, kw):
with ua.skip_backend(be_inner):
return f(*a, **kw)

be_inner.__ua_function__ = be2_ua_func
with ua.set_backend(be_outer), ua.set_backend(be_inner):
assert mm2() is obj

0 comments on commit 604446a

Please sign in to comment.