Skip to content

Commit

Permalink
Add missing code on mypy errors
Browse files Browse the repository at this point in the history
Use mypy api to get the messages details

before:
```
prospector/blender.py:81:13: error(mypy): List comprehension has incompatible type List[Message]; expected List[str]  [misc]
prospector/blender.py:101:12: error(mypy): Incompatible return value type (got "list[str]", expected "list[Message]")  [return-value]
```

after:
```
prospector/blender.py:81:12: misc(mypy): List comprehension has incompatible type List[Message]; expected List[str].
prospector/blender.py:101:11: return-value(mypy): Incompatible return value type (got "list[str]", expected "list[Message]").
```
  • Loading branch information
sbrunner committed Jan 29, 2025
1 parent 224fc52 commit 5f48373
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
3 changes: 1 addition & 2 deletions .prospector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ mypy:
extra-checks: true

pylint:
options:
extension-pkg-allow-list: mypy
disable:
- c-extension-no-member # Needed for mypy
- too-few-public-methods
- missing-docstring
- star-args
Expand Down
66 changes: 52 additions & 14 deletions prospector/tools/mypy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import json
from multiprocessing import Process, Queue
from typing import TYPE_CHECKING, Any, Callable, Optional

from mypy import api
from typing import (
TYPE_CHECKING,
Any,
Callable,
Optional,
)

import mypy.api
import mypy.build
import mypy.errors
import mypy.fscache
import mypy.main

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

__all__ = ("MypyTool",)

from prospector.tools.exceptions import BadToolConfig

if TYPE_CHECKING:
Expand Down Expand Up @@ -58,9 +65,10 @@ def _run_in_subprocess(
class MypyTool(ToolBase):
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.checker = api
self.checker = mypy.api
self.options = ["--show-column-numbers", "--no-error-summary"]
self.use_dmypy = False
self.fscache = mypy.fscache.FileSystemCache()

def configure(self, prospector_config: "ProspectorConfig", _: Any) -> None:
options = prospector_config.tool_options("mypy")
Expand Down Expand Up @@ -90,18 +98,48 @@ def configure(self, prospector_config: "ProspectorConfig", _: Any) -> None:
raise BadToolConfig("mypy", f"The option {name} has an unsupported value type: {type(value)}")

def run(self, found_files: FileFinder) -> list[Message]:
paths = [str(path) for path in found_files.python_modules]
paths.extend(self.options)
args = [str(path) for path in found_files.python_modules]
args.extend(self.options)
if self.use_dmypy:
# Due to dmypy messing with stdout/stderr we call it in a separate
# process
q: Queue[str] = Queue(1)
p = Process(target=_run_in_subprocess, args=(q, self.checker.run_dmypy, ["run", "--"] + paths))
p = Process(target=_run_in_subprocess, args=(q, self.checker.run_dmypy, ["run", "--"] + args))
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()]
report, _ = result[0], result[1:] # noqa
return [format_message(message) for message in report.splitlines()]
else:
return self._run_std(args)

def _run_std(self, args: list[str]) -> list[Message]:
sources, options = mypy.main.process_options(args, fscache=self.fscache)
options.output = "json"
res = mypy.build.build(sources, options, fscache=self.fscache)

messages = []
for mypy_json in res.errors:
mypy_message = json.loads(mypy_json)
message = f"{mypy_message['message']}."
if mypy_message.get("hint", ""):
message = f"{message} {mypy_message['hint']}."
code = mypy_message["code"]
messages.append(
Message(
"mypy",
code=code,
location=Location(
path=mypy_message["file"],
module=None,
function=None,
line=mypy_message["line"],
character=mypy_message["column"],
),
message=message,
doc_url=f"{mypy.errors.BASE_RTD_URL}-{code}",
)
)

return messages

0 comments on commit 5f48373

Please sign in to comment.