Skip to content

Commit

Permalink
Populate the config stash on xdist workers
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtucker committed Aug 25, 2024
1 parent b577d9d commit 8a5fedc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
17 changes: 8 additions & 9 deletions src/pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ def pytest_configure(config):
register a custom marker for MypyItems,
and configure the plugin based on the CLI.
"""
if not _xdist_worker(config):
xdist_worker = _xdist_worker(config)
if not xdist_worker:
config.pluginmanager.register(MypyReportingPlugin())

# Get the path to a temporary file and delete it.
Expand All @@ -106,6 +107,11 @@ def pytest_configure(config):
# the workers so that they know where to read parsed results from.
if config.pluginmanager.getplugin("xdist"):
config.pluginmanager.register(MypyXdistControllerPlugin())
else:
# xdist workers create the stash using input from the controller plugin.
config.stash[stash_key["config"]] = MypyConfigStash.from_serialized(
xdist_worker["input"]["mypy_config_stash_serialized"]
)

config.addinivalue_line(
"markers",
Expand Down Expand Up @@ -271,14 +277,7 @@ def from_mypy(
@classmethod
def from_session(cls, session) -> "MypyResults":
"""Load (or generate) cached mypy results for a pytest session."""
xdist_worker = _xdist_worker(session.config)
if not xdist_worker:
mypy_config_stash = session.config.stash[stash_key["config"]]
else:
mypy_config_stash = MypyConfigStash.from_serialized(
xdist_worker["input"]["mypy_config_stash_serialized"]
)
mypy_results_path = mypy_config_stash.mypy_results_path
mypy_results_path = session.config.stash[stash_key["config"]].mypy_results_path
with FileLock(str(mypy_results_path) + ".lock"):
try:
with open(mypy_results_path, mode="r") as results_f:
Expand Down
11 changes: 3 additions & 8 deletions tests/test_pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,10 @@ def test_mypy_no_output(testdir, xdist_args):
conftest="""
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_terminal_summary(config):
@pytest.hookimpl(trylast=True)
def pytest_configure(config):
pytest_mypy = config.pluginmanager.getplugin("mypy")
try:
mypy_config_stash = config.stash[pytest_mypy.stash_key["config"]]
except KeyError:
# xdist worker
return
mypy_config_stash = config.stash[pytest_mypy.stash_key["config"]]
with open(mypy_config_stash.mypy_results_path, mode="w") as results_f:
pytest_mypy.MypyResults(
opts=[],
Expand All @@ -535,7 +531,6 @@ def pytest_terminal_summary(config):
abspath_errors={},
unmatched_stdout="",
).dump(results_f)
yield
""",
)
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
Expand Down

0 comments on commit 8a5fedc

Please sign in to comment.