\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/cloudlinux/venv/lib/python3.11/site-packages/prospector/tools/mypy/

Viewing File: __init__.py

from multiprocessing import Process, Queue

from mypy import api

from prospector.message import Location, Message
from prospector.tools import ToolBase

__all__ = ("MypyTool",)

from prospector.tools.exceptions import BadToolConfig

LIST_OPTIONS = ["allow", "check", "disallow", "no-check", "no-warn", "warn"]
VALID_OPTIONS = LIST_OPTIONS + [
    "use-dmypy",
    "strict",
    "follow-imports",
    "ignore-missing-imports",
    "implicit-optional",
    "strict-optional",
    "platform",
    "python-2-mode",
    "python-version",
    "namespace-packages",
]


def format_message(message):
    try:
        (path, line, char, err_type, err_msg) = message.split(":", 4)
        line = int(line)
        character = int(char)
    except ValueError:
        try:
            (path, line, err_type, err_msg) = message.split(":", 3)
            line = int(line)
            character = None
        except ValueError:
            (path, err_type, err_msg) = message.split(":", 2)
            line = 0
            character = None
    location = Location(
        path=path,
        module=None,
        function=None,
        line=line,
        character=character,
    )
    return Message(
        source="mypy",
        code=err_type.lstrip(" "),
        location=location,
        message=err_msg.lstrip(" "),
    )


def _run_in_subprocess(q, cmd, paths):
    """
    This function exists only to be called by multiprocessing.Process as using
    lambda is forbidden
    """
    q.put(cmd(paths))


class MypyTool(ToolBase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.checker = api
        self.options = ["--show-column-numbers", "--no-error-summary"]
        self.use_dmypy = False

    def configure(self, prospector_config, _):
        options = prospector_config.tool_options("mypy")

        for option_key in options.keys():
            if option_key not in VALID_OPTIONS:
                url = "https://github.com/PyCQA/prospector/blob/master/prospector/tools/mypy/__init__.py"
                raise BadToolConfig(
                    "mypy", f"Option {option_key} is not valid. " f"See the list of possible options: {url}"
                )

        self.use_dmypy = options.get("use-dmypy", False)

        strict = options.get("strict", False)

        follow_imports = options.get("follow-imports", "normal")
        ignore_missing_imports = options.get("ignore-missing-imports", False)
        implict_optional = options.get("implict-optional", False)
        platform = options.get("platform", None)
        python_2_mode = options.get("python-2-mode", False)
        python_version = options.get("python-version", None)
        strict_optional = options.get("strict-optional", False)
        namespace_packages = options.get("namespace-packages", False)

        self.options.append(f"--follow-imports={follow_imports}")

        if strict:
            self.options.append("--strict")

        if ignore_missing_imports:
            self.options.append("--ignore-missing-imports")

        if implict_optional:
            self.options.append("--implict-optional")

        if platform:
            self.options.append(f"--platform {platform}")

        if python_2_mode:
            self.options.append("--py2")

        if python_version:
            self.options.append(f"--python-version {python_version}")

        if strict_optional:
            self.options.append("--strict-optional")

        if namespace_packages:
            self.options.append("--namespace-packages")

        for list_option in LIST_OPTIONS:
            for entry in options.get(list_option, []):
                self.options.append(f"--{list_option}-{entry}")

    def run(self, found_files):
        paths = [str(path) for path in found_files.python_modules]
        paths.extend(self.options)
        if self.use_dmypy:
            # Due to dmypy messing with stdout/stderr we call it in a separate
            # process
            q = Queue(1)
            p = Process(target=_run_in_subprocess, args=(q, self.checker.run_dmypy, ["run", "--"] + paths))
            p.start()
            result = q.get()
            p.join()
        else:
            result = self.checker.run(paths)
        report, _ = result[0], result[1:]  # noqa

        return [format_message(message) for message in report.splitlines()]