From bdedabb930487b8e71bd7266dfdfe30c9a22d8ba Mon Sep 17 00:00:00 2001 From: Rob Parolin Date: Wed, 25 Nov 2026 15:03:45 -0700 Subject: [PATCH] refactor(pathfinder): ask a search result whether it found something Callers told a hit from a miss with `isinstance(result, FindResult)`. Both result types now carry a `found` flag, so callers write `if result.found:` instead. The flag is typed as a literal `True` on `FindResult` and `False` on `NotFound`, so a type checker still narrows the type inside the `if`. To make that uniform, the two file probes in `search_platform.py` return a `FindResult` instead of a bare path, and `FindResult` moves there, next to `NotFound`. `search_steps.py` still exports it. No behavior change. 1291 passed, 6 skipped, and every comparison against `main` is still byte-identical, including simulated Windows. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin --- .../_dynamic_libs/load_nvidia_dynamic_lib.py | 8 ++--- .../_dynamic_libs/search_platform.py | 22 +++++++++--- .../pathfinder/_dynamic_libs/search_steps.py | 35 +++++-------------- cuda_pathfinder/tests/test_search_steps.py | 5 +++ 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py index c84b3e627d8..a46eb5632a0 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py @@ -156,7 +156,7 @@ def _try_ctk_root_canary(ctx: SearchContext) -> FindResult | NotFound: if ctk_root is None: continue result = find_via_ctk_root(ctx, ctk_root) - if isinstance(result, FindResult): + if result.found: return result missed += result return missed @@ -180,7 +180,7 @@ def _load_lib_no_cache(libname: str) -> LoadedDL: return loaded # Phase 3: Load from found path, or fall back to system search + late find. - if isinstance(early, FindResult): + if early.found: return LOADER.load_with_abs_path(desc, early.abs_path, early.found_via) loaded = LOADER.load_with_system_search(desc) @@ -188,13 +188,13 @@ def _load_lib_no_cache(libname: str) -> LoadedDL: return loaded late = run_find_steps(ctx, LATE_FIND_STEPS) - if isinstance(late, FindResult): + if late.found: return LOADER.load_with_abs_path(desc, late.abs_path, late.found_via) missed = early + late if desc.ctk_root_canary_anchor_libnames: canary = _try_ctk_root_canary(ctx) - if isinstance(canary, FindResult): + if canary.found: return LOADER.load_with_abs_path(desc, canary.abs_path, canary.found_via) missed += canary diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_platform.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_platform.py index 722c4fa171a..916ff315b14 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_platform.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_platform.py @@ -14,22 +14,34 @@ from collections.abc import Sequence from dataclasses import dataclass from pathlib import PurePath -from typing import NoReturn +from typing import ClassVar, Literal, NoReturn from cuda.pathfinder._dynamic_libs.lib_descriptor import LibDescriptor from cuda.pathfinder._dynamic_libs.load_dl_common import DynamicLibNotFoundError from cuda.pathfinder._dynamic_libs.supported_nvidia_libs import is_suppressed_dll_file from cuda.pathfinder._utils.find_sub_dirs import find_sub_dirs_all_sitepackages +from cuda.pathfinder._utils.found_via import FOUND_VIA_SITE_PACKAGES from cuda.pathfinder._utils.platform_aware import IS_WINDOWS from cuda.pathfinder._utils.windows_arch import windows_pe_matches_arch, windows_python_arch +@dataclass(frozen=True, slots=True) +class FindResult: + """A library file located on disk (not yet loaded).""" + + abs_path: str + found_via: str + #: Lets callers ask ``result.found`` of either result type. + found: ClassVar[Literal[True]] = True + + @dataclass(frozen=True, slots=True) class NotFound: """Why a search missed: the text for the final not-found error.""" error_messages: tuple[str, ...] = () attachments: tuple[str, ...] = () + found: ClassVar[Literal[False]] = False def __add__(self, other: NotFound) -> NotFound: return NotFound(self.error_messages + other.error_messages, self.attachments + other.attachments) @@ -159,14 +171,14 @@ def _find_lib_file(ctx: SearchContext, dirpath: str) -> str | None: return _find_descriptor_dll_under_dir(dirpath, ctx.desc, ctx.pe_arch_to_check) -def find_lib_in_site_packages(ctx: SearchContext) -> str | NotFound: +def find_lib_in_site_packages(ctx: SearchContext) -> FindResult | NotFound: sub_dirs_searched: list[tuple[str, ...]] = [] for rel_dir in ctx.site_packages_rel_dirs: sub_dir = PurePath(rel_dir).parts for abs_dir in find_sub_dirs_all_sitepackages(sub_dir): lib_path = _find_lib_file(ctx, abs_dir) if lib_path is not None: - return lib_path + return FindResult(lib_path, FOUND_VIA_SITE_PACKAGES) sub_dirs_searched.append(sub_dir) missed = NotFound() for sub_dir in sub_dirs_searched: @@ -174,10 +186,10 @@ def find_lib_in_site_packages(ctx: SearchContext) -> str | NotFound: return missed -def find_lib_in_dir(ctx: SearchContext, lib_dir: str) -> str | NotFound: +def find_lib_in_dir(ctx: SearchContext, lib_dir: str, found_via: str) -> FindResult | NotFound: lib_path = _find_lib_file(ctx, lib_dir) if lib_path is not None: - return lib_path + return FindResult(lib_path, found_via) if ctx.pe_arch_to_check is None: error_message = f"No such file: {ctx.lib_searched_for}" else: diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py index 180dfc4d20f..dc52271151c 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py @@ -26,9 +26,9 @@ import glob import os from collections.abc import Callable, Iterator -from dataclasses import dataclass from cuda.pathfinder._dynamic_libs.search_platform import ( + FindResult, NotFound, SearchContext, find_lib_in_dir, @@ -39,26 +39,12 @@ FOUND_VIA_CONDA, FOUND_VIA_CUDA_PATH, FOUND_VIA_PROGRAM_FILES, - FOUND_VIA_SITE_PACKAGES, FOUND_VIA_SYSTEM_CTK_ROOT, ) from cuda.pathfinder._utils.path_sort import numeric_aware_path_sort_key from .._utils.resolve_path import resolve_path -# --------------------------------------------------------------------------- -# Data types -# --------------------------------------------------------------------------- - - -@dataclass(frozen=True) -class FindResult: - """A library file located on disk (not yet loaded).""" - - abs_path: str - found_via: str - - #: Type alias for a find step callable. FindStep = Callable[[SearchContext], FindResult | NotFound] @@ -80,10 +66,10 @@ def _find_under_root( """Resolve *rel_dirs* under *root*, then find the requested library.""" missed = NotFound() for lib_dir in _iter_lib_dirs(root, rel_dirs): - found = find_lib_in_dir(ctx, lib_dir) - if not isinstance(found, NotFound): - return FindResult(found, found_via) - missed += found + result = find_lib_in_dir(ctx, lib_dir, found_via) + if result.found: + return result + missed += result return missed @@ -155,10 +141,7 @@ def find_in_site_packages(ctx: SearchContext) -> FindResult | NotFound: """Search pip wheel install locations.""" if not ctx.site_packages_rel_dirs: return NotFound() - found = find_lib_in_site_packages(ctx) - if isinstance(found, NotFound): - return found - return FindResult(found, FOUND_VIA_SITE_PACKAGES) + return find_lib_in_site_packages(ctx) def find_in_conda(ctx: SearchContext) -> FindResult | NotFound: @@ -179,7 +162,7 @@ def find_in_install_root_env_vars(ctx: SearchContext) -> FindResult | NotFound: if not root: continue result = _find_under_root(ctx, root, rel_dirs, env_var) - if isinstance(result, FindResult): + if result.found: return result missed += result return missed @@ -210,7 +193,7 @@ def find_in_program_files_roots(ctx: SearchContext) -> FindResult | NotFound: if not os.path.isdir(root): continue result = _find_under_anchor_root(ctx, str(resolve_path(root)), FOUND_VIA_PROGRAM_FILES) - if isinstance(result, FindResult): + if result.found: return result missed += result return missed @@ -241,7 +224,7 @@ def run_find_steps(ctx: SearchContext, steps: tuple[FindStep, ...]) -> FindResul missed = NotFound() for step in steps: result = step(ctx) - if isinstance(result, FindResult): + if result.found: return result missed += result return missed diff --git a/cuda_pathfinder/tests/test_search_steps.py b/cuda_pathfinder/tests/test_search_steps.py index 8500698ba10..81bcd6bfdc2 100644 --- a/cuda_pathfinder/tests/test_search_steps.py +++ b/cuda_pathfinder/tests/test_search_steps.py @@ -955,6 +955,11 @@ def test_combines_misses_in_step_order(self): def test_empty_steps(self): assert run_find_steps(_ctx(), ()) == NotFound() + @pytest.mark.agent_authored(model="claude-fable-5-2") + def test_both_result_types_say_whether_they_found_something(self): + assert FindResult("/lib/libfoo.so", "conda").found is True + assert NotFound(("No such file: libfoo.so",)).found is False + def test_skips_misses_returns_later_hit(self): hit = FindResult("/later/lib.so", "step-c") result = run_find_steps(_ctx(), (lambda _: NotFound(), lambda _: hit))