Skip to content

Commit 3864526

Browse files
committed
Remove usage of py.path with pytest 7
`py.path` provides classes for representing filesystem paths, but became obsolete when `pathlib` was added to Python standard library. `pytest` recommends creating temporary directories with the `tmp_path` fixture, which uses `pathlib`, instead of the older `tmpdir` fixture, which uses `py.path`. Furthermore, it is suggested to call `pytest` with `-p no:legacypath` to remove support for `py.path` entirely, which helps ensure `tmpdir` is not used at all. However, this also breaks any code accessing `_pytest.nodes.Node.fspath`. Because `pytest-mpl` accesses that then packages using it cannot turn off `py.path` support to guard against `tmpdir` usage. Although replacing accessing `fspath` in older versions of `pytest` is complicated, it is very simple since `pytest` 7, so now at least the packages using recent versions of `pytest` can choose to make use of the `-p no:legacypath` option.
1 parent 95c440b commit 3864526

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

pytest_mpl/plugin.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from urllib.request import urlopen
4242

4343
import pytest
44+
from packaging.version import Version
4445

4546
from pytest_mpl.summary.html import generate_summary_basic_html, generate_summary_html
4647

@@ -63,6 +64,14 @@
6364
ALL_IMAGE_FORMATS = RASTER_IMAGE_FORMATS + VECTOR_IMAGE_FORMATS
6465

6566

67+
def _get_item_dir(item):
68+
if Version(pytest.__version__) < Version("7.0.0"):
69+
path = Path(item.fspath)
70+
else:
71+
path = item.path
72+
return path.parent
73+
74+
6675
def _hash_file(in_stream):
6776
"""
6877
Hashes an already opened file.
@@ -445,19 +454,19 @@ def get_baseline_directory(self, item):
445454
baseline_dir = compare.kwargs.get('baseline_dir', None)
446455
if baseline_dir is None:
447456
if self.baseline_dir is None:
448-
baseline_dir = Path(item.fspath).parent / 'baseline'
457+
baseline_dir = _get_item_dir(item) / 'baseline'
449458
else:
450459
if self.baseline_relative_dir:
451460
# baseline dir is relative to the current test
452-
baseline_dir = Path(item.fspath).parent / self.baseline_relative_dir
461+
baseline_dir = _get_item_dir(item) / self.baseline_relative_dir
453462
else:
454463
# baseline dir is relative to where pytest was run
455464
baseline_dir = self.baseline_dir
456465

457466
baseline_remote = (isinstance(baseline_dir, str) and # noqa
458467
baseline_dir.startswith(('http://', 'https://')))
459468
if not baseline_remote:
460-
return Path(item.fspath).parent / baseline_dir
469+
return _get_item_dir(item) / baseline_dir
461470

462471
return baseline_dir
463472

@@ -686,7 +695,7 @@ def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
686695
hash_library_filename = compare.kwargs.get("hash_library", None) or self.hash_library
687696
if self._hash_library_from_cli: # for backwards compatibility
688697
hash_library_filename = self.hash_library
689-
hash_library_filename = (Path(item.fspath).parent / hash_library_filename).absolute()
698+
hash_library_filename = _get_item_dir(item) / hash_library_filename
690699

691700
if not Path(hash_library_filename).exists():
692701
pytest.fail(f"Can't find hash library at path {hash_library_filename}")

0 commit comments

Comments
 (0)