From 1ea16f1a34636d92083dedc45873841a3cf9ee64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Thu, 17 Oct 2024 17:13:56 +0200 Subject: [PATCH] Thread all the mypy options --- docs/profiles.rst | 24 ++--- prospector/tools/mypy/__init__.py | 91 +++++-------------- .../mypy/test_profiles/mypy_bad_options.yaml | 3 +- 3 files changed, 31 insertions(+), 87 deletions(-) diff --git a/docs/profiles.rst b/docs/profiles.rst index bf3b43ef..c947b7db 100644 --- a/docs/profiles.rst +++ b/docs/profiles.rst @@ -380,25 +380,15 @@ The available options are: +----------------+------------------------+----------------------------------------------+ | pylint | -anything- | Any of the `pylint options`_ | +----------------+------------------------+----------------------------------------------+ -| mypy | strict | strict mode | -+----------------+------------------------+----------------------------------------------+ -| mypy | follow-imports | How to treat imports | -+----------------+------------------------+----------------------------------------------+ -| mypy | ignore-missing-import | Silently ignore imports of missing modules | -+----------------+------------------------+----------------------------------------------+ -| mypy | platform | Check for the given platform | -+----------------+------------------------+----------------------------------------------+ -| mypy | python-version | assume it will be running on Python x.y | -+----------------+------------------------+----------------------------------------------+ -| mypy | strict-optional | Checking of Optional types and None values | -+----------------+------------------------+----------------------------------------------+ -| mypy | namespace-packages | Import discovery uses namespace packages | -+----------------+------------------------+----------------------------------------------+ | mypy | use-dmypy | Use mypy daemon (mypy server) for faster | | | | checks | +----------------+------------------------+----------------------------------------------+ -| mypy | check-untyped-defs |Type check the interior of functions without | -| | |type annotations | +| mypy | -anything-other- | Any of the `mypy options`_, | +| | | if the value is a list of string we it | +| | | corresponds to the argument like | +| | | `---` | +| | | e.g. `disallow: [any-expr, any-explicit]` => | +| | | `--disallow-any-expr --disallow-any-explicit`| +----------------+------------------------+----------------------------------------------+ | bandit | config | configuration filename | +----------------+------------------------+----------------------------------------------+ @@ -425,8 +415,6 @@ The available options are: | pyright | venv-path | Directory that contains virtual environments | +----------------+------------------------+----------------------------------------------+ -See `mypy options`_ for more details - See `bandit options`_ for more details See `pyright options`_ for more details diff --git a/prospector/tools/mypy/__init__.py b/prospector/tools/mypy/__init__.py index 2c45e11e..56391251 100644 --- a/prospector/tools/mypy/__init__.py +++ b/prospector/tools/mypy/__init__.py @@ -9,21 +9,6 @@ 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", - "check-untyped-defs", -] - def format_message(message): try: @@ -72,59 +57,29 @@ 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}" - ) - - 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) - check_untyped_defs = options.get("check-untyped-defs", 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") - - if check_untyped_defs: - self.options.append("--check-untyped-defs") - - for list_option in LIST_OPTIONS: - for entry in options.get(list_option, []): - self.options.append(f"--{list_option}-{entry}") + self.use_dmypy = options.pop("use-dmypy", False) + + # For backward compatibility + if "follow-imports" not in options: + options["follow-imports"] = "normal" + if "python-2-mode" in options and "py2" not in options: + options["py2"] = options.pop("python-2-mode") + + for name, value in options.items(): + if value is False: + continue + if value is True: + self.options.append(f"--{name}") + continue + if isinstance(value, (int, float, str)): + self.options.append(f"--{name}={value}") + continue + if isinstance(value, list): + for v in value: + self.options.append(f"--{name}-{v}") + continue + + raise BadToolConfig("mypy", f"The option {name} has an unsupported balue type: {type(value)}") def run(self, found_files): paths = [str(path) for path in found_files.python_modules] diff --git a/tests/tools/mypy/test_profiles/mypy_bad_options.yaml b/tests/tools/mypy/test_profiles/mypy_bad_options.yaml index 8233a612..fb175b26 100644 --- a/tests/tools/mypy/test_profiles/mypy_bad_options.yaml +++ b/tests/tools/mypy/test_profiles/mypy_bad_options.yaml @@ -1,4 +1,5 @@ mypy: run: yes options: - warn-unreachable: true + wrong-option-type: + test: true