From 1c667d79b93ff91c582dc631b6e524106ea395f2 Mon Sep 17 00:00:00 2001 From: Mark Sze Date: Wed, 4 Mar 2026 14:39:06 +1100 Subject: [PATCH] fix: __getattr__ raises AttributeError instead of KeyError DashScopeAPIResponse.__getattr__ raised KeyError when an attribute was missing, breaking getattr(obj, name, default) and hasattr(obj, name) which rely on AttributeError for fallback behavior. Closes #114 --- dashscope/api_entities/dashscope_response.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dashscope/api_entities/dashscope_response.py b/dashscope/api_entities/dashscope_response.py index a49cd23..dae1ef4 100644 --- a/dashscope/api_entities/dashscope_response.py +++ b/dashscope/api_entities/dashscope_response.py @@ -59,7 +59,10 @@ def setattr(self, attr, value): return super().__setitem__(attr, value) def __getattr__(self, attr): - return self[attr] + try: + return self[attr] + except KeyError: + raise AttributeError(attr) from None def __setattr__(self, attr, value): self[attr] = value