Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread all the mypy options #682

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions docs/profiles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| | | `--<key>-<value>` |
| | | e.g. `disallow: [any-expr, any-explicit]` => |
| | | `--disallow-any-expr --disallow-any-explicit`|
+----------------+------------------------+----------------------------------------------+
| bandit | config | configuration filename |
+----------------+------------------------+----------------------------------------------+
Expand All @@ -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
Expand Down
91 changes: 23 additions & 68 deletions prospector/tools/mypy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion tests/tools/mypy/test_profiles/mypy_bad_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mypy:
run: yes
options:
warn-unreachable: true
wrong-option-type:
test: true
Loading