Skip to content

Commit 90e1f32

Browse files
committed
Use config.stash to store the results path
1 parent 826bccb commit 90e1f32

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/pytest_mypy.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
mypy_argv = []
1616
nodeid_name = "mypy"
17+
stash_keys = {
18+
"mypy_results_path": pytest.StashKey[Path](),
19+
}
1720
terminal_summary_title = "mypy"
1821

1922

@@ -80,18 +83,18 @@ def pytest_configure(config):
8083
# Subsequent MypyItems will see the file exists,
8184
# and they will read the parsed results.
8285
with NamedTemporaryFile(delete=True) as tmp_f:
83-
config._mypy_results_path = tmp_f.name
86+
config.stash[stash_keys["mypy_results_path"]] = Path(tmp_f.name)
8487

8588
# If xdist is enabled, then the results path should be exposed to
8689
# the workers so that they know where to read parsed results from.
8790
if config.pluginmanager.getplugin("xdist"):
8891

8992
class _MypyXdistPlugin:
9093
def pytest_configure_node(self, node): # xdist hook
91-
"""Pass config._mypy_results_path to workers."""
92-
_get_xdist_workerinput(node)[
93-
"_mypy_results_path"
94-
] = node.config._mypy_results_path
94+
"""Pass the mypy results path to workers."""
95+
_get_xdist_workerinput(node)["_mypy_results_path"] = str(
96+
node.config.stash[stash_keys["mypy_results_path"]]
97+
)
9598

9699
config.pluginmanager.register(_MypyXdistPlugin())
97100

@@ -259,14 +262,14 @@ def from_mypy(
259262
@classmethod
260263
def from_session(cls, session) -> "MypyResults":
261264
"""Load (or generate) cached mypy results for a pytest session."""
262-
results_path = (
263-
session.config._mypy_results_path
265+
mypy_results_path = Path(
266+
session.config.stash[stash_keys["mypy_results_path"]]
264267
if _is_xdist_controller(session.config)
265268
else _get_xdist_workerinput(session.config)["_mypy_results_path"]
266269
)
267-
with FileLock(results_path + ".lock"):
270+
with FileLock(str(mypy_results_path) + ".lock"):
268271
try:
269-
with open(results_path, mode="r") as results_f:
272+
with open(mypy_results_path, mode="r") as results_f:
270273
results = cls.load(results_f)
271274
except FileNotFoundError:
272275
results = cls.from_mypy(
@@ -276,7 +279,7 @@ def from_session(cls, session) -> "MypyResults":
276279
if isinstance(item, MypyFileItem)
277280
],
278281
)
279-
with open(results_path, mode="w") as results_f:
282+
with open(mypy_results_path, mode="w") as results_f:
280283
results.dump(results_f)
281284
return results
282285

@@ -296,8 +299,9 @@ def pytest_terminal_summary(terminalreporter, config):
296299
"""Report stderr and unrecognized lines from stdout."""
297300
if not _is_xdist_controller(config):
298301
return
302+
mypy_results_path = config.stash[stash_keys["mypy_results_path"]]
299303
try:
300-
with open(config._mypy_results_path, mode="r") as results_f:
304+
with open(mypy_results_path, mode="r") as results_f:
301305
results = MypyResults.load(results_f)
302306
except FileNotFoundError:
303307
# No MypyItems executed.
@@ -309,4 +313,4 @@ def pytest_terminal_summary(terminalreporter, config):
309313
terminalreporter.write_line(results.unmatched_stdout, **color)
310314
if results.stderr:
311315
terminalreporter.write_line(results.stderr, yellow=True)
312-
Path(config._mypy_results_path).unlink()
316+
mypy_results_path.unlink()

tests/test_pytest_mypy.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,13 @@ def test_mypy_no_output(testdir, xdist_args):
520520
521521
@pytest.hookimpl(hookwrapper=True)
522522
def pytest_terminal_summary(config):
523-
mypy_results_path = getattr(config, "_mypy_results_path", None)
524-
if not mypy_results_path:
523+
pytest_mypy = config.pluginmanager.getplugin("mypy")
524+
stash_key = pytest_mypy.stash_keys["mypy_results_path"]
525+
try:
526+
mypy_results_path = config.stash[stash_key]
527+
except KeyError:
525528
# xdist worker
526529
return
527-
pytest_mypy = config.pluginmanager.getplugin("mypy")
528530
with open(mypy_results_path, mode="w") as results_f:
529531
pytest_mypy.MypyResults(
530532
opts=[],

0 commit comments

Comments
 (0)