def __call__(cls, *args, **kwargs): # type.__call__ calls instance.__init__ internally try: instance = ABCMeta.__call__(cls, *args, **kwargs) except Exception as e: # FIXME: What is the point of this long exception message? # Why can't we just let the exception propagate up the stack? # Is it because of some weird interaction between this metaclass and the Qt event loop? # ....probably err = "Could not create instance of '{}'\n".format(cls) err += "args = {}\n".format(args) err += "kwargs = {}\n".format(kwargs) err += "The exception was:\n" err += str(e) err += "\nTraceback:\n" import traceback import sys import io if sys.version_info.major == 2: s = io.BytesIO() else: s = io.StringIO() traceback.print_exc(file=s) err += s.getvalue() raise RuntimeError(err) instance._after_init() return instance
def __call__(cls, *args, **kwargs): instance = NativeABCMeta.__call__(cls, *args, **kwargs) abstract_attributes = { name for name in dir(instance) if getattr(getattr( instance, name), '__is_abstract_attribute__', False) } if abstract_attributes: raise NotImplementedError( "Can't instantiate abstract class {} with" " abstract attributes: {}".format( cls.__name__, ', '.join(abstract_attributes))) return instance
def __call__(cls, *args: Any, **kwargs: Any) -> Any: instance = NativeABCMeta.__call__(cls, *args, **kwargs) abstract_attributes = [] for name in dir(instance): attr = getattr(instance, name, None) if attr is not None: if getattr(attr, '_is_abstract_attribute_', False): abstract_attributes.append(name) if len(abstract_attributes) > 0: raise NotImplementedError( "Class {} doesn't initialize following abstract attributes: {}" .format(cls.__name__, ', '.join(abstract_attributes))) return instance
def __call__(cls, *args, **kwargs): """Create a new class ``cls`` from given arguments.""" obj = ABCMeta.__call__(cls, *args, **kwargs) if not hasattr(obj, 'domain'): raise NotImplementedError('`Operator` instances must have a ' '`Operator.domain` attribute.') if not hasattr(obj, 'range'): raise NotImplementedError('`Operator` instances must have a ' '`Operator.range` attribute.') if not hasattr(obj, '_call') and not hasattr(obj, '_apply'): raise NotImplementedError('`Operator` instances must either ' 'have `_call` or `_apply` as ' 'attribute.') return obj
def __call__(cls, *args, **kwargs): instance = NativeABCMeta.__call__(cls, *args, **kwargs) abstract_attrs = ({name for name in dir(instance) if getattr(getattr(instance, name), '__is_abstract_attribute__', False)}) if abstract_attrs: cls_name = cls.__name__ attr_str = ', '.join(abstract_attrs) num_missing = len(abstract_attrs) raise NotImplementedError(f"Children of abstract class {cls_name} " f"must define {attr_str} as attribute " f"{'s' if num_missing > 1 else ''}") return instance
def __call__(cls, *args, **kwargs): """Intercept instance creation.""" # Create instance instance = ABCMeta.__call__(cls, *args, **kwargs) # Check abstract not_defined = [] for attr in cls.__abstract_attributes: if attr not in instance.__dict__: not_defined.append(attr) if not_defined: raise TypeError(cls.__name__ + ".__init__ did not define these abstract " "attributes:\n" + str(not_defined)) return instance
def __call__(cls, *args, **kwargs): # type.__call__ calls instance.__init__ internally try: instance = ABCMeta.__call__(cls, *args, **kwargs) except Exception as e: err = "Could not create instance of '{}'\n".format(cls) err += "args = {}\n".format(args) err += "kwargs = {}\n".format(kwargs) err += "The exception was:\n" err += str(e) err += "\nTraceback:\n" import traceback import StringIO s = StringIO.StringIO() traceback.print_exc(file=s) err += s.getvalue() raise RuntimeError(err) instance._after_init() return instance
def __call__(cls, *args, **kwargs): logger.debug('pass arguments to constructor of class "%s"', cls.__name__) return ABCMeta.__call__(cls, *args, **kwargs)
def __call__(*args, **kwargs): obj = ABCMeta.__call__(*args, **kwargs) _check_obj(obj) return obj
def __call__(cls, *args, **kwargs): logger.debug( 'pass arguments to constructor of class "%s"', cls.__name__ ) return ABCMeta.__call__(cls, *args, **kwargs)
def __call__(cls, *arg, **kwarg): return ABCMeta.__call__(cls, *arg, **kwarg)