def decorator(f): fully_qualified, old_attribute_name = _utils.get_qualified_name(f) if attr_postfix: old_attribute_name += attr_postfix @six.wraps(f) def wrapper(self, *args, **kwargs): base_name = _utils.get_class_name(self, fully_qualified=False) if fully_qualified: old_name = old_attribute_name else: old_name = ".".join((base_name, old_attribute_name)) new_name = ".".join((base_name, new_attribute_name)) prefix = _KIND_MOVED_PREFIX_TPL % (kind, old_name, new_name) out_message = _utils.generate_message( prefix, message=message, version=version, removal_version=removal_version) _utils.deprecation(out_message, stacklevel=stacklevel, category=category) return f(self, *args, **kwargs) return wrapper
def removed_module( module, replacement=None, message=None, version=None, removal_version=None, stacklevel=3, category=None ): """Helper to be called inside a module to emit a deprecation warning :param str replacment: A location (or information about) of any potential replacement for the removed module (if applicable) :param str message: A message to include in the deprecation warning :param str version: Specify what version the removed module is present in :param str removal_version: What version the module will be removed. If '?' is used this implies an undefined future version :param int stacklevel: How many entries deep in the call stack before ignoring :param type category: warnings message category (this defaults to ``DeprecationWarning`` when none is provided) """ if inspect.ismodule(module): module_name = _get_qualified_name(module) elif isinstance(module, six.string_types): module_name = module else: _qual, type_name = _utils.get_qualified_name(type(module)) raise TypeError("Unexpected module type '%s' (expected string or" " module type only)" % type_name) prefix = "The '%s' module usage is deprecated" % module_name if replacement: postfix = ", please use %s instead" % replacement else: postfix = None out_message = _utils.generate_message( prefix, postfix=postfix, message=message, version=version, removal_version=removal_version ) _utils.deprecation(out_message, stacklevel=stacklevel, category=category)
def wrapper(f, instance, args, kwargs): qualified, f_name = _utils.get_qualified_name(f) if qualified: if inspect.isclass(f): prefix_pre = "Using class" thing_post = '' else: prefix_pre = "Using function/method" thing_post = '()' if not qualified: prefix_pre = "Using function/method" base_name = None if instance is None: # Decorator was used on a class if inspect.isclass(f): prefix_pre = "Using class" thing_post = '' module_name = _get_module_name(inspect.getmodule(f)) if module_name == '__main__': f_name = _utils.get_class_name( f, fully_qualified=False) else: f_name = _utils.get_class_name( f, fully_qualified=True) # Decorator was a used on a function else: thing_post = '()' module_name = _get_module_name(inspect.getmodule(f)) if module_name != '__main__': f_name = _utils.get_callable_name(f) # Decorator was used on a classmethod or instancemethod else: thing_post = '()' base_name = _utils.get_class_name(instance, fully_qualified=False) if base_name: thing_name = ".".join([base_name, f_name]) else: thing_name = f_name else: thing_name = f_name if thing_post: thing_name += thing_post prefix = prefix_pre + " '%s' is deprecated" % (thing_name) out_message = _utils.generate_message( prefix, version=version, removal_version=removal_version, message=message) _utils.deprecation(out_message, stacklevel=stacklevel, category=category) return f(*args, **kwargs)
def wrapper(f, instance, args, kwargs): qualified, f_name = _utils.get_qualified_name(f) if qualified: if inspect.isclass(f): prefix_pre = "Using class" thing_post = '' else: prefix_pre = "Using function/method" thing_post = '()' if not qualified: prefix_pre = "Using function/method" base_name = None if instance is None: # Decorator was used on a class if inspect.isclass(f): prefix_pre = "Using class" thing_post = '' module_name = _get_qualified_name(inspect.getmodule(f)) if module_name == '__main__': f_name = _utils.get_class_name( f, fully_qualified=False) else: f_name = _utils.get_class_name( f, fully_qualified=True) # Decorator was a used on a function else: thing_post = '()' module_name = _get_qualified_name(inspect.getmodule(f)) if module_name != '__main__': f_name = _utils.get_callable_name(f) # Decorator was used on a classmethod or instancemethod else: thing_post = '()' base_name = _utils.get_class_name(instance, fully_qualified=False) if base_name: thing_name = ".".join([base_name, f_name]) else: thing_name = f_name else: thing_name = f_name if thing_post: thing_name += thing_post prefix = prefix_pre + " '%s' is deprecated" % (thing_name) out_message = _utils.generate_message( prefix, version=version, removal_version=removal_version, message=message) _utils.deprecation(out_message, stacklevel=stacklevel, category=category) return f(*args, **kwargs)
def moved_class(new_class, old_class_name, old_module_name, message=None, version=None, removal_version=None, stacklevel=3, category=None): """Deprecates a class that was moved to another location. This creates a 'new-old' type that can be used for a deprecation period that can be inherited from. This will emit warnings when the old locations class is initialized, telling where the new and improved location for the old class now is. """ if not inspect.isclass(new_class): _qual, type_name = _utils.get_qualified_name(type(new_class)) raise TypeError("Unexpected class type '%s' (expected" " class type only)" % type_name) old_name = ".".join((old_module_name, old_class_name)) new_name = _utils.get_class_name(new_class) prefix = _CLASS_MOVED_PREFIX_TPL % (old_name, new_name) out_message = _utils.generate_message(prefix, message=message, version=version, removal_version=removal_version) def decorator(f): @six.wraps(f, assigned=("__name__", "__doc__")) def wrapper(self, *args, **kwargs): _utils.deprecation(out_message, stacklevel=stacklevel, category=category) return f(self, *args, **kwargs) return wrapper old_class = type(old_class_name, (new_class, ), {}) old_class.__module__ = old_module_name old_class.__init__ = decorator(old_class.__init__) return old_class
def removed_module(module, replacement=None, message=None, version=None, removal_version=None, stacklevel=3, category=None): """Helper to be called inside a module to emit a deprecation warning :param str replacment: A location (or information about) of any potential replacement for the removed module (if applicable) :param str message: A message to include in the deprecation warning :param str version: Specify what version the removed module is present in :param str removal_version: What version the module will be removed. If '?' is used this implies an undefined future version :param int stacklevel: How many entries deep in the call stack before ignoring :param type category: warnings message category (this defaults to ``DeprecationWarning`` when none is provided) """ if inspect.ismodule(module): module_name = _get_module_name(module) elif isinstance(module, six.string_types): module_name = module else: _qual, type_name = _utils.get_qualified_name(type(module)) raise TypeError("Unexpected module type '%s' (expected string or" " module type only)" % type_name) prefix = "The '%s' module usage is deprecated" % module_name if replacement: postfix = ", please use %s instead" % replacement else: postfix = None out_message = _utils.generate_message(prefix, postfix=postfix, message=message, version=version, removal_version=removal_version) _utils.deprecation(out_message, stacklevel=stacklevel, category=category)
def decorator(f): fully_qualified, old_attribute_name = _utils.get_qualified_name(f) if attr_postfix: old_attribute_name += attr_postfix @wrapt.decorator def wrapper(wrapped, instance, args, kwargs): base_name = _utils.get_class_name(wrapped, fully_qualified=False) if fully_qualified: old_name = old_attribute_name else: old_name = ".".join((base_name, old_attribute_name)) new_name = ".".join((base_name, new_attribute_name)) prefix = _KIND_MOVED_PREFIX_TPL % (kind, old_name, new_name) out_message = _utils.generate_message( prefix, message=message, version=version, removal_version=removal_version) _utils.deprecation(out_message, stacklevel=stacklevel, category=category) return wrapped(*args, **kwargs) return wrapper(f)
def moved_class(new_class, old_class_name, old_module_name, message=None, version=None, removal_version=None, stacklevel=3, category=None): """Deprecates a class that was moved to another location. This creates a 'new-old' type that can be used for a deprecation period that can be inherited from. This will emit warnings when the old locations class is initialized, telling where the new and improved location for the old class now is. """ if not inspect.isclass(new_class): _qual, type_name = _utils.get_qualified_name(type(new_class)) raise TypeError("Unexpected class type '%s' (expected" " class type only)" % type_name) old_name = ".".join((old_module_name, old_class_name)) new_name = _utils.get_class_name(new_class) prefix = _CLASS_MOVED_PREFIX_TPL % (old_name, new_name) out_message = _utils.generate_message( prefix, message=message, version=version, removal_version=removal_version) def decorator(f): @six.wraps(f, assigned=_utils.get_assigned(f)) def wrapper(self, *args, **kwargs): _utils.deprecation(out_message, stacklevel=stacklevel, category=category) return f(self, *args, **kwargs) return wrapper old_class = type(old_class_name, (new_class,), {}) old_class.__module__ = old_module_name old_class.__init__ = decorator(old_class.__init__) return old_class
def _get_module_name(mod): return _utils.get_qualified_name(mod)[1]
def _check_it(cls): if not inspect.isclass(cls): _qual, type_name = _utils.get_qualified_name(type(cls)) raise TypeError("Unexpected class type '%s' (expected" " class type only)" % type_name)
def _get_qualified_name(obj): return _utils.get_qualified_name(obj)[1]