From ee5d38d3563da83d28166c77b3ff3a6113aa9552 Mon Sep 17 00:00:00 2001 From: Egemen Tuncarslan Date: Tue, 1 Sep 2026 15:39:12 +0300 Subject: [PATCH] Make the registry self-tests report the backends they promise Both files carry a header advertising a command: # python -m DeepDataMiningLearning.ngdet.detectors.base # Expected: prints the registered detector backends (after importing adapters). Run it and the answer is an empty list. Same for ngperception/depth/estimators/base.py. Under `python -m pkg.mod` the file executes as `__main__`; the `from . import ` inside the block then imports the adapters, which do `from .base import register, ...` -- loading this module a second time under its real name. `@register` populates that copy's registry, and `__main__` prints its own, still empty. CPython says as much: RuntimeWarning: 'DeepDataMiningLearning.ngdet.detectors.base' found in sys.modules after import of package 'DeepDataMiningLearning.ngdet.detectors', but prior to execution of '...base'; this may result in unpredictable behaviour Nothing outside the self-test is affected -- a normal `import` sees one module object and the registry fills correctly (verified: importing hf_detr takes DETECTOR_REGISTRY from [] to ['hf_detr']). The report is what is wrong, not the registry. The blocks now read the canonical module rather than their own globals: ngdet.detectors.base [] -> ['hf_detr', 'yolo', 'locate_anything'] depth.estimators.base [] -> ['hf_depth'] Only the `if __name__ == "__main__":` block changes in each file. I checked that the `Registered: {list(...)}` text inside build_detector / build_estimator is untouched -- an earlier attempt of mine rewrote that line by accident, which would have turned a helpful KeyError into a NameError. Both still raise KeyError naming the registered backends. Co-Authored-By: Claude Opus 5 --- DeepDataMiningLearning/ngdet/detectors/base.py | 9 +++++++-- .../ngperception/depth/estimators/base.py | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/DeepDataMiningLearning/ngdet/detectors/base.py b/DeepDataMiningLearning/ngdet/detectors/base.py index 70001c57..9854f63d 100644 --- a/DeepDataMiningLearning/ngdet/detectors/base.py +++ b/DeepDataMiningLearning/ngdet/detectors/base.py @@ -186,5 +186,10 @@ def build_detector(spec: str, taxonomy, device: str = "cuda", # Expected: prints the registered detector backends (after importing adapters). # =========================================================================== if __name__ == "__main__": - from . import hf_detr, yolo, locate_anything # noqa: F401 - print("Registered detector backends:", list(DETECTOR_REGISTRY)) + # Under `python -m` this file runs as `__main__`; importing an adapter + # loads it a *second* time under its real name, and @register fills that + # copy's registry rather than this one. Read the canonical module so the + # self-test reports what a normal import sees instead of an empty list. + from DeepDataMiningLearning.ngdet.detectors import base as _canonical + from DeepDataMiningLearning.ngdet.detectors import hf_detr, yolo, locate_anything # noqa: F401 + print("Registered detector backends:", list(_canonical.DETECTOR_REGISTRY)) diff --git a/DeepDataMiningLearning/ngperception/depth/estimators/base.py b/DeepDataMiningLearning/ngperception/depth/estimators/base.py index 8fb38e9e..206dfe3c 100644 --- a/DeepDataMiningLearning/ngperception/depth/estimators/base.py +++ b/DeepDataMiningLearning/ngperception/depth/estimators/base.py @@ -116,5 +116,10 @@ def build_estimator(spec: str, device: str = "cuda", **kwargs) -> BaseDepthEstim # Expected: prints the registered depth backends (after importing adapters). # =========================================================================== if __name__ == "__main__": - from . import hf_depth # noqa: F401 - print("Registered depth backends:", list(DEPTH_REGISTRY)) + # Under `python -m` this file runs as `__main__`; importing an adapter + # loads it a *second* time under its real name, and @register fills that + # copy's registry rather than this one. Read the canonical module so the + # self-test reports what a normal import sees instead of an empty list. + from DeepDataMiningLearning.ngperception.depth.estimators import base as _canonical + from DeepDataMiningLearning.ngperception.depth.estimators import hf_depth # noqa: F401 + print("Registered depth backends:", list(_canonical.DEPTH_REGISTRY))