refactor: implement registries as mutable mappings#1237
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the Register class across multiple packages to inherit from collections.abc.MutableMapping instead of dict, implementing the required abstract methods and updating direct dictionary accesses. It also introduces a comprehensive test suite. The reviewer suggested simplifying the test setup by using standard imports and modifying sys.path instead of dynamically loading modules with importlib.util.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
概述
本 PR 将注册表实现从继承具体的
dict类,改为继承collections.abc.MutableMapping。运行时、平台和训练模块中的注册表现在统一使用
_dict作为唯一的底层数据存储,同时提供完整且行为一致的映射接口。修改动机
此前的
Register类继承了dict,但注册对象实际上存储在单独的_dict属性中。一方面,会导致数据重复存储;另外一方面,会导致Register的行为可能与预期不符(使用者会认为Register应该像字典一样使用)。这会导致不同的映射操作访问不同的数据:
get()、keys()和items()使用_dict。len()、迭代、布尔值判断以及从dict继承的方法使用基类dict的内部存储。dict,但注册表的查询操作无法访问这些数据。
例如,即使注册表中已经包含注册对象,
len(registry)仍然可能返回零。使用
MutableMapping后,_dict将成为唯一的数据源,注册表的各种映射操作也会获得一致的行为。修改内容
MutableMapping。MutableMapping。MutableMapping。MutableMapping所需的映射基础方法:__getitem____setitem____delitem____iter____len___dict。_dict的直接访问,替换为公开的映射接口。兼容性
现有注册表 API 保持不变:
@registry@registry("name")registry[key]get()keys()items()merge()Register不再是具体的dict子类,而是实现标准的MutableMapping接口。经过仓库内调用点检查,没有发现依赖
Register必须是dict实例的代码。测试方案
新增测试覆盖以下内容:
MutableMappingupdate()、setdefault()、pop()、删除和clear()执行的测试命令:
python -B -m unittest discover \ -s test_cases \ -p 'test_registry_factory.py' \ -v测试结果:
Ruff 代码检查、格式检查、语法检查和 diff 检查也均已通过。
Summary
This PR replaces the concrete
dictinheritance used by the registryimplementations with
collections.abc.MutableMapping.The runtime, platform, and training registries now use
_dictas theirsingle backing store while providing a complete and consistent mapping
interface.
Motivation
Registerpreviously inherited fromdictwhile storing registeredobjects in a separate
_dictattribute.As a result, different mapping operations could observe different data:
get(),keys(), anditems()used_dict.len(), iteration, boolean evaluation, and inheriteddictoperationsused the base
dictstorage.dictbut were invisibleto registry lookup operations.
For example, a registry containing registered objects could still report
a length of zero.
Using
MutableMappingmakes_dictthe single source of truth and givesthe registry consistent mapping behavior.
Changes
MutableMapping.MutableMapping.MutableMapping.__getitem____setitem____delitem____iter____len___dictcorrectly from constructor arguments._dictaccess used by the Intel XPU alias with thepublic mapping interface.
Compatibility
The existing registry APIs remain unchanged:
@registry@registry("name")registry[key]get()keys()items()merge()Registeris no longer a concretedictsubclass. It now implements thestandard
MutableMappinginterface instead.No in-repository callers were found that depend on
Registerbeing aninstance of
dict.Test Plan
The new tests cover:
MutableMappingconformanceupdate(),setdefault(),pop(), deletion, andclear()Commands executed:
python -B -m unittest discover \ -s test_cases \ -p 'test_registry_factory.py' \ -vResult:
Ruff lint, formatting, syntax, and diff checks also passed.