|
6 | 6 | import sys |
7 | 7 | import uuid |
8 | 8 | from base64 import b64encode |
| 9 | +from collections import UserDict |
9 | 10 | from collections.abc import MutableMapping |
10 | 11 | from dataclasses import dataclass, field |
11 | 12 | from typing import ( |
@@ -1219,6 +1220,84 @@ def test_dict_wrong_type(self): |
1219 | 1220 | with pytest.raises(ValidationError, match=r"Expected `object`, got `int`"): |
1220 | 1221 | assert convert(1, dict) |
1221 | 1222 |
|
| 1223 | + @pytest.mark.parametrize("strict", [False, True]) |
| 1224 | + def test_dict_getitem_without_keys(self, strict): |
| 1225 | + class Table: |
| 1226 | + def __getitem__(self, key): |
| 1227 | + pytest.fail("Should not access non-mapping items") |
| 1228 | + |
| 1229 | + class Ex(Struct): |
| 1230 | + table: dict[str, Any] |
| 1231 | + |
| 1232 | + with pytest.raises(ValidationError) as rec: |
| 1233 | + convert({"table": Table()}, Ex, strict=strict) |
| 1234 | + assert str(rec.value) == "Expected `object`, got `Table` - at `$.table`" |
| 1235 | + |
| 1236 | + @pytest.mark.parametrize( |
| 1237 | + "keys_type", |
| 1238 | + [ |
| 1239 | + list, |
| 1240 | + tuple, |
| 1241 | + iter, |
| 1242 | + lambda keys: dict.fromkeys(keys).keys(), |
| 1243 | + lambda keys: UserDict.fromkeys(keys).keys(), |
| 1244 | + ], |
| 1245 | + ) |
| 1246 | + def test_dict_duck_mapping(self, keys_type): |
| 1247 | + class DuckMapping: |
| 1248 | + def keys(self): |
| 1249 | + return keys_type(["x", "y", "x"]) |
| 1250 | + |
| 1251 | + def __getitem__(self, key): |
| 1252 | + return {"x": 1, "y": 2}[key] |
| 1253 | + |
| 1254 | + res = convert(DuckMapping(), dict[str, float]) |
| 1255 | + assert res == {"x": 1.0, "y": 2.0} |
| 1256 | + |
| 1257 | + def test_dict_mapping_keys_lookup_error(self): |
| 1258 | + class BadMapping: |
| 1259 | + @property |
| 1260 | + def keys(self): |
| 1261 | + raise RuntimeError("boom") |
| 1262 | + |
| 1263 | + def __getitem__(self, key): |
| 1264 | + pytest.fail("Should not access items when keys lookup fails") |
| 1265 | + |
| 1266 | + with pytest.raises(RuntimeError, match="boom"): |
| 1267 | + convert(BadMapping(), dict) |
| 1268 | + |
| 1269 | + def test_dict_mapping_keys_call_error_propagates(self): |
| 1270 | + class BadMapping: |
| 1271 | + def keys(self): |
| 1272 | + raise RuntimeError("boom") |
| 1273 | + |
| 1274 | + def __getitem__(self, key): |
| 1275 | + pytest.fail("Should not access items when keys call fails") |
| 1276 | + |
| 1277 | + with pytest.raises(RuntimeError, match="boom"): |
| 1278 | + convert(BadMapping(), dict) |
| 1279 | + |
| 1280 | + def test_dict_mapping_getitem_error_propagates(self): |
| 1281 | + class BadMapping: |
| 1282 | + def keys(self): |
| 1283 | + return ["x"] |
| 1284 | + |
| 1285 | + def __getitem__(self, key): |
| 1286 | + raise RuntimeError("boom") |
| 1287 | + |
| 1288 | + with pytest.raises(RuntimeError, match="boom"): |
| 1289 | + convert(BadMapping(), dict) |
| 1290 | + |
| 1291 | + def test_dict_mapping_keys_not_callable(self): |
| 1292 | + class BadMapping: |
| 1293 | + keys = None |
| 1294 | + |
| 1295 | + def __getitem__(self, key): |
| 1296 | + return 1 |
| 1297 | + |
| 1298 | + with pytest.raises(TypeError, match="not callable"): |
| 1299 | + convert(BadMapping(), dict) |
| 1300 | + |
1222 | 1301 | def test_str_formatted_keys(self): |
1223 | 1302 | msg = {uuid.uuid4(): 1, uuid.uuid4(): 2} |
1224 | 1303 | res = convert(to_builtins(msg), dict[uuid.UUID, int]) |
@@ -1325,6 +1404,29 @@ class Ex(Struct): |
1325 | 1404 |
|
1326 | 1405 | @py315_or_later_only |
1327 | 1406 | class TestFrozenDict: |
| 1407 | + def test_getitem_without_keys(self): |
| 1408 | + class DuckMapping: |
| 1409 | + def __getitem__(self, key): |
| 1410 | + return 1 |
| 1411 | + |
| 1412 | + with pytest.raises( |
| 1413 | + ValidationError, match="Expected `object`, got `DuckMapping`" |
| 1414 | + ): |
| 1415 | + convert(DuckMapping(), frozendict[str, float]) |
| 1416 | + |
| 1417 | + def test_duck_mapping(self): |
| 1418 | + class DuckMapping: |
| 1419 | + def keys(self): |
| 1420 | + return ["x"] |
| 1421 | + |
| 1422 | + def __getitem__(self, key): |
| 1423 | + return 1 |
| 1424 | + |
| 1425 | + result = convert(DuckMapping(), frozendict[str, float]) |
| 1426 | + assert type(result) is frozendict |
| 1427 | + assert result == frozendict({"x": 1.0}) |
| 1428 | + assert type(result["x"]) is float |
| 1429 | + |
1328 | 1430 | def check_frozen(self, val, expected): |
1329 | 1431 | assert isinstance(expected, frozendict) |
1330 | 1432 | assert type(val) is type(expected) |
|
0 commit comments