Skip to content

Commit

Permalink
Merge pull request #611 from lukaspiatkowski/dmypy
Browse files Browse the repository at this point in the history
Add use-dmypy option to MypyTool
  • Loading branch information
carlio authored May 7, 2023
2 parents c798436 + 13693be commit 02b84b3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/profiles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,9 @@ The available options are:
+----------------+------------------------+----------------------------------------------+
| mypy | namespace-packages | Import discovery uses namespace packages |
+----------------+------------------------+----------------------------------------------+
| mypy | use-dmypy | Use mypy daemon (mypy server) for faster |
| | | checks |
+----------------+------------------------+----------------------------------------------+
| bandit | config | configuration filename |
+----------------+------------------------+----------------------------------------------+
| bandit | profile | profile to use |
Expand Down
25 changes: 24 additions & 1 deletion prospector/tools/mypy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from multiprocessing import Process, Queue

from mypy import api

from prospector.message import Location, Message
Expand All @@ -9,6 +11,7 @@

LIST_OPTIONS = ["allow", "check", "disallow", "no-check", "no-warn", "warn"]
VALID_OPTIONS = LIST_OPTIONS + [
"use-dmypy",
"strict",
"follow-imports",
"ignore-missing-imports",
Expand Down Expand Up @@ -50,11 +53,20 @@ def format_message(message):
)


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")
Expand All @@ -66,6 +78,8 @@ def configure(self, prospector_config, _):
"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")
Expand Down Expand Up @@ -110,7 +124,16 @@ def configure(self, prospector_config, _):
def run(self, found_files):
paths = [str(path) for path in found_files.python_modules]
paths.extend(self.options)
result = self.checker.run(paths)
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()]

0 comments on commit 02b84b3

Please sign in to comment.