Exemple #1
0
def process_signature(app, what, name, obj, options, signature,
                      return_annotation):
    filter_params = getattr(app.config, _ext, None)
    stars = bool(getattr(app.config, _ext + '_stars', True))

    if filter_params is None or not callable(obj) or signature is None:
        return signature
    if not callable(filter_params):
        _log.error(_ext + ' is not callable')
        return signature

    signature = Signature(obj)

    def _filter(param):
        param_name = param.name
        var_pos = param.kind is inspect.Parameter.VAR_POSITIONAL
        var_kw = param.kind is inspect.Parameter.VAR_KEYWORD
        if stars:
            if var_pos:
                param_name = '*' + param_name
            elif var_kw:
                param_name = '**' + param_name
        keep = filter_params(obj, param_name)
        if not keep and not var_pos and not var_kw \
                and param.default is inspect.Parameter.empty:
            _log.warning(_ext + ': Param ' + param_name + ' has no default!')
        return keep

    signature.signature = signature.signature.replace(
        parameters=filter(_filter, signature.signature.parameters.values()),
        return_annotation=inspect.Signature.empty)

    return signature.format_args().replace('\\', '\\\\'), None
def process_signature(app, what: str, name: str, obj, options, signature, return_annotation):
    if not callable(obj):
        return

    original_obj = obj
    if inspect.isclass(obj):
        obj = getattr(obj, '__init__', getattr(obj, '__new__', None))

    if not getattr(obj, '__annotations__', None):
        return

    obj = inspect.unwrap(obj)
    signature = Signature(obj)
    parameters = [
        param.replace(annotation=inspect.Parameter.empty)
        for param in signature.signature.parameters.values()
    ]

    if '<locals>' in obj.__qualname__:
        logger.warning(
            'Cannot treat a function defined as a local function: "%s"  (use @functools.wraps)',
            name)
        return

    if parameters:
        if inspect.isclass(original_obj):
            del parameters[0]
        elif what == 'method':
            outer = inspect.getmodule(obj)
            for clsname in obj.__qualname__.split('.')[:-1]:
                outer = getattr(outer, clsname)

            method_name = obj.__name__
            if method_name.startswith("__") and not method_name.endswith("__"):
                # If the method starts with double underscore (dunder)
                # Python applies mangling so we need to prepend the class name.
                # This doesn't happen if it always ends with double underscore.
                class_name = obj.__qualname__.split('.')[-2]
                method_name = "_{c}{m}".format(c=class_name, m=method_name)

            method_object = outer.__dict__[method_name] if outer else obj
            if not isinstance(method_object, (classmethod, staticmethod)):
                del parameters[0]

    signature.signature = signature.signature.replace(
        parameters=parameters,
        return_annotation=inspect.Signature.empty)

    return signature.format_args().replace('\\', '\\\\'), None
Exemple #3
0
def process_signature(
    app,
    what: str,
    name: str,
    obj,  # pylint: disable=too-many-arguments,unused-argument
    options,
    signature,
    return_annotation,
):  # pylint: disable=unused-argument
    if callable(obj):
        if what in ("class", "exception"):
            obj = getattr(obj, "__init__")

        obj = unwrap(obj)
        try:
            signature = Signature(obj)
        except:
            signature = Signature(lambda: None)
        parameters = [
            param.replace(annotation=inspect.Parameter.empty) for param in signature.signature.parameters.values()
        ]

        if parameters:
            if what in ("class", "exception"):
                del parameters[0]
            elif what == "method":
                outer = inspect.getmodule(obj)
                if outer is None:
                    return 
                for clsname in obj.__qualname__.split(".")[:-1]:
                    outer = getattr(outer, clsname, outer)

                method_name = obj.__name__
                if method_name.startswith("__") and not method_name.endswith("__"):
                    # If the method starts with double underscore (dunder)
                    # Python applies mangling so we need to prepend the class name.
                    # This doesn't happen if it always ends with double underscore.
                    class_name = obj.__qualname__.split(".")[-2]
                    method_name = "_{c}{m}".format(c=class_name, m=method_name)

                method_object = getattr(outer,'__dict__',{}).get(method_name, None)
                if not isinstance(method_object, (classmethod, staticmethod)):
                    del parameters[0]

        signature.signature = signature.signature.replace(
            parameters=parameters, return_annotation=inspect.Signature.empty
        )

        return signature.format_args().replace("\\", "\\\\"), None
def strip_annotations(app, what: str, name: str, obj, options, signature,
                      return_annotation):
    if what not in {'function', 'method', 'class'}:
        return

    new_signature = Signature(obj)
    new_signature.signature = new_signature.signature.replace(
        return_annotation=inspect.Signature.empty,
        parameters=[
            param.replace(annotation=inspect.Parameter.empty)
            for param in new_signature.signature.parameters.values()
            if param.name != 'self'
        ],
    )

    return new_signature.format_args(), None
Exemple #5
0
def process_signature(app, what: str, name: str, obj, options, signature,
                      return_annotation):
    if not callable(obj):
        return

    if what in ('class', 'exception'):
        obj = getattr(obj, '__init__', getattr(obj, '__new__', None))

    if not getattr(obj, '__annotations__', None):
        return

    obj = unwrap(obj)
    signature = Signature(obj)
    parameters = [
        param.replace(annotation=inspect.Parameter.empty)
        for param in signature.signature.parameters.values()
    ]

    if parameters:
        if what in ('class', 'exception'):
            del parameters[0]
        elif what == 'method':
            outer = inspect.getmodule(obj)
            for clsname in obj.__qualname__.split('.')[:-1]:
                outer = getattr(outer, clsname)

            method_name = obj.__name__
            if method_name.startswith("__") and not method_name.endswith("__"):
                # If the method starts with double underscore (dunder)
                # Python applies mangling so we need to prepend the class name.
                # This doesn't happen if it always ends with double underscore.
                class_name = obj.__qualname__.split('.')[-2]
                method_name = "_{c}{m}".format(c=class_name, m=method_name)

            method_object = outer.__dict__[method_name]
            if not isinstance(method_object, (classmethod, staticmethod)):
                del parameters[0]

    signature.signature = signature.signature.replace(
        parameters=parameters, return_annotation=inspect.Signature.empty)

    return signature.format_args(), None