1380637840ee — Gerald Klix (speedy) 6 years ago
SUM: Fixed some bugs.
FIX: In same cases the `__module__` attribute of a generic can not be determined.
     Avoid a nasty exception.
FIX: The `Dispatch.ON_CLASS` dispatch-type had completely wrong error handling.
1 files changed, 15 insertions(+), 8 deletions(-)

M gf/base.py
M gf/base.py +15 -8
@@ 106,9 106,10 @@ class GenericFunction(object):
         """If we dispatch on non types change the mro computation."""
         # Early exit, won't switch if we only dispatch on class
         if self.dispatch == Dispatch.ON_CLASS:
-            if any(isinstance(t, type) for t in types):
-                raise TypeError("Can't dispatch on instances in state: %s" %
-                                self.dispatch)
+            if any(not isinstance(t, type) for t in types):
+                raise TypeError(
+                    "Can't dispatch on instances in state: %s" %
+                    self.dispatch)
             return
         if self.has_default_dispatch and any(not isinstance(t, type)
                                              for t in types):

          
@@ 162,7 163,8 @@ class GenericFunction(object):
 
     def _get_functions_types(self, function):
         """Answer the types of the functions parameters."""
-        return [object if parameter.annotation == Parameter.empty else parameter.annotation
+        return [object if parameter.annotation == Parameter.empty 
+                else parameter.annotation
                 for parameter in get_signature(function).parameters.values()
                 if parameter.kind == Parameter.POSITIONAL_OR_KEYWORD]
         

          
@@ 198,7 200,7 @@ class GenericFunction(object):
         types = tuple(types)
         try:
             func = self.cache[types]
-        except TypeError:  # types is not hashable, so don't cash it
+        except TypeError:  # types is not hashable, so don't cache it
             func = self.find_func(types)
         except KeyError:
             self.cache[types] = func = self.find_func(types)

          
@@ 252,7 254,7 @@ class GenericFunction(object):
     
         self._fix_variadics(len(types))
         # I can't help myself -- this is going to be intense functional code.
-        # Find all possible candidate signatures.
+        # Find all possible candidate signatures. (2018-09-29: Comment by GvR)
         mros = tuple(self.compute_mro(t) for t in types)
         n = len(mros)
         candidates = [sig for sig in self.registry

          
@@ 286,6 288,7 @@ class GenericFunction(object):
         def dominates(dom, sub,
                       orders=tuple(dict((t, i) for i, t in enumerate(mro))
                                    for mro in mros)):
+            # 2018-09-29: Comment by GvR
             # Predicate to decide whether dom strictly dominates sub.
             # Strict domination is defined as domination without equality.
             # The arguments dom and sub are type tuples of equal length.

          
@@ 380,8 383,12 @@ def _generic(
     else:
         if name is not None:
             substitute.__name__ = substitute.__qualname__ = name
-            substitute.__module__ = (
-                sys._getframe(4).f_globals['__name__'])
+            try:
+                substitute.__module__ = (
+                    sys._getframe(4).f_globals['__name__'])
+            except KeyError:
+                # Might happen during doctests
+                substitute.__module__ = '<unknown module>'
         if doc is not None:
             substitute.__doc__ = doc
     genericFunction.substitute = substitute