Skip to content

Commit a03ee02

Browse files
committed
config: make collect_ignore accept any PathLike
The main reason is to remove a reference to `py.path`.
1 parent b26d1bb commit a03ee02

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

doc/en/reference/reference.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ pytest treats some global variables in a special manner when defined in a test m
936936
**Tutorial**: :ref:`customizing-test-collection`
937937

938938
Can be declared in *conftest.py files* to exclude test directories or modules.
939-
Needs to be ``list[str]``.
939+
Needs to be a list of paths (``str``, :class:`pathlib.Path` or any :class:`os.PathLike`).
940940

941941
.. code-block:: python
942942

src/_pytest/config/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,9 +1445,7 @@ def _getconftest_pathlist(self, name: str, path: Path) -> Optional[List[Path]]:
14451445
modpath = Path(mod.__file__).parent
14461446
values: List[Path] = []
14471447
for relroot in relroots:
1448-
if isinstance(relroot, Path):
1449-
pass
1450-
elif isinstance(relroot, LEGACY_PATH):
1448+
if isinstance(relroot, os.PathLike):
14511449
relroot = Path(relroot)
14521450
else:
14531451
relroot = relroot.replace("/", os.sep)

testing/test_collection.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,19 @@ def pytest_ignore_collect(path, config):
367367
def test_collectignore_exclude_on_option(self, pytester: Pytester) -> None:
368368
pytester.makeconftest(
369369
"""
370-
# potentially avoid dependency on pylib
371-
from _pytest.compat import legacy_path
372370
from pathlib import Path
373-
collect_ignore = [legacy_path('hello'), 'test_world.py', Path('bye')]
371+
372+
class MyPathLike:
373+
def __init__(self, path):
374+
self.path = path
375+
def __fspath__(self):
376+
return "path"
377+
378+
collect_ignore = [MyPathLike('hello'), 'test_world.py', Path('bye')]
379+
374380
def pytest_addoption(parser):
375381
parser.addoption("--XX", action="store_true", default=False)
382+
376383
def pytest_configure(config):
377384
if config.getvalue("XX"):
378385
collect_ignore[:] = []

0 commit comments

Comments
 (0)