Skip to content

Commit b577d9d

Browse files
committed
Refactor xdist integration
1 parent 6264093 commit b577d9d

File tree

2 files changed

+34
-39
lines changed

2 files changed

+34
-39
lines changed

src/pytest_mypy.py

+17-26
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,28 @@ def pytest_addoption(parser):
5959
)
6060

6161

62-
XDIST_WORKERINPUT_ATTRIBUTE_NAMES = (
63-
"workerinput",
64-
# xdist < 2.0.0:
65-
"slaveinput",
66-
)
62+
def _xdist_worker(config):
63+
try:
64+
return {"input": _xdist_workerinput(config)}
65+
except AttributeError:
66+
return {}
6767

6868

69-
def _get_xdist_workerinput(config_node):
70-
workerinput = None
71-
for attr_name in XDIST_WORKERINPUT_ATTRIBUTE_NAMES:
72-
workerinput = getattr(config_node, attr_name, None)
73-
if workerinput is not None:
74-
break
75-
return workerinput
76-
77-
78-
def _is_xdist_controller(config):
79-
"""
80-
True if the code running the given pytest.config object is running in
81-
an xdist controller node or not running xdist at all.
82-
"""
83-
return _get_xdist_workerinput(config) is None
69+
def _xdist_workerinput(node):
70+
try:
71+
return node.workerinput
72+
except AttributeError: # compat xdist < 2.0
73+
return node.slaveinput
8474

8575

8676
class MypyXdistControllerPlugin:
8777
"""A plugin that is only registered on xdist controller processes."""
8878

8979
def pytest_configure_node(self, node):
9080
"""Pass the config stash to workers."""
91-
_get_xdist_workerinput(node)["mypy_config_stash_serialized"] = (
92-
node.config.stash[stash_key["config"]].serialized()
93-
)
81+
_xdist_workerinput(node)["mypy_config_stash_serialized"] = node.config.stash[
82+
stash_key["config"]
83+
].serialized()
9484

9585

9686
def pytest_configure(config):
@@ -99,7 +89,7 @@ def pytest_configure(config):
9989
register a custom marker for MypyItems,
10090
and configure the plugin based on the CLI.
10191
"""
102-
if _is_xdist_controller(config):
92+
if not _xdist_worker(config):
10393
config.pluginmanager.register(MypyReportingPlugin())
10494

10595
# Get the path to a temporary file and delete it.
@@ -281,11 +271,12 @@ def from_mypy(
281271
@classmethod
282272
def from_session(cls, session) -> "MypyResults":
283273
"""Load (or generate) cached mypy results for a pytest session."""
284-
if _is_xdist_controller(session.config):
274+
xdist_worker = _xdist_worker(session.config)
275+
if not xdist_worker:
285276
mypy_config_stash = session.config.stash[stash_key["config"]]
286277
else:
287278
mypy_config_stash = MypyConfigStash.from_serialized(
288-
_get_xdist_workerinput(session.config)["mypy_config_stash_serialized"]
279+
xdist_worker["input"]["mypy_config_stash_serialized"]
289280
)
290281
mypy_results_path = mypy_config_stash.mypy_results_path
291282
with FileLock(str(mypy_results_path) + ".lock"):

tox.ini

+17-13
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
minversion = 4.4
44
isolated_build = true
55
envlist =
6-
py37-pytest{7.0, 7.x}-mypy{1.0, 1.x}
7-
py38-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}
8-
py39-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}
9-
py310-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}
10-
py311-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}
11-
py312-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}
6+
py37-pytest{7.0, 7.x}-mypy{1.0, 1.x}-xdist{1.x, 2.0, 2.x, 3.0, 3.x}
7+
py38-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}-xdist{1.x, 2.0, 2.x, 3.0, 3.x}
8+
py39-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}-xdist{1.x, 2.0, 2.x, 3.0, 3.x}
9+
py310-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}-xdist{1.x, 2.0, 2.x, 3.0, 3.x}
10+
py311-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}-xdist{1.x, 2.0, 2.x, 3.0, 3.x}
11+
py312-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}-xdist{1.x, 2.0, 2.x, 3.0, 3.x}
1212
publish
1313
static
1414

1515
[gh-actions]
1616
python =
17-
3.7: py37-pytest{7.0, 7.x}-mypy{1.0, 1.x}
18-
3.8: py38-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}, publish, static
19-
3.9: py39-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}
20-
3.10: py310-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}
21-
3.11: py311-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}
22-
3.12: py312-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}
17+
3.7: py37-pytest{7.0, 7.x}-mypy{1.0, 1.x}-xdist{1.x, 2.0, 2.x, 3.0, 3.x}
18+
3.8: py38-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}-xdist{1.x, 2.0, 2.x, 3.0, 3.x}, publish, static
19+
3.9: py39-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}-xdist{1.x, 2.0, 2.x, 3.0, 3.x}
20+
3.10: py310-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}-xdist{1.x, 2.0, 2.x, 3.0, 3.x}
21+
3.11: py311-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}-xdist{1.x, 2.0, 2.x, 3.0, 3.x}
22+
3.12: py312-pytest{7.0, 7.x, 8.0, 8.x}-mypy{1.0, 1.x}-xdist{1.x, 2.0, 2.x, 3.0, 3.x}
2323

2424
[testenv]
2525
constrain_package_deps = true
@@ -30,11 +30,15 @@ deps =
3030
pytest8.x: pytest ~= 8.0
3131
mypy1.0: mypy ~= 1.0.0
3232
mypy1.x: mypy ~= 1.0
33+
xdist1.x: pytest-xdist ~= 1.0
34+
xdist2.0: pytest-xdist ~= 2.0.0
35+
xdist2.x: pytest-xdist ~= 2.0
36+
xdist3.0: pytest-xdist ~= 3.0.0
37+
xdist3.x: pytest-xdist ~= 3.0
3338

3439
packaging ~= 21.3
3540
pytest-cov ~= 4.1.0
3641
pytest-randomly ~= 3.4
37-
pytest-xdist ~= 1.34
3842

3943
commands = pytest -p no:mypy {posargs:--cov pytest_mypy --cov-branch --cov-fail-under 100 --cov-report term-missing -n auto}
4044

0 commit comments

Comments
 (0)