Sourcery refactoring

This commit is contained in:
Dan Yeaw 2022-11-19 22:09:49 -05:00
parent 599fd85141
commit 6513631bc2
No known key found for this signature in database
GPG Key ID: 86B9FEF88B780F2B
5 changed files with 14 additions and 11 deletions

View File

@ -3,7 +3,7 @@
This code is a Python 3, slimmed down version of the
generic package by Andrey Popp.
Only the generic function code is left in tact -- no generic methods.
Only the generic function code is left intact -- no generic methods.
The interface has been made in line with `functools.singledispatch`.
Note that this module does not support annotated functions.
@ -133,6 +133,6 @@ class FunctionDispatcher(Generic[T]):
def _arity(argspec: inspect.FullArgSpec) -> int:
"""Determinal positional arity of argspec."""
args = argspec.args if argspec.args else []
defaults = argspec.defaults if argspec.defaults else []
args = argspec.args or []
defaults = argspec.defaults or []
return len(args) - len(defaults)

View File

@ -86,9 +86,7 @@ class MethodDispatcher(FunctionDispatcher[T]):
self.local.unbound_rules = []
def __get__(self, obj, cls):
if obj is None:
return self
return types.MethodType(self, obj)
return self if obj is None else types.MethodType(self, obj)
def register(self, *argtypes: KeyType) -> Callable[[T], T]:
"""Register new case for multimethod for ``argtypes``"""

View File

@ -84,7 +84,7 @@ class Registry(Generic[T]):
corresponding axes, in order, using ``None`` as a placeholder for
skipped axes."""
axes_dict = self._axes_dict
aligned: list[S | None] = [None for i in range(len(axes_dict))]
aligned: list[S | None] = [None for _ in range(len(axes_dict))]
args_len = len(args)
if args_len + len(kw) > len(aligned):
@ -106,7 +106,7 @@ class Registry(Generic[T]):
aligned[i] = v
# Trim empty tail nodes for faster look ups
# Trim empty tail nodes for faster look-ups
while aligned and aligned[-1] is None:
del aligned[-1]

View File

@ -33,7 +33,7 @@ def test_one_argument():
with pytest.raises(TypeError):
dispatcher("s")
dispatcher.register_rule(lambda x: x + "1", str)
dispatcher.register_rule(lambda x: f"{x}1", str)
assert dispatcher(1) == 2
assert dispatcher("1") == "11"
with pytest.raises(TypeError):

View File

@ -12,7 +12,8 @@ def test_multimethod():
@foo.register(str) # type: ignore[no-redef]
def foo(self, x):
return x + "1"
return f"{x}1"
assert Dummy().foo(1) == 2
assert Dummy().foo("1") == "11"
@ -69,6 +70,7 @@ def test_multimethod_otherwise_clausewith_two_arguments():
def test_inheritance():
@has_multimethods
class Dummy:
@multimethod(int)
@ -79,11 +81,13 @@ def test_inheritance():
def foo(self, x):
return x + 1.5
@has_multimethods
class DummySub(Dummy):
@Dummy.foo.register(str)
def foo(self, x):
return x + "1"
return f"{x}1"
@foo.register(tuple) # type: ignore[no-redef]
def foo(self, x):
@ -93,6 +97,7 @@ def test_inheritance():
def foo(self, x):
return not x
assert Dummy().foo(1) == 2
assert Dummy().foo(1.5) == 3.0