Lines Matching refs:cls

188 def total_ordering(cls):
191 roots = {op for op in _convert if getattr(cls, op, None) is not getattr(object, op, None)}
198 setattr(cls, opname, opfunc)
199 return cls
283 def __new__(cls, func, /, *args, **keywords):
292 self = super(partial, cls).__new__(cls)
363 # flattening is mandatory in order to place cls/self before all
378 format_string = "{module}.{cls}({func}, {args}, {keywords})"
380 cls=self.__class__.__qualname__,
393 def __get__(self, obj, cls=None):
397 new_func = get(obj, cls)
409 result = self._make_unbound_method().__get__(obj, cls)
687 def _c3_mro(cls, abcs=None):
696 i.e. issubclass(cls, abc) returns True for the class itself but returns
704 for i, base in enumerate(reversed(cls.__bases__)):
706 boundary = len(cls.__bases__) - i
711 explicit_bases = list(cls.__bases__[:boundary])
713 other_bases = list(cls.__bases__[boundary:])
715 if issubclass(cls, base) and not any(
716 issubclass(b, base) for b in cls.__bases__
718 # If *cls* is the class that introduces behaviour described by
727 [[cls]] +
732 def _compose_mro(cls, types):
733 """Calculates the method resolution order for a given class *cls*.
739 bases = set(cls.__mro__)
744 and issubclass(cls, typ))
755 # *cls* can be used to stabilize ABC ordering.
761 if sub not in bases and issubclass(cls, sub):
772 return _c3_mro(cls, abcs=mro)
774 def _find_impl(cls, registry):
775 """Returns the best matching implementation from *registry* for type *cls*.
784 mro = _compose_mro(cls, registry.keys())
790 if (t in registry and t not in cls.__mro__
791 and match not in cls.__mro__
818 def dispatch(cls):
819 """generic_func.dispatch(cls) -> <function implementation>
822 for the given *cls* registered on *generic_func*.
832 impl = dispatch_cache[cls]
835 impl = registry[cls]
837 impl = _find_impl(cls, registry)
838 dispatch_cache[cls] = impl
841 def _is_union_type(cls):
843 return get_origin(cls) in {Union, types.UnionType}
845 def _is_valid_dispatch_type(cls):
846 if isinstance(cls, type):
849 return (_is_union_type(cls) and
850 all(isinstance(arg, type) for arg in get_args(cls)))
852 def register(cls, func=None):
853 """generic_func.register(cls, func) -> func
855 Registers a new implementation for the given *cls* on a *generic_func*.
859 if _is_valid_dispatch_type(cls):
861 return lambda f: register(cls, f)
866 f"{cls!r} is not a class or union type."
868 ann = getattr(cls, '__annotations__', {})
871 f"Invalid first argument to `register()`: {cls!r}. "
875 func = cls
879 argname, cls = next(iter(get_type_hints(func).items()))
880 if not _is_valid_dispatch_type(cls):
881 if _is_union_type(cls):
884 f"{cls!r} not all arguments are classes."
889 f"{cls!r} is not a class."
892 if _is_union_type(cls):
895 for arg in get_args(cls):
898 registry[cls] = func
899 if cache_token is None and hasattr(cls, '__abstractmethods__'):
936 def register(self, cls, method=None):
937 """generic_method.register(cls, func) -> func
939 Registers a new implementation for the given *cls* on a *generic_method*.
941 return self.dispatcher.register(cls, func=method)
943 def __get__(self, obj, cls=None):
946 return method.__get__(obj, cls)(*args, **kwargs)