From fc236af513d8cf6789ddedf94db878c8b97628cb Mon Sep 17 00:00:00 2001 From: a Date: Wed, 14 Aug 2024 14:31:49 +0800 Subject: [PATCH] fix: pytest >=8.1.0 displays no diff for AssertionError with --import-mode=importlib(#12659) --- src/_pytest/pathlib.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/_pytest/pathlib.py b/src/_pytest/pathlib.py index e4dc4eddc9c..a3ce87ae4cd 100644 --- a/src/_pytest/pathlib.py +++ b/src/_pytest/pathlib.py @@ -10,6 +10,7 @@ import fnmatch from functools import partial from importlib.machinery import ModuleSpec +from importlib.machinery import PathFinder import importlib.util import itertools import os @@ -817,7 +818,10 @@ def resolve_pkg_root_and_module_name( if pkg_root is not None: module_name = compute_module_name(pkg_root, path) if module_name: - return pkg_root, module_name + spec = PathFinder.find_spec(module_name, [str(pkg_root)]) + if spec is not None: + # Verify that pkk_root and module_name are valid before returning(#12659) + return pkg_root, module_name raise CouldNotResolvePathError(f"Could not resolve for {path}")