Skip to content

Commit 2ee7bd4

Browse files
authored
Merge pull request #125 from Mohsin-Ul-Islam/config-file-flag
feat: Allow pytest-mypy to pass mypy custom config file via flag
2 parents 3dc75df + 8de5139 commit 2ee7bd4

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/pytest_mypy.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ def pytest_addoption(parser):
3232
action="store_true",
3333
help="suppresses error messages about imports that cannot be resolved",
3434
)
35+
group.addoption(
36+
"--mypy-config-file",
37+
action="store",
38+
type=str,
39+
help="adds custom mypy config file",
40+
)
3541

3642

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

103+
mypy_config_file = config.getoption("--mypy-config-file")
104+
if mypy_config_file:
105+
mypy_argv.append("--config-file={}".format(mypy_config_file))
106+
97107

98108
def pytest_collect_file(path, parent):
99109
"""Create a MypyFileItem for every file mypy should run on."""
100110
if path.ext in {".py", ".pyi"} and any(
101-
[parent.config.option.mypy, parent.config.option.mypy_ignore_missing_imports],
111+
[
112+
parent.config.option.mypy,
113+
parent.config.option.mypy_config_file,
114+
parent.config.option.mypy_ignore_missing_imports,
115+
],
102116
):
103117
# Do not create MypyFile instance for a .py file if a
104118
# .pyi file with the same name already exists;

tests/test_pytest_mypy.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,34 @@ def test_mypy_ignore_missings_imports(testdir, xdist_args):
129129
assert result.ret == 0
130130

131131

132+
def test_mypy_config_file(testdir, xdist_args):
133+
"""Verify that --mypy-config-file works."""
134+
testdir.makepyfile(
135+
"""
136+
def pyfunc(x):
137+
return x * 2
138+
""",
139+
)
140+
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
141+
mypy_file_checks = 1
142+
mypy_status_check = 1
143+
mypy_checks = mypy_file_checks + mypy_status_check
144+
result.assert_outcomes(passed=mypy_checks)
145+
assert result.ret == 0
146+
mypy_config_file = testdir.makeini(
147+
"""
148+
[mypy]
149+
disallow_untyped_defs = True
150+
""",
151+
)
152+
result = testdir.runpytest_subprocess(
153+
"--mypy-config-file",
154+
mypy_config_file,
155+
*xdist_args,
156+
)
157+
result.assert_outcomes(failed=mypy_checks)
158+
159+
132160
def test_mypy_marker(testdir, xdist_args):
133161
"""Verify that -m mypy only runs the mypy tests."""
134162
testdir.makepyfile(

0 commit comments

Comments
 (0)