Skip to content

Commit

Permalink
Store generic origins as tuples.
Browse files Browse the repository at this point in the history
Minor optimization and simplification to check `issubclass`.
  • Loading branch information
coady committed Jan 27, 2024
1 parent b3f4404 commit 8346c76
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions multimethod/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class multimethod(dict):

__name__: str
pending: set
generics: list[set]
generics: list[tuple] # positional bases which require instance checks

def __new__(cls, func):
homonym = inspect.currentframe().f_back.f_locals.get(func.__name__)
Expand Down Expand Up @@ -311,8 +311,8 @@ def __setitem__(self, types: tuple, func: Callable):
key.parents.add(types)
for index, cls in enumerate(types):
if origins := set(subtype.origins(cls)):
self.generics += (set() for _ in range(index + 1 - len(self.generics)))
self.generics[index].update(origins)
self.generics += [()] * (index + 1 - len(self.generics))
self.generics[index] = tuple(origins.union(self.generics[index]))
super().__setitem__(types, func)
self.__doc__ = self.docstring

Expand Down Expand Up @@ -349,7 +349,7 @@ def __missing__(self, types: tuple) -> Callable:

def dispatch(self, *args) -> Callable:
types = tuple(map(type, args))
if not any(issubclass(cls, tuple(generics)) for cls, generics in zip(types, self.generics)):
if not any(map(issubclass, types, self.generics)):
return self[types]
matches = {key for key in list(self) if isinstance(key, signature) and key.instances(*args)}
matches -= {ancestor for match in matches for ancestor in match.parents}
Expand Down

0 comments on commit 8346c76

Please sign in to comment.