Coverage for yuio / secret.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-05 11:41 +0000

1# Yuio project, MIT license. 

2# 

3# https://github.com/taminomara/yuio/ 

4# 

5# You're free to copy this file to your project and edit it for your needs, 

6# just keep this copyright line please :3 

7 

8""" 

9Utility types for working with secret data. 

10 

11 

12.. autoclass:: SecretValue 

13 :members: 

14 

15.. type:: SecretString 

16 :canonical: SecretValue[str] 

17 

18 Convenience alias for secret strings. 

19 

20""" 

21 

22from __future__ import annotations 

23 

24from dataclasses import dataclass 

25 

26from typing import TYPE_CHECKING 

27 

28if TYPE_CHECKING: 

29 import typing_extensions as _t 

30else: 

31 from yuio import _typing as _t 

32 

33__all__ = [ 

34 "SecretString", 

35 "SecretValue", 

36] 

37 

38T = _t.TypeVar("T", covariant=True) 

39 

40 

41@dataclass(frozen=True, unsafe_hash=True, slots=True) 

42class SecretValue(_t.Generic[T]): 

43 """ 

44 A simple wrapper that prevents inner value from accidentally leaking to logs 

45 or messages: it returns ``"***"`` when converted to string via 

46 :class:`str() <str>` or :func:`repr`. 

47 

48 """ 

49 

50 data: T 

51 """ 

52 Secret data. 

53 

54 """ 

55 

56 def __str__(self) -> str: 

57 return "***" 

58 

59 def __repr__(self) -> str: 

60 return f"{self.__class__.__name__}(***)" 

61 

62 

63SecretString: _t.TypeAlias = SecretValue[str] 

64""" 

65Convenience alias for secret string. 

66 

67"""