Coverage for yuio / secret.py: 100%
14 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-05 11:41 +0000
« 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
8"""
9Utility types for working with secret data.
12.. autoclass:: SecretValue
13 :members:
15.. type:: SecretString
16 :canonical: SecretValue[str]
18 Convenience alias for secret strings.
20"""
22from __future__ import annotations
24from dataclasses import dataclass
26from typing import TYPE_CHECKING
28if TYPE_CHECKING:
29 import typing_extensions as _t
30else:
31 from yuio import _typing as _t
33__all__ = [
34 "SecretString",
35 "SecretValue",
36]
38T = _t.TypeVar("T", covariant=True)
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`.
48 """
50 data: T
51 """
52 Secret data.
54 """
56 def __str__(self) -> str:
57 return "***"
59 def __repr__(self) -> str:
60 return f"{self.__class__.__name__}(***)"
63SecretString: _t.TypeAlias = SecretValue[str]
64"""
65Convenience alias for secret string.
67"""