Files
RustPython/extra_tests/snippets/builtin_mappingproxy.py
youknowdot d7b4be2978 Align merged runtime contracts with Python 3.15 final
Restrict mappingproxy ordering, use final codec registry normalization, and prefer index conversion before float conversion for sleep. Retain universal Python assertions and import the matching final codec/time canonical test hunks plus the non-ASCII codec-name deprecation warning.

Upstream source: CPython d220409a13b77de7a6c30abe16ea90db01528edc. The existing codec normalization implementation and bytecode contract remain unchanged.

Assisted-by: OpenAI Codex:model identifier unavailable
2026-10-11 07:36:19 +09:00

87 lines
2.1 KiB
Python

from testutils import assert_raises
class A(dict):
def a():
pass
def b():
pass
assert A.__dict__["a"] == A.a
with assert_raises(KeyError) as cm:
A.__dict__["not here"]
assert cm.exception.args[0] == "not here"
assert "b" in A.__dict__
assert "c" not in A.__dict__
assert "__dict__" in A.__dict__
assert A.__dict__.get("not here", "default") == "default"
assert A.__dict__.get("a", "default") is A.a
assert A.__dict__.get("not here") is None
# A mappingproxy preserves arbitrary equality-comparison results unchanged.
def check_mappingproxy_comparison_results():
import operator
from types import MappingProxyType
from testutils import assert_raises
class Result:
def __bool__(self):
raise AssertionError("comparison result must not be coerced to bool")
result = Result()
calls = []
def compare(name):
def method(self, other):
calls.append(name)
return result
return method
class Compared:
__eq__ = compare("eq")
__ne__ = compare("ne")
__lt__ = compare("lt")
__le__ = compare("le")
__gt__ = compare("gt")
__ge__ = compare("ge")
class CustomMapping(Compared, dict):
pass
for operation, reflected in (
(operator.eq, "eq"),
(operator.ne, "ne"),
):
calls.clear()
assert operation(MappingProxyType(CustomMapping()), {}) is result
assert calls == [operation.__name__], calls
calls.clear()
assert operation(MappingProxyType({}), Compared()) is result
assert calls == [reflected], calls
for operation, reflected in (
(operator.lt, "gt"),
(operator.le, "ge"),
(operator.gt, "lt"),
(operator.ge, "le"),
):
calls.clear()
with assert_raises(TypeError):
operation(MappingProxyType(CustomMapping()), {})
assert calls == [], calls
calls.clear()
assert operation(MappingProxyType({}), Compared()) is result
assert calls == [reflected], calls
check_mappingproxy_comparison_results()