Skip to content

Commit

Permalink
[#455] Raising an exception if bad mypy options are found
Browse files Browse the repository at this point in the history
  • Loading branch information
carlio committed Mar 1, 2022
1 parent 30114cb commit c163370
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Just say no to bugs.

* Stopped the ProfileValidator tool raising errors about ``pep8`` and ``pep257`` sections being unknown. Instead, they raise deprecated warnings.
* Blending works again - for example, pylint and pycodestyle errors representing the same thing are combined. After renaming pep8 to pycodestyle, this only worked when using legacy names.
* Unrecognised Mypy options now raise an exception instead of silently carrying on - `#455 <https://github.com/PyCQA/prospector/issues/455>`_

**Tidyup**:

Expand Down
4 changes: 0 additions & 4 deletions MANIFEST.in

This file was deleted.

3 changes: 3 additions & 0 deletions prospector/tools/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class BadToolConfig(Exception):
def __init__(self, tool_name: str, message: str):
super().__init__(f"Bad option value found for tool {tool_name}: {message}")
25 changes: 22 additions & 3 deletions prospector/tools/mypy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@

__all__ = ("MypyTool",)


MYPY_OPTIONS = ["allow", "check", "disallow", "no-check", "no-warn", "warn"]
from prospector.tools.exceptions import BadToolConfig

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


def format_message(message):
Expand Down Expand Up @@ -47,6 +59,13 @@ def __init__(self, *args, **kwargs):
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}"
)

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

follow_imports = options.get("follow-imports", "normal")
Expand Down Expand Up @@ -84,7 +103,7 @@ def configure(self, prospector_config, _):
if namespace_packages:
self.options.append("--namespace-packages")

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

Expand Down
21 changes: 21 additions & 0 deletions tests/tools/mypy/test_mypy_tool.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from pathlib import Path
from unittest import SkipTest, TestCase
from unittest.mock import patch

from prospector.config import ProspectorConfig
from prospector.finder import find_python
from prospector.message import Location, Message
from prospector.tools.exceptions import BadToolConfig

try:
from prospector.tools.mypy import format_message
Expand All @@ -9,6 +14,22 @@


class TestMypyTool(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 = find_python([], [], True, Path(__file__).parent.absolute())
self.assertRaises(BadToolConfig, self._get_config("mypy_bad_options").get_tools, finder)

def test_good_options(self):
finder = find_python([], [], True, Path(__file__).parent.absolute())
self._get_config("mypy_good_options").get_tools(finder)


class TestMypyMessageFormat(TestCase):
def test_format_message_with_character(self):
location = Location(path="file.py", module=None, function=None, line=17, character=2)
expected = Message(source="mypy", code="error", location=location, message="Important error")
Expand Down
5 changes: 5 additions & 0 deletions tests/tools/mypy/test_profiles/mypy_bad_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

mypy:
run: yes
options:
warn-unreachable: true
6 changes: 6 additions & 0 deletions tests/tools/mypy/test_profiles/mypy_good_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

mypy:
run: yes
options:
warn:
- unreachable

0 comments on commit c163370

Please sign in to comment.