Files
TorchNPU/test/distributed/checkpoint
ascend-robot 09e3ade912 [master] test: add test cases for _StoragePrefix
Co-authored-by: huowentan<3294682143@qq.com>



# message auto-generated for no-merge-commit merge:
!42901 merge api-storage-prefix-master into master

[master] test: add test cases for _StoragePrefix

Created-by: 2501_93637465
Commit-by: huowentan
Merged-by: ascend-robot
Description: # 【合入来源】

任务来源:https://gitcode.com/Ascend/pytorch/issues/3147

关联 Issue:https://gitcode.com/Ascend/pytorch/issues/3147

Fixes #3147

- [ ] 需求
- [ ] 问题单
- [x] issue/工单
- [ ] 重构优化
- [ ] 资料更新

API:`torch.distributed.checkpoint.filesystem._StoragePrefix`

目标分支:`master`

# 【修改方案】

经检索,PyTorch 社区现有用例缺少对
`torch.distributed.checkpoint.filesystem._StoragePrefix`
的独立、充分验证,因此新增测试文件:

`test/distributed/checkpoint/test_storage_prefix.py`

测试覆盖:

- 基本 prefix 构造;
- 空字符串、长路径和数字字符串;
- 特殊字符与 Unicode;
- prefix 属性访问和修改;
- 相同及不同 prefix 的相等性判断;
- 与非 `_StoragePrefix` 对象比较;
- `repr`、`fields` 和 `asdict`;
- metadata 场景使用;
- `None` 和缺少必选参数等边界场景。

该 API 是用于保存 checkpoint 存储前缀的 dataclass,仅处理字符串,
不涉及 Tensor、device 或 NPU 计算,因此无需进行 NPU 适配。

# 【资料变更】

`torch.distributed.checkpoint.filesystem._StoragePrefix` 是 PyTorch 私有接口。

根据 API 一致性任务指南,私有接口无需补充
`docs/zh/api/native_api` 支持资料,因此本 PR 不涉及资料变更。

# 【接口变更】

不涉及接口定义或功能实现变更,仅新增 API 一致性测试。

# 【功能验证】

| 测试项 | 结果 |
| --- | --- |
| `test/distributed/checkpoint/test_storage_prefix.py` | PASS |
| 15 个测试用例 | PASS |
| `python -m py_compile` | PASS |
| `git diff --check` | PASS |
| PR CI 流水线 | PASS |

测试结果:

```text
----------------------------------------------------------------------
Ran 15 tests

OK
```

# 【CheckList】

- [x] 使用完整 API 名称
- [x] 新增文件包含 Huawei copyright
- [x] 新增文件包含模块 docstring
- [x] 使用 `TestCase`、`run_tests` 和 `self.assert*`
- [x] 不包含 `print`、`try-except` 或 `import torch_npu`
- [x] 当前分支仅包含一个提交
- [x] CLA 和 CI 检查通过

See merge request: Ascend/pytorch!42901
2026-08-19 12:05:06 +08:00
..