Пример #1
0
def callable_type(fdef: FuncItem,
                  fallback: Instance,
                  ret_type: Optional[Type] = None) -> CallableType:
    # TODO: somewhat unfortunate duplication with prepare_method_signature in semanal
    if fdef.info and not fdef.is_static:
        self_type = fill_typevars(fdef.info)  # type: Type
        if fdef.is_class or fdef.name() == '__new__':
            self_type = TypeType.make_normalized(self_type)
        args = [
            self_type
        ] + [AnyType(TypeOfAny.unannotated)] * (len(fdef.arg_names) - 1)
    else:
        args = [AnyType(TypeOfAny.unannotated)] * len(fdef.arg_names)

    return CallableType(
        args,
        fdef.arg_kinds,
        [None if argument_elide_name(n) else n for n in fdef.arg_names],
        ret_type or AnyType(TypeOfAny.unannotated),
        fallback,
        name=fdef.name(),
        line=fdef.line,
        column=fdef.column,
        implicit=True,
    )
Пример #2
0
 def copy_function_attributes(self, new: FuncItem,
                              original: FuncItem) -> None:
     new.info = original.info
     new.min_args = original.min_args
     new.max_pos = original.max_pos
     new.is_overload = original.is_overload
     new.is_generator = original.is_generator
Пример #3
0
 def copy_function_attributes(self, new: FuncItem,
                              original: FuncItem) -> None:
     new.info = original.info
     new.min_args = original.min_args
     new.max_pos = original.max_pos
     new.is_overload = original.is_overload
     new.is_generator = original.is_generator
Пример #4
0
 def copy_function_attributes(self, new: FuncItem, original: FuncItem) -> None:
     new.info = original.info
     new.min_args = original.min_args
     new.max_pos = original.max_pos
     new.is_implicit = original.is_implicit
     new.is_overload = original.is_overload
     new.is_generator = original.is_generator
     new.init = self.duplicate_inits(original.init)
Пример #5
0
 def copy_function_attributes(self, new: FuncItem,
                              original: FuncItem) -> None:
     new.info = original.info
     new.min_args = original.min_args
     new.max_pos = original.max_pos
     new.is_implicit = original.is_implicit
     new.is_overload = original.is_overload
     new.is_generator = original.is_generator
     new.init = self.duplicate_inits(original.init)
def verify_funcitem(stub: nodes.FuncItem,
                    runtime: MaybeMissing[types.FunctionType],
                    object_path: List[str]) -> Iterator[Error]:
    if isinstance(runtime, Missing):
        yield Error(object_path, "is not present at runtime", stub, runtime)
        return
    if (not isinstance(runtime,
                       (types.FunctionType, types.BuiltinFunctionType))
            and not isinstance(runtime,
                               (types.MethodType, types.BuiltinMethodType))
            and not inspect.ismethoddescriptor(runtime)):
        yield Error(object_path, "is not a function", stub, runtime)
        return

    for message in _verify_static_class_methods(stub, runtime, object_path):
        yield Error(object_path, "is inconsistent, " + message, stub, runtime)

    try:
        signature = inspect.signature(runtime)
    except (ValueError, RuntimeError):
        # inspect.signature throws sometimes
        # catch RuntimeError because of https://bugs.python.org/issue39504
        return

    if stub.name in ("__init_subclass__", "__class_getitem__"):
        # These are implicitly classmethods. If the stub chooses not to have @classmethod, we
        # should remove the cls argument
        if stub.arguments[0].variable.name == "cls":
            stub = copy.copy(stub)
            stub.arguments = stub.arguments[1:]

    stub_sig = Signature.from_funcitem(stub)
    runtime_sig = Signature.from_inspect_signature(signature)

    for message in _verify_signature(stub_sig,
                                     runtime_sig,
                                     function_name=stub.name):
        yield Error(
            object_path,
            "is inconsistent, " + message,
            stub,
            runtime,
            runtime_desc="def " + str(signature),
        )