Coverage for dataexcept/_causes.py: 100%

11 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-09-27 14:44 +0000

1"""Internal helpers for the canonical wrapped-exception contract.""" 

2 

3from __future__ import annotations 

4 

5__all__ = ["resolve_cause"] 

6 

7 

8def resolve_cause( 

9 *, 

10 cause: Exception | None = None, 

11 original: Exception | None = None, 

12 original_exception: Exception | None = None, 

13) -> Exception | None: 

14 """Return the one supplied cause, rejecting invalid or ambiguous aliases. 

15 

16 ``cause`` is the canonical public keyword. ``original`` and 

17 ``original_exception`` remain supported only for backward compatibility. 

18 """ 

19 values = ( 

20 ("cause", cause), 

21 ("original", original), 

22 ("original_exception", original_exception), 

23 ) 

24 for name, value in values: 

25 if value is not None and not isinstance(value, Exception): 

26 raise TypeError(f"{name} must be Exception or None") 

27 

28 supplied = [value for _, value in values if value is not None] 

29 if len(supplied) > 1: 

30 raise TypeError("provide only one of cause, original, or original_exception") 

31 return supplied[0] if supplied else None