Skip to content

Commit

Permalink
Add instance __ua_convert__ wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
hameerabbasi committed May 7, 2020
1 parent c9d9bdc commit a1c98d6
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions uarray/_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"BackendNotImplementedError",
"Dispatchable",
"wrap_single_convertor",
"wrap_single_convertor_instance",
"all_of_type",
"mark_as",
"set_state",
Expand Down Expand Up @@ -511,3 +512,28 @@ def __ua_convert__(dispatchables, coerce):
return converted

return __ua_convert__


def wrap_single_convertor_instance(convert_single):
"""
Wraps a ``__ua_convert__`` defined for a single element to all elements.
If any of them return ``NotImplemented``, the operation is assumed to be
undefined.
Accepts a signature of (value, type, coerce).
"""

@functools.wraps(convert_single)
def __ua_convert__(self, dispatchables, coerce):
converted = []
for d in dispatchables:
c = convert_single(self, d.value, d.type, coerce and d.coercible)

if c is NotImplemented:
return NotImplemented

converted.append(c)

return converted

return __ua_convert__

0 comments on commit a1c98d6

Please sign in to comment.