Skip to content

Constrained scalar types

The constrained scalars under didactic.types.

didactic.types._types_lib.SecretStr

SecretStr(value: str)

Wraps a string so its repr / serialisation hides the value.

Parameters:

Name Type Description Default
value str

The wrapped string. Stored verbatim; never printed.

required

Examples:

>>> s = SecretStr("hunter2")
>>> repr(s)
'SecretStr(***)'
>>> s.get_secret_value()
'hunter2'

get_secret_value

get_secret_value() -> str

Return the wrapped string.

The remaining members of didactic.types are annotation aliases:

name shape
EmailStr Annotated[str, _StringPattern(<email regex>, "email")]
HttpUrl Annotated[str, _StringPattern(<http url regex>, "http_url")]
Json[T] Annotated[str, _JsonOf(inner=T)]

Use them as drop-in field annotations:

from didactic.types import EmailStr, HttpUrl, SecretStr


class User(dx.Model):
    email: EmailStr
    homepage: HttpUrl | None = None
    api_key: SecretStr