Skip to content

refactor: implement registries as mutable mappings#1237

Open
GlowLED wants to merge 2 commits into
ModelTC:mainfrom
GlowLED:refactor/register-dict-inheritance
Open

refactor: implement registries as mutable mappings#1237
GlowLED wants to merge 2 commits into
ModelTC:mainfrom
GlowLED:refactor/register-dict-inheritance

Conversation

@GlowLED

@GlowLED GlowLED commented Jul 11, 2026

Copy link
Copy Markdown

概述

本 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
  • 将 Intel XPU 别名注册中对 _dict 的直接访问,替换为公开的映射接口。
  • 添加覆盖三种注册表实现的单元测试。

兼容性

现有注册表 API 保持不变:

  • @registry
  • @registry("name")
  • registry[key]
  • get()
  • keys()
  • items()
  • 成员检查
  • merge()

Register 不再是具体的 dict 子类,而是实现标准的MutableMapping 接口。

经过仓库内调用点检查,没有发现依赖 Register 必须是 dict 实例的代码。

测试方案

新增测试覆盖以下内容:

  • 是否正确实现 MutableMapping
  • 使用映射和关键字参数初始化注册表
  • 带名称和不带名称的装饰器注册
  • 查找和迭代
  • 重复注册和非法注册
  • update()setdefault()pop()、删除和 clear()
  • 注册表合并以及合并冲突
  • 运行时、平台和训练模块中的三种注册表实现

执行的测试命令:

python -B -m unittest discover \
  -s test_cases \
  -p 'test_registry_factory.py' \
  -v

测试结果:

Ran 6 tests
OK

Ruff 代码检查、格式检查、语法检查和 diff 检查也均已通过。

Summary

This PR replaces the concrete dict inheritance used by the registry
implementations with collections.abc.MutableMapping.

The runtime, platform, and training registries now use _dict as their
single backing store while providing a complete and consistent mapping
interface.

Motivation

Register previously inherited from dict while storing registered
objects in a separate _dict attribute.

As a result, different mapping operations could observe different data:

  • Registration, get(), keys(), and items() used _dict.
  • len(), iteration, boolean evaluation, and inherited dict operations
    used the base dict storage.
  • Constructor arguments were stored in the base dict but were invisible
    to registry lookup operations.

For example, a registry containing registered objects could still report
a length of zero.

Using MutableMapping makes _dict the single source of truth and gives
the registry consistent mapping behavior.

Changes

  • Migrate the runtime registry to MutableMapping.
  • Migrate the platform registry to MutableMapping.
  • Migrate the training registry to MutableMapping.
  • Implement the required mapping primitives:
    • __getitem__
    • __setitem__
    • __delitem__
    • __iter__
    • __len__
  • Initialize _dict correctly from constructor arguments.
  • Replace the direct _dict access used by the Intel XPU alias with the
    public mapping interface.
  • Add unit tests covering all three registry implementations.

Compatibility

The existing registry APIs remain unchanged:

  • @registry
  • @registry("name")
  • registry[key]
  • get()
  • keys()
  • items()
  • membership checks
  • merge()

Register is no longer a concrete dict subclass. It now implements the
standard MutableMapping interface instead.

No in-repository callers were found that depend on Register being an
instance of dict.

Test Plan

The new tests cover:

  • MutableMapping conformance
  • initialization from mappings and keyword arguments
  • named and unnamed decorator registration
  • lookup and iteration
  • duplicate and invalid registration
  • update(), setdefault(), pop(), deletion, and clear()
  • registry merging and merge conflicts
  • runtime, platform, and training registry implementations

Commands executed:

python -B -m unittest discover \
  -s test_cases \
  -p 'test_registry_factory.py' \
  -v

Result:

  Ran 6 tests
  OK

Ruff lint, formatting, syntax, and diff checks also passed.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread test_cases/test_registry_factory.py Outdated
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant