Skip to content

Commit 07e12f4

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` includes such code 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 07e12f4

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

pytest_mpl/plugin.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363
ALL_IMAGE_FORMATS = RASTER_IMAGE_FORMATS + VECTOR_IMAGE_FORMATS
6464

6565

66+
def _get_item_dir(item):
67+
# .path is available starting from pytest 7, .fspath is for older versions.
68+
return getattr(item, "path", Path(item.fspath)).parent
69+
70+
6671
def _hash_file(in_stream):
6772
"""
6873
Hashes an already opened file.
@@ -445,19 +450,19 @@ def get_baseline_directory(self, item):
445450
baseline_dir = compare.kwargs.get('baseline_dir', None)
446451
if baseline_dir is None:
447452
if self.baseline_dir is None:
448-
baseline_dir = Path(item.fspath).parent / 'baseline'
453+
baseline_dir = _get_item_dir(item) / 'baseline'
449454
else:
450455
if self.baseline_relative_dir:
451456
# baseline dir is relative to the current test
452-
baseline_dir = Path(item.fspath).parent / self.baseline_relative_dir
457+
baseline_dir = _get_item_dir(item) / self.baseline_relative_dir
453458
else:
454459
# baseline dir is relative to where pytest was run
455460
baseline_dir = self.baseline_dir
456461

457462
baseline_remote = (isinstance(baseline_dir, str) and # noqa
458463
baseline_dir.startswith(('http://', 'https://')))
459464
if not baseline_remote:
460-
return Path(item.fspath).parent / baseline_dir
465+
return _get_item_dir(item) / baseline_dir
461466

462467
return baseline_dir
463468

@@ -686,7 +691,7 @@ def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
686691
hash_library_filename = compare.kwargs.get("hash_library", None) or self.hash_library
687692
if self._hash_library_from_cli: # for backwards compatibility
688693
hash_library_filename = self.hash_library
689-
hash_library_filename = (Path(item.fspath).parent / hash_library_filename).absolute()
694+
hash_library_filename = _get_item_dir(item) / hash_library_filename
690695

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

0 commit comments

Comments
 (0)