Skip to content
Merged
16 changes: 15 additions & 1 deletion src/pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def pytest_addoption(parser):
action="store_true",
help="suppresses error messages about imports that cannot be resolved",
)
group.addoption(
"--mypy-config-file",
action="store",
type=str,
help="adds custom mypy config file",
)


XDIST_WORKERINPUT_ATTRIBUTE_NAMES = (
Expand Down Expand Up @@ -94,11 +100,19 @@ def pytest_configure_node(self, node): # xdist hook
if config.getoption("--mypy-ignore-missing-imports"):
mypy_argv.append("--ignore-missing-imports")

mypy_config_file = config.getoption("--mypy-config-file")
if mypy_config_file:
mypy_argv.append("--config-file={}".format(mypy_config_file))


def pytest_collect_file(path, parent):
"""Create a MypyFileItem for every file mypy should run on."""
if path.ext in {".py", ".pyi"} and any(
[parent.config.option.mypy, parent.config.option.mypy_ignore_missing_imports],
[
parent.config.option.mypy,
parent.config.option.mypy_config_file,
parent.config.option.mypy_ignore_missing_imports,
],
):
# Do not create MypyFile instance for a .py file if a
# .pyi file with the same name already exists;
Expand Down
28 changes: 28 additions & 0 deletions tests/test_pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,34 @@ def test_mypy_ignore_missings_imports(testdir, xdist_args):
assert result.ret == 0


def test_mypy_config_file(testdir, xdist_args):
"""Verify that --mypy-config-file works."""
testdir.makepyfile(
"""
def pyfunc(x):
return x * 2
""",
)
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
mypy_file_checks = 1
mypy_status_check = 1
mypy_checks = mypy_file_checks + mypy_status_check
result.assert_outcomes(passed=mypy_checks)
assert result.ret == 0
mypy_config_file = testdir.makeini(
"""
[mypy]
disallow_untyped_defs = True
""",
)
result = testdir.runpytest_subprocess(
"--mypy-config-file",
mypy_config_file,
*xdist_args,
)
result.assert_outcomes(failed=mypy_checks)


def test_mypy_marker(testdir, xdist_args):
"""Verify that -m mypy only runs the mypy tests."""
testdir.makepyfile(
Expand Down