Describe Formatters¶
Type and value formatting utilities for describe output.
formatters ¶
Type and value formatting utilities for describe output.
TYPE_PARSING_HINTS
module-attribute
¶
TYPE_PARSING_HINTS = {
"list": "comma-separated values",
"set": "comma-separated unique values",
"tuple": "comma-separated values",
"dict": "comma-separated key:value pairs",
"timedelta": "duration format (e.g., 5s, 1m, 1h, 1d, 1w) or seconds as int",
"UUID": "UUID string",
"Decimal": "decimal number string",
"Path": "file or directory path",
"HttpUrl": "HTTP(S) URL (e.g., https://example.com)",
"PostgresDsn": "PostgreSQL DSN (e.g., postgresql://user:pass@localhost:5432/db)",
"RedisDsn": "Redis DSN (e.g., redis://localhost:6379/0)",
"Json": "valid JSON string",
"SecretStr": "sensitive string (won't be logged)",
"datetime": "ISO 8601 datetime string",
}
FieldDescription
dataclass
¶
FieldDescription(
env_var: str,
field_name: str,
type_name: str,
required: bool,
default: str,
description: str,
constraints: str,
separator: str = ",",
)
Structured representation of a field for describe output.
Attributes:
| Name | Type | Description |
|---|---|---|
env_var |
str
|
The environment variable name (with prefix applied) |
field_name |
str
|
The Python field name |
type_name |
str
|
Human-readable type string (e.g., "list[str]") |
required |
bool
|
Whether the field is required |
default |
str
|
Formatted default value string (e.g., "8000", "None", "-") |
description |
str
|
Field description or "-" if none |
constraints |
str
|
Formatted constraints string (e.g., "ge=1, le=65535") |
separator |
str
|
Delimiter the field uses for collection values (default ",") |
format_type_name ¶
Format a type annotation as a readable string.
Examples:
int -> "int" list[str] -> "list[str]" str | None -> "str | None" LogLevel -> "LogLevel (debug, info, warning, error)"
Source code in dotenvmodel/describe/formatters.py
get_type_parsing_hint ¶
Get parsing hint for a type to help developers format values.
Source code in dotenvmodel/describe/formatters.py
generate_constraint_examples ¶
Generate valid and invalid examples for field constraints.
Source code in dotenvmodel/describe/formatters.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
format_constraints ¶
format_constraints(
field_info: FieldInfo,
truncate: bool = True,
field_type: type | None = None,
class_strip_strings: bool = False,
) -> str
Format field constraints as a readable string.
Source code in dotenvmodel/describe/formatters.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
format_default ¶
Format a field's default value for display.
Renders values in the format dotenvmodel itself parses so generated
.env.example files round-trip: list/set/tuple defaults
are joined with the field's separator, dict defaults as
key=value pairs, and Json[...] fields as JSON. A
default_factory is invoked once to render its result. Rendering
never crashes: a factory that raises, or a value that fails to render
(e.g. a non-serializable Json[...] default), logs a warning and
returns the SET_PER_ENVIRONMENT placeholder instead. Warnings
carry the exception type only — messages can embed secret values.
When truncate is true, joined/JSON renders are capped at
TRUNCATE_THRESHOLD_MEDIUM; the dotenv format renders untruncated
so example values stay complete.
Source code in dotenvmodel/describe/formatters.py
describe_class ¶
describe_class(
config_cls: type[DotEnvConfig], truncate: bool = True
) -> tuple[str, str, list[FieldDescription]]
Extract field descriptions from a config class.
Returns:
| Type | Description |
|---|---|
tuple[str, str, list[FieldDescription]]
|
Tuple of (class_name, env_prefix, list of FieldDescription) |