From 15cbfac63de3b608d7a1246d006047162d703caf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl> Date: Mon, 4 Mar 2024 10:46:42 +0100 Subject: [PATCH] fixes --- satella/coding/overloading.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/satella/coding/overloading.py b/satella/coding/overloading.py index db65cb1d..134aa63a 100644 --- a/satella/coding/overloading.py +++ b/satella/coding/overloading.py @@ -6,6 +6,8 @@ import typing as tp # Taken from https://stackoverflow.com/questions/28237955/same-name-for-classmethod-and- # instancemethod + + class class_or_instancemethod(classmethod): """ A decorator to make your methods both classmethods (they will receive an instance of type @@ -110,6 +112,8 @@ class overload: >>> @what_type.overload >>> def what_type(x: int): >>> print('Int') + >>> what_type(5) + >>> what_type('string') Note that this instance's __wrapped__ will refer to the first function. TypeError will be called if no signatures match arguments. @@ -120,7 +124,11 @@ class overload: if hasattr(fun, '__doc__'): self.__doc__ = fun.__doc__ self.__wrapped__ = fun - self.history_list = [] + + @property + def all_functions(self) -> tp.Iterable[object]: + """Return a list of all functions registered within this overload""" + return self.type_signatures_to_functions.values() def overload(self, fun): """ @@ -138,12 +146,12 @@ class overload: :raises TypeError: no type signature given """ - matchings = [] + matching = [] for sign, fun in self.type_signatures_to_functions.items(): if sign.matches(*args, **kwargs): - matchings.append((sign, fun)) - matchings.sort() - if not matchings: + matching.append((sign, fun)) + matching.sort() + if not matching: raise TypeError('No matching entries!') else: - return matchings[-1][1](*args, **kwargs) # call the most specific function you could find + return matching[-1][1](*args, **kwargs) # call the most specific function you could find -- GitLab