Check the number of arguments passed to `safe_getattr()` (#12961)

This commit is contained in:
Adam Turner
2024-10-04 14:37:05 +01:00
committed by GitHub
parent c3088ef1df
commit 883faaa568

View File

@@ -389,6 +389,10 @@ def isgenericalias(obj: Any) -> TypeIs[types.GenericAlias]:
def safe_getattr(obj: Any, name: str, *defargs: Any) -> Any:
"""A getattr() that turns all exceptions into AttributeErrors."""
if len(defargs) > 1:
msg = f'safe_getattr expected at most 3 arguments, got {len(defargs)}'
raise TypeError(msg)
try:
return getattr(obj, name, *defargs)
except Exception as exc: