已合并
test: Add tests for StringTable.default_factory #43210
test: Add tests for StringTable.default_factory #43210
已合并
Flipped创建于 7月29日
1 个文件变更+61-12
@@ -1,19 +1,35 @@
1+# Copyright (c) 2026 Huawei Technologies Co., Ltd
2+# All rights reserved.
3+#
4+# Licensed under the BSD 3-Clause License (the "License");
5+# you may not use this file except in compliance with the License.
6+# You may obtain a copy of the License at
7+#
8+# https://opensource.org/licenses/BSD-3-Clause
9+#
10+# Unless required by applicable law or agreed to in writing, software
11+# distributed under the License is distributed on an "AS IS" BASIS,
12+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+# implied.
14+# See the License for the specific language governing permissions and
15+# limitations under the License.
16+ 
1"""17"""
2Add validation cases for torch.autograd.profiler_util.StringTable on NPU:18Add validation cases for torch.autograd.profiler_util.StringTable on NPU:
3-1. PyTorch community lacks direct API validations for StringTable.pop.19+1. PyTorch community lacks sufficient and direct API validations for some
4-2. This file validates StringTable.pop (extendable).20+ torch.autograd.profiler_util.StringTable APIs.
21+2. This file validates torch.autograd.profiler_util.StringTable.pop and
22+ torch.autograd.profiler_util.StringTable.default_factory (extendable).
5"""23"""
6 24 
7-import torch25+from torch.autograd.profiler_util import StringTable
8-from torch.testing._internal.common_utils import run_tests, TestCase26+from torch.testing._internal.common_utils import TestCase, run_tests
9 27 
10 28 
11class TestStringTable(TestCase):29class TestStringTable(TestCase):
12 30 
13 def test_pop_existing_key(self):31 def test_pop_existing_key(self):
14 """Validate StringTable.pop returns value and removes key when key exists."""32 """Validate StringTable.pop returns value and removes key when key exists."""
15- from torch.autograd.profiler_util import StringTable
16- 
17 st = StringTable()33 st = StringTable()
18 st["key1"] = "value1"34 st["key1"] = "value1"
19 result = st.pop("key1")35 result = st.pop("key1")
@@ -22,8 +38,6 @@ class TestStringTable(TestCase):
22 38 
23 def test_pop_with_default(self):39 def test_pop_with_default(self):
24 """Validate StringTable.pop returns default value when key does not exist."""40 """Validate StringTable.pop returns default value when key does not exist."""
25- from torch.autograd.profiler_util import StringTable
26- 
27 st = StringTable()41 st = StringTable()
28 st["key1"] = "value1"42 st["key1"] = "value1"
29 result = st.pop("key2", "default")43 result = st.pop("key2", "default")
@@ -32,8 +46,6 @@ class TestStringTable(TestCase):
32 46 
33 def test_pop_existing_key_with_default(self):47 def test_pop_existing_key_with_default(self):
34 """Validate StringTable.pop returns actual value even when default is provided."""48 """Validate StringTable.pop returns actual value even when default is provided."""
35- from torch.autograd.profiler_util import StringTable
36- 
37 st = StringTable()49 st = StringTable()
38 st["key1"] = "value1"50 st["key1"] = "value1"
39 result = st.pop("key1", "default")51 result = st.pop("key1", "default")
@@ -42,12 +54,49 @@ class TestStringTable(TestCase):
42 54 
43 def test_pop_missing_key_no_default(self):55 def test_pop_missing_key_no_default(self):
44 """Validate StringTable.pop raises KeyError when key is missing and no default."""56 """Validate StringTable.pop raises KeyError when key is missing and no default."""
45- from torch.autograd.profiler_util import StringTable
46- 
47 st = StringTable()57 st = StringTable()
48 with self.assertRaises(KeyError):58 with self.assertRaises(KeyError):
49 st.pop("nonexistent")59 st.pop("nonexistent")
50 60 
61+ def test_default_factory_is_none_by_default(self):
62+ self.assertIsNone(StringTable().default_factory)
63+ self.assertIsNone(StringTable(None).default_factory)
64+ 
65+ def test_default_factory_preserves_callable(self):
66+ def factory():
67+ return "default"
68+ 
69+ table = StringTable(factory)
70+ 
71+ self.assertIs(table.default_factory, factory)
72+ 
73+ def test_default_factory_is_writable(self):
74+ def factory():
75+ return "default"
76+ 
77+ table = StringTable()
78+ table.default_factory = factory
79+ self.assertIs(table.default_factory, factory)
80+ 
81+ table.default_factory = None
82+ self.assertIsNone(table.default_factory)
83+ 
84+ def test_default_factory_rejects_non_callable_at_construction(self):
85+ with self.assertRaises(TypeError):
86+ StringTable("invalid")
atomgit-bot
atomgit-botatomgit-bot7月29日

🟠 High Priority

变更行:第 84–86 行新增的 test_default_factory_rejects_non_callable_at_construction 测试断言 StringTable("invalid") 在构造时抛出 TypeError

受影响的契约/行为:StringTable 继承自 collections.defaultdict,其 __init__ 未覆写,直接使用 CPython 的 defaultdict.__init__。CPython 的 defaultdict.__init__ 不在构造时校验 default_factory 的可调用性——它只是存储该值。TypeError("first argument must be callable or None") 仅由 defaultdict.__missing__ 在访问缺失键时抛出。而 StringTable.__missing__ 已被覆写为直接返回键名本身(字符串驻留),不会调用 default_factory

失败模式:执行此测试时,StringTable("invalid") 不会抛出任何异常,assertRaises(TypeError) 将失败,报 AssertionError: TypeError not raised

建议:删除该测试,或将其改为验证实际行为:StringTable 在构造时接受非可调用对象而不抛异常(因为 missing 不使用 default_factory)。如果目标是在构造时校验,请先在 StringTable.init 中实现校验逻辑。

likedislike
Flipped
Flipped
7月29日 评论:
87+ 
88+ def test_default_factory_is_not_used_by_string_table_missing(self):
89+ calls = []
90+ 
91+ def factory():
92+ calls.append(True)
93+ return "default"
94+ 
95+ table = StringTable(factory)
96+ 
97+ self.assertEqual(table["t"], "t")
98+ self.assertEqual(calls, [])
99+ 
51 100 
52if __name__ == "__main__":101if __name__ == "__main__":
53 run_tests()102 run_tests()