Coverage for yuio / secret.py: 100%
14 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-03 15:42 +0000
« 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
8"""
9Utility types for working with secret data.
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:
15.. vhs:: /_tapes/secrets.tape
16 :alt: Demonstration of the `ask` function with secret data.
17 :width: 480
18 :height: 240
20.. autoclass:: SecretValue
21 :members:
23.. type:: SecretString
24 :canonical: SecretValue[str]
26 Convenience alias for secret strings.
28"""
30from __future__ import annotations
32from dataclasses import dataclass
34from typing import TYPE_CHECKING
36if TYPE_CHECKING:
37 import typing_extensions as _t
38else:
39 from yuio import _typing as _t
41__all__ = [
42 "SecretString",
43 "SecretValue",
44]
46T = _t.TypeVar("T", covariant=True)
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`.
56 """
58 data: T
59 """
60 Secret data.
62 """
64 def __str__(self) -> str:
65 return "***"
67 def __repr__(self) -> str:
68 return f"{self.__class__.__name__}(***)"
71SecretString: _t.TypeAlias = SecretValue[str]
72"""
73Convenience alias for secret string.
75"""