Skip to content

Commit 9d03d95

Browse files
committed
Create MypyConfigStash
1 parent 9d1e6d9 commit 9d03d95

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/pytest_mypy.py

+29-11
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,24 @@
1212
import pytest
1313

1414

15+
@dataclass(frozen=True) # compat python < 3.10 (kw_only=True)
16+
class MypyConfigStash:
17+
"""Plugin data stored in the pytest.Config stash."""
18+
19+
mypy_results_path: Path
20+
21+
@classmethod
22+
def from_serialized(cls, serialized):
23+
return cls(mypy_results_path=Path(serialized))
24+
25+
def serialized(self):
26+
return str(self.mypy_results_path)
27+
28+
1529
mypy_argv = []
1630
nodeid_name = "mypy"
17-
stash_keys = {
18-
"mypy_results_path": pytest.StashKey[Path](),
31+
stash_key = {
32+
"config": pytest.StashKey[MypyConfigStash](),
1933
}
2034
terminal_summary_title = "mypy"
2135

@@ -83,7 +97,9 @@ def pytest_configure(config):
8397
# Subsequent MypyItems will see the file exists,
8498
# and they will read the parsed results.
8599
with NamedTemporaryFile(delete=True) as tmp_f:
86-
config.stash[stash_keys["mypy_results_path"]] = Path(tmp_f.name)
100+
config.stash[stash_key["config"]] = MypyConfigStash(
101+
mypy_results_path=Path(tmp_f.name),
102+
)
87103

88104
# If xdist is enabled, then the results path should be exposed to
89105
# the workers so that they know where to read parsed results from.
@@ -92,8 +108,8 @@ def pytest_configure(config):
92108
class _MypyXdistPlugin:
93109
def pytest_configure_node(self, node): # xdist hook
94110
"""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"]]
111+
_get_xdist_workerinput(node)["mypy_config_stash_serialized"] = (
112+
node.config.stash[stash_key["config"]].serialized()
97113
)
98114

99115
config.pluginmanager.register(_MypyXdistPlugin())
@@ -262,11 +278,13 @@ def from_mypy(
262278
@classmethod
263279
def from_session(cls, session) -> "MypyResults":
264280
"""Load (or generate) cached mypy results for a pytest session."""
265-
mypy_results_path = Path(
266-
session.config.stash[stash_keys["mypy_results_path"]]
267-
if _is_xdist_controller(session.config)
268-
else _get_xdist_workerinput(session.config)["_mypy_results_path"]
269-
)
281+
if _is_xdist_controller(session.config):
282+
mypy_config_stash = session.config.stash[stash_key["config"]]
283+
else:
284+
mypy_config_stash = MypyConfigStash.from_serialized(
285+
_get_xdist_workerinput(session.config)["mypy_config_stash_serialized"]
286+
)
287+
mypy_results_path = mypy_config_stash.mypy_results_path
270288
with FileLock(str(mypy_results_path) + ".lock"):
271289
try:
272290
with open(mypy_results_path, mode="r") as results_f:
@@ -299,7 +317,7 @@ def pytest_terminal_summary(terminalreporter, config):
299317
"""Report stderr and unrecognized lines from stdout."""
300318
if not _is_xdist_controller(config):
301319
return
302-
mypy_results_path = config.stash[stash_keys["mypy_results_path"]]
320+
mypy_results_path = config.stash[stash_key["config"]].mypy_results_path
303321
try:
304322
with open(mypy_results_path, mode="r") as results_f:
305323
results = MypyResults.load(results_f)

tests/test_pytest_mypy.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -521,13 +521,12 @@ def test_mypy_no_output(testdir, xdist_args):
521521
@pytest.hookimpl(hookwrapper=True)
522522
def pytest_terminal_summary(config):
523523
pytest_mypy = config.pluginmanager.getplugin("mypy")
524-
stash_key = pytest_mypy.stash_keys["mypy_results_path"]
525524
try:
526-
mypy_results_path = config.stash[stash_key]
525+
mypy_config_stash = config.stash[pytest_mypy.stash_key["config"]]
527526
except KeyError:
528527
# xdist worker
529528
return
530-
with open(mypy_results_path, mode="w") as results_f:
529+
with open(mypy_config_stash.mypy_results_path, mode="w") as results_f:
531530
pytest_mypy.MypyResults(
532531
opts=[],
533532
stdout="",

0 commit comments

Comments
 (0)