From eee62ee18b2e222b90eb2ca41365dc47839c2f81 Mon Sep 17 00:00:00 2001 From: Eero Vaher Date: Wed, 22 Nov 2023 10:52:05 +0100 Subject: [PATCH] Allow using `-p no:legacypath` with `pytest` >= 7 The `-p no:legacypath` option for `pytest` prevents using legacy `py.path` and forces using `pathlib` from the Python standard library instead. 07e12f4fe360493899c53c172a8d1a02cfdd0d21 (Remove usage of `py.path` with `pytest` 7) attempted to remove all uses of `py.path` with `pytest` >= 7 so that downstream packages could use that option, (at least with recent enough `pytest`). That commit introduced using `getattr()` to access the `path` attribute of `_pytest.nodes.Node` instances and falling back to the `fspath` attribute if `path` does not exist (which happens with `pytest` < 7). However, evaluating the `default` for `getattr()` then guarantees that `fspath` is accessed, which is incompatible with `-p no:legacypath`. The simplest way of avoiding accessing `fspath` is to use explicitly version-dependent code. --- pytest_mpl/plugin.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pytest_mpl/plugin.py b/pytest_mpl/plugin.py index 4ab01dd..625bf99 100644 --- a/pytest_mpl/plugin.py +++ b/pytest_mpl/plugin.py @@ -41,6 +41,7 @@ from urllib.request import urlopen import pytest +from packaging.version import Version from pytest_mpl.summary.html import generate_summary_basic_html, generate_summary_html @@ -56,6 +57,8 @@ Actual shape: {actual_shape} {actual_path}""" +PYTEST_LT_7 = Version(pytest.__version__) < Version("7.0.0") + # The following are the subsets of formats supported by the Matplotlib image # comparison machinery RASTER_IMAGE_FORMATS = ['png'] @@ -64,8 +67,8 @@ def _get_item_dir(item): - # .path is available starting from pytest 7, .fspath is for older versions. - return getattr(item, "path", Path(item.fspath)).parent + path = Path(item.fspath) if PYTEST_LT_7 else item.path + return path.parent def _hash_file(in_stream):