When a Proxy Must Tell the Truth
In this lesson, you see what a proxy may report about frozen settings. The settings library
ships its settings frozen, and a dashboard test uses a stub instead, one that answers every
read with 99, to try an out-of-range level.
In Python, the shipped Settings is a frozen dataclass, so assigning to level raises a
FrozenInstanceError. The Stub class wraps the settings, and its __getattr__() returns 99
for anything the stub itself doesn’t have:
from dataclasses import dataclass
@dataclass(frozen=True)
class Settings:
level: int
label: str
class Stub:
def __init__(self, target):
self._target = target
def __getattr__(self, name):
return 99
settings = Settings(2, "desk")
stub = Stub(settings)
print(settings.level)
print(stub.level)
Freezing blocks writes, not what a wrapper reports, so the log shows 2 from the settings, then
99 from the stub:
2
99