Coverage for yuio / secret.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-03 15:42 +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 

11Using these types ensures that their values don't end up in error messages 

12or logs. This also causes :func:`yuio.io.ask` to use special widgets that don't 

13print entered text: 

14 

15.. vhs:: /_tapes/secrets.tape 

16 :alt: Demonstration of the `ask` function with secret data. 

17 :width: 480 

18 :height: 240 

19 

20.. autoclass:: SecretValue 

21 :members: 

22 

23.. type:: SecretString 

24 :canonical: SecretValue[str] 

25 

26 Convenience alias for secret strings. 

27 

28""" 

29 

30from __future__ import annotations 

31 

32from dataclasses import dataclass 

33 

34from typing import TYPE_CHECKING 

35 

36if TYPE_CHECKING: 

37 import typing_extensions as _t 

38else: 

39 from yuio import _typing as _t 

40 

41__all__ = [ 

42 "SecretString", 

43 "SecretValue", 

44] 

45 

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

47 

48 

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

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

51 """ 

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

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

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

55 

56 """ 

57 

58 data: T 

59 """ 

60 Secret data. 

61 

62 """ 

63 

64 def __str__(self) -> str: 

65 return "***" 

66 

67 def __repr__(self) -> str: 

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

69 

70 

71SecretString: _t.TypeAlias = SecretValue[str] 

72""" 

73Convenience alias for secret string. 

74 

75"""