-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #612 from lukaspiatkowski/pyright
Add pyright as an optional tool
- Loading branch information
Showing
12 changed files
with
266 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import json | ||
import subprocess | ||
|
||
import pyright | ||
|
||
from prospector.message import Location, Message | ||
from prospector.tools import ToolBase | ||
|
||
__all__ = ("PyrightTool",) | ||
|
||
from prospector.tools.exceptions import BadToolConfig | ||
|
||
VALID_OPTIONS = [ | ||
"level", | ||
"project", | ||
"pythonplatform", | ||
"pythonversion", | ||
"skipunannotated", | ||
"typeshed-path", | ||
"venv-path", | ||
] | ||
|
||
|
||
def format_messages(json_encoded): | ||
json_decoded = json.loads(json_encoded) | ||
diagnostics = json_decoded.get("generalDiagnostics", []) | ||
messages = [] | ||
for diag in diagnostics: | ||
start_range = diag.get("range", {}).get("start", {}) | ||
location = Location( | ||
path=diag["file"], | ||
module=None, | ||
function=None, | ||
line=start_range.get("line", -1), | ||
character=start_range.get("character", -1), | ||
) | ||
messages.append( | ||
Message(source="pyright", code=diag.get("rule", ""), location=location, message=diag.get("message", "")) | ||
) | ||
|
||
return messages | ||
|
||
|
||
class PyrightTool(ToolBase): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.checker = pyright | ||
self.options = ["--outputjson"] | ||
|
||
def configure(self, prospector_config, _): | ||
options = prospector_config.tool_options("pyright") | ||
|
||
for option_key in options.keys(): | ||
if option_key not in VALID_OPTIONS: | ||
url = "https://github.com/PyCQA/prospector/blob/master/prospector/tools/pyright/__init__.py" | ||
raise BadToolConfig( | ||
"pyright", f"Option {option_key} is not valid. " f"See the list of possible options: {url}" | ||
) | ||
|
||
level = options.get("level", None) | ||
project = options.get("project", None) | ||
pythonplatform = options.get("pythonplatform", None) | ||
pythonversion = options.get("pythonversion", None) | ||
skipunannotated = options.get("skipunannotated", False) | ||
typeshed_path = options.get("typeshed-path", None) | ||
venv_path = options.get("venv-path", None) | ||
|
||
if level: | ||
self.options.extend(["--level", level]) | ||
if project: | ||
self.options.extend(["--project", project]) | ||
if pythonplatform: | ||
self.options.extend(["--pythonplatform", pythonplatform]) | ||
if pythonversion: | ||
self.options.extend(["--pythonversion", pythonversion]) | ||
if skipunannotated: | ||
self.options.append("--skipunannotated") | ||
if typeshed_path: | ||
self.options.extend(["--typeshed-path", typeshed_path]) | ||
if venv_path: | ||
self.options.extend(["--venv-path", venv_path]) | ||
|
||
def run(self, found_files): | ||
paths = [str(path) for path in found_files.python_modules] | ||
paths.extend(self.options) | ||
result = self.checker.run(*paths, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | ||
|
||
return format_messages(result.stdout) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pyright: | ||
run: yes | ||
options: | ||
warn-unreachable: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pyright: | ||
run: yes | ||
options: | ||
level: error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import json | ||
from pathlib import Path | ||
from unittest import SkipTest, TestCase | ||
from unittest.mock import patch | ||
|
||
from prospector.config import ProspectorConfig | ||
from prospector.finder import FileFinder | ||
from prospector.message import Location, Message | ||
from prospector.tools.exceptions import BadToolConfig | ||
|
||
try: | ||
from prospector.tools.pyright import format_messages | ||
except ImportError: | ||
raise SkipTest | ||
|
||
|
||
class TestPyrightTool(TestCase): | ||
@staticmethod | ||
def _get_config(profile_name: str) -> ProspectorConfig: | ||
profile_path = Path(__file__).parent / f"test_profiles/{profile_name}.yaml" | ||
with patch("sys.argv", ["prospector", "--profile", str(profile_path.absolute())]): | ||
return ProspectorConfig() | ||
|
||
def test_unrecognised_options(self): | ||
finder = FileFinder(Path(__file__).parent) | ||
self.assertRaises(BadToolConfig, self._get_config("pyright_bad_options").get_tools, finder) | ||
|
||
def test_good_options(self): | ||
finder = FileFinder(Path(__file__).parent) | ||
self._get_config("pyright_good_options").get_tools(finder) | ||
|
||
|
||
class TestPyrightMessageFormat(TestCase): | ||
def _encode_messages(self, messages): | ||
return json.dumps({"generalDiagnostics": messages}) | ||
|
||
def test_format_message_with_character(self): | ||
location = Location(path="file.py", module=None, function=None, line=17, character=2) | ||
expected = Message(source="pyright", code="error", location=location, message="Important error") | ||
self.assertEqual( | ||
format_messages( | ||
self._encode_messages( | ||
[ | ||
{ | ||
"file": "file.py", | ||
"message": "Important error", | ||
"rule": "error", | ||
"range": {"start": {"line": 17, "character": 2}}, | ||
} | ||
] | ||
) | ||
), | ||
[expected], | ||
) | ||
|
||
def test_format_message_without_character(self): | ||
location = Location(path="file.py", module=None, function=None, line=17, character=-1) | ||
expected = Message(source="pyright", code="note", location=location, message="Important error") | ||
self.assertEqual( | ||
format_messages( | ||
self._encode_messages( | ||
[ | ||
{ | ||
"file": "file.py", | ||
"message": "Important error", | ||
"rule": "note", | ||
"range": {"start": {"line": 17}}, | ||
} | ||
] | ||
) | ||
), | ||
[expected], | ||
) | ||
|
||
def test_format_message_without_line(self): | ||
location = Location(path="file.py", module=None, function=None, line=-1, character=-1) | ||
expected = Message( | ||
source="pyright", | ||
code="error", | ||
location=location, | ||
message="Important error", | ||
) | ||
self.assertEqual( | ||
format_messages( | ||
self._encode_messages( | ||
[ | ||
{ | ||
"file": "file.py", | ||
"message": "Important error", | ||
"rule": "error", | ||
} | ||
] | ||
) | ||
), | ||
[expected], | ||
) |