Skip to content

Commit c15488a

Browse files
eleanorjboydCopilot
andcommitted
Fix pytest test harness type errors
Keep the subprocess cwd annotation compatible with pathlib callers and simplify the local Black fixture for the supported pytest 8.4+ range so the CI Pyright version can analyze it cleanly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b9fe1a4 commit c15488a

2 files changed

Lines changed: 18 additions & 34 deletions

File tree

python_files/tests/pytestadapter/.data/2496-black-formatter/conftest.py

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# Local replacement for the pytest-black plugin that supports pytest 7+.
2+
# Local replacement for the pytest-black plugin that supports pytest 8.4+.
33
# pytest-black 0.6.0 uses the deprecated `path` argument in pytest_collect_file,
44
# which was removed in pytest 8.1. This conftest provides a compatible implementation.
55

@@ -9,15 +9,10 @@
99

1010
import pytest
1111

12-
PYTEST_VER = tuple(int(x) for x in pytest.__version__.split(".")[:2])
13-
1412
try:
15-
import tomllib
13+
import tomli
1614
except ImportError:
17-
try:
18-
import tomli as tomllib # type: ignore[no-redef]
19-
except ImportError:
20-
tomllib = None # type: ignore[assignment]
15+
tomli = None # type: ignore[assignment]
2116

2217
HISTKEY = "black/mtimes"
2318

@@ -42,28 +37,14 @@ def pytest_unconfigure(config):
4237
config.cache.set(HISTKEY, config._blackmtimes)
4338

4439

45-
if PYTEST_VER >= (8, 1):
46-
47-
def pytest_collect_file(file_path, parent):
48-
config = parent.config
49-
if (
50-
config.option.black
51-
and file_path.suffix in (".py", ".pyi")
52-
and file_path.name != "conftest.py"
53-
):
54-
return BlackFile.from_parent(parent, path=file_path)
55-
56-
elif PYTEST_VER >= (7, 0):
57-
58-
def pytest_collect_file(file_path, path, parent): # type: ignore[misc] # noqa: ARG001
59-
# `path` must match the pytest 7.x hookspec; use file_path (pathlib.Path) in body.
60-
config = parent.config
61-
if (
62-
config.option.black
63-
and file_path.suffix in (".py", ".pyi")
64-
and file_path.name != "conftest.py"
65-
):
66-
return BlackFile.from_parent(parent, path=file_path)
40+
def pytest_collect_file(file_path, parent):
41+
config = parent.config
42+
if (
43+
config.option.black
44+
and file_path.suffix in (".py", ".pyi")
45+
and file_path.name != "conftest.py"
46+
):
47+
return BlackFile.from_parent(parent, path=file_path)
6748

6849

6950
class BlackFile(pytest.File):
@@ -77,9 +58,9 @@ def __init__(self, **kwargs):
7758
super().__init__(**kwargs)
7859
self.add_marker("black")
7960
try:
80-
if tomllib is not None:
61+
if tomli is not None:
8162
with open("pyproject.toml", "rb") as toml_file:
82-
settings = tomllib.load(toml_file)["tool"]["black"]
63+
settings = tomli.load(toml_file)["tool"]["black"]
8364
if "include" in settings:
8465
settings["include"] = self._re_fix_verbose(settings["include"])
8566
if "exclude" in settings:

python_files/tests/pytestadapter/helpers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import tempfile
1313
import threading
1414
import uuid
15-
from typing import Any, Dict, List, Optional, Tuple
15+
from typing import Any, Dict, List, Optional, Tuple, Union
1616

1717
if sys.platform == "win32":
1818
from namedpipe import NPopen # pylint: disable=import-error # cspell: disable-line
@@ -245,7 +245,10 @@ def _listen_on_pipe_new(listener, result: List[str], completed: threading.Event)
245245

246246

247247
def _run_test_code(
248-
proc_args: List[str], proc_env, proc_cwd: str, completed: threading.Event
248+
proc_args: List[str],
249+
proc_env,
250+
proc_cwd: Union[str, os.PathLike[str]],
251+
completed: threading.Event,
249252
) -> subprocess.CompletedProcess:
250253
try:
251254
return subprocess.run(

0 commit comments

Comments
 (0)