MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_str_nonstr

Method test_str_nonstr

Lib/test/test_dict.py:1477–1575  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1475 obj.attr = EvilAttr(obj.__dict__)
1476
1477 def test_str_nonstr(self):
1478 # cpython uses a different lookup function if the dict only contains
1479 # `str` keys. Make sure the unoptimized path is used when a non-`str`
1480 # key appears.
1481
1482 class StrSub(str):
1483 pass
1484
1485 eq_count = 0
1486 # This class compares equal to the string 'key3'
1487 class Key3:
1488 def __hash__(self):
1489 return hash('key3')
1490
1491 def __eq__(self, other):
1492 nonlocal eq_count
1493 if isinstance(other, Key3) or isinstance(other, str) and other == 'key3':
1494 eq_count += 1
1495 return True
1496 return False
1497
1498 key3_1 = StrSub('key3')
1499 key3_2 = Key3()
1500 key3_3 = Key3()
1501
1502 dicts = []
1503
1504 # Create dicts of the form `{'key1': 42, 'key2': 43, key3: 44}` in a
1505 # bunch of different ways. In all cases, `key3` is not of type `str`.
1506 # `key3_1` is a `str` subclass and `key3_2` is a completely unrelated
1507 # type.
1508 for key3 in (key3_1, key3_2):
1509 # A literal
1510 dicts.append({'key1': 42, 'key2': 43, key3: 44})
1511
1512 # key3 inserted via `dict.__setitem__`
1513 d = {'key1': 42, 'key2': 43}
1514 d[key3] = 44
1515 dicts.append(d)
1516
1517 # key3 inserted via `dict.setdefault`
1518 d = {'key1': 42, 'key2': 43}
1519 self.assertEqual(d.setdefault(key3, 44), 44)
1520 dicts.append(d)
1521
1522 # key3 inserted via `dict.update`
1523 d = {'key1': 42, 'key2': 43}
1524 d.update({key3: 44})
1525 dicts.append(d)
1526
1527 # key3 inserted via `dict.__ior__`
1528 d = {'key1': 42, 'key2': 43}
1529 d |= {key3: 44}
1530 dicts.append(d)
1531
1532 # `dict(iterable)`
1533 def make_pairs():
1534 yield ('key1', 42)

Callers

nothing calls this directly

Calls 12

Key3Class · 0.85
enumerateFunction · 0.85
subTestMethod · 0.80
assertFalseMethod · 0.80
assertGreaterEqualMethod · 0.80
StrSubClass · 0.70
appendMethod · 0.45
assertEqualMethod · 0.45
setdefaultMethod · 0.45
updateMethod · 0.45
copyMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected