12
12
import pytest
13
13
14
14
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
+
15
29
mypy_argv = []
16
30
nodeid_name = "mypy"
17
- stash_keys = {
18
- "mypy_results_path " : pytest .StashKey [Path ](),
31
+ stash_key = {
32
+ "config " : pytest .StashKey [MypyConfigStash ](),
19
33
}
20
34
terminal_summary_title = "mypy"
21
35
@@ -83,7 +97,9 @@ def pytest_configure(config):
83
97
# Subsequent MypyItems will see the file exists,
84
98
# and they will read the parsed results.
85
99
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
+ )
87
103
88
104
# If xdist is enabled, then the results path should be exposed to
89
105
# the workers so that they know where to read parsed results from.
@@ -92,8 +108,8 @@ def pytest_configure(config):
92
108
class _MypyXdistPlugin :
93
109
def pytest_configure_node (self , node ): # xdist hook
94
110
"""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 ()
97
113
)
98
114
99
115
config .pluginmanager .register (_MypyXdistPlugin ())
@@ -262,11 +278,13 @@ def from_mypy(
262
278
@classmethod
263
279
def from_session (cls , session ) -> "MypyResults" :
264
280
"""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
270
288
with FileLock (str (mypy_results_path ) + ".lock" ):
271
289
try :
272
290
with open (mypy_results_path , mode = "r" ) as results_f :
@@ -299,7 +317,7 @@ def pytest_terminal_summary(terminalreporter, config):
299
317
"""Report stderr and unrecognized lines from stdout."""
300
318
if not _is_xdist_controller (config ):
301
319
return
302
- mypy_results_path = config .stash [stash_keys [ "mypy_results_path " ]]
320
+ mypy_results_path = config .stash [stash_key [ "config " ]]. mypy_results_path
303
321
try :
304
322
with open (mypy_results_path , mode = "r" ) as results_f :
305
323
results = MypyResults .load (results_f )
0 commit comments