MCPcopy Index your code
hub / github.com/msgspec/msgspec / test_int_keys

Method test_int_keys

tests/unit/test_convert.py:1186–1211  ·  view source on GitHub ↗
(self, dictcls, key_type)

Source from the content-addressed store, hash-verified

1184
1185 @pytest.mark.parametrize("key_type", ["int", "enum", "literal"])
1186 def test_int_keys(self, dictcls, key_type):
1187 msg = dictcls({1: "A", 2: "B"})
1188 if key_type == "enum":
1189 Key = enum.IntEnum("Key", ["one", "two"])
1190 sol = {Key.one: "A", Key.two: "B"}
1191 elif key_type == "literal":
1192 Key = Literal[1, 2]
1193 sol = msg
1194 else:
1195 Key = int
1196 sol = msg
1197
1198 res = convert(msg, Dict[Key, str])
1199 assert res == sol
1200
1201 res = convert(msg, Dict[Key, str], str_keys=True)
1202 assert res == sol
1203
1204 str_msg = dictcls(to_builtins(dict(msg), str_keys=True))
1205 res = convert(str_msg, Dict[Key, str], str_keys=True)
1206 assert res == sol
1207
1208 with pytest.raises(
1209 ValidationError, match=r"Expected `int`, got `str` - at `key` in `\$`"
1210 ):
1211 convert(str_msg, Dict[Key, str])
1212
1213 def test_non_str_keys(self, dictcls):
1214 convert(dictcls({1.5: 1}), Dict[float, int]) == {1.5: 1}

Callers

nothing calls this directly

Calls 1

dictclsFunction · 0.85

Tested by

no test coverage detected