Fields¶
Field definitions for dotenvmodel configuration classes.
fields ¶
Field descriptor and Required sentinel for dotenvmodel.
Required
module-attribute
¶
Sentinel value marking a field as required.
Use this as a class attribute value instead of Field() when you want
to be explicit that a field is required. Functionally identical to
Field() with no arguments.
When to use
- When you prefer the explicit
Requiredsyntax overField() - For readability when a field has no constraints or defaults
Example
See Also
Field: For fields with defaults or constraints.
ValidatorContext
dataclass
¶
Context passed to a field's custom validator hook.
When to use
- Received as the second argument of any
Field(validator=...)callable; you never construct it yourself
Attributes:
| Name | Type | Description |
|---|---|---|
field_name |
str
|
The Python field name (e.g. |
env_var_name |
str
|
The resolved environment variable name, including any
|
Example
See Also
Field: For attaching a validator to a field.
FieldInfo ¶
FieldInfo(
default: Any = _MISSING,
*,
default_factory: Callable[[], Any] | None = None,
alias: str | None = None,
description: str | None = None,
ge: int | float | Decimal | None = None,
le: int | float | Decimal | None = None,
gt: int | float | Decimal | None = None,
lt: int | float | Decimal | None = None,
min_length: int | None = None,
max_length: int | None = None,
regex: str | None = None,
starts_with: str | None = None,
ends_with: str | None = None,
strip: bool | str | Pattern[str] | None = None,
choices: list[Any] | None = None,
validator: Callable[[Any, ValidatorContext], Any]
| None = None,
min_items: int | None = None,
max_items: int | None = None,
uuid_version: int | None = None,
separator: str = ",",
url_unquote: bool = True,
resolve_path: bool = True,
require_exists: bool = False,
)
Information about a configuration field.
This class holds all metadata about a field including its default value,
validation constraints, and documentation. You typically don't create
FieldInfo directly — use the Field() function instead.
When to use directly
- Rarely. Use
Field()in almost all cases. - When introspecting field metadata via
get_fields()
Attributes:
| Name | Type | Description |
|---|---|---|
default |
Any
|
Default value if env var not set (or |
default_factory |
Callable[[], Any] | None
|
Callable that returns a default value (for mutable defaults) |
alias |
str | None
|
Alternative environment variable name (overrides prefix) |
description |
str | None
|
Human-readable description for documentation |
ge |
int | float | Decimal | None
|
Greater-than-or-equal constraint (>=) |
le |
int | float | Decimal | None
|
Less-than-or-equal constraint (<=) |
gt |
int | float | Decimal | None
|
Greater-than constraint (>) |
lt |
int | float | Decimal | None
|
Less-than constraint (<) |
min_length |
int | None
|
Minimum string length |
max_length |
int | None
|
Maximum string length |
regex |
str | None
|
Regular expression pattern to match |
starts_with |
str | None
|
Required string prefix |
ends_with |
str | None
|
Required string suffix |
strip |
bool | str | Pattern[str] | None
|
Strip mode for string values (bool, char-set str, or re.Pattern) |
choices |
list[Any] | None
|
List of allowed values |
validator |
Callable[[Any, ValidatorContext], Any] | None
|
Custom validation/transformation hook |
min_items |
int | None
|
Minimum items in a collection |
max_items |
int | None
|
Maximum items in a collection |
uuid_version |
int | None
|
Required UUID version (1, 3, 4, or 5) |
separator |
str
|
Delimiter for parsing list/set/tuple/dict from string (default ",") |
url_unquote |
bool
|
Whether to URL-unquote SecretStr values (default True) |
required |
bool
|
Whether the field is required (computed from default) |
See Also
Field: The function you should use to create fields.
Source code in dotenvmodel/fields.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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 282 283 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 | |
validator
instance-attribute
¶
get_default ¶
Get the default value for this field.
__repr__ ¶
Source code in dotenvmodel/fields.py
Field ¶
Field(
default: Any = _MISSING,
*,
default_factory: Callable[[], Any] | None = None,
alias: str | None = None,
description: str | None = None,
ge: int | float | Decimal | None = None,
le: int | float | Decimal | None = None,
gt: int | float | Decimal | None = None,
lt: int | float | Decimal | None = None,
min_length: int | None = None,
max_length: int | None = None,
regex: str | None = None,
starts_with: str | None = None,
ends_with: str | None = None,
strip: bool | str | Pattern[str] | None = None,
choices: list[Any] | None = None,
validator: Callable[[Any, ValidatorContext], Any]
| None = None,
min_items: int | None = None,
max_items: int | None = None,
uuid_version: int | None = None,
separator: str = ",",
url_unquote: bool = True,
resolve_path: bool = True,
require_exists: bool = False,
) -> Any
Define a configuration field with validation and default values.
When to use
- Always use
Field()(orRequired) to define config fields - Use
Field()with no arguments for a required string field - Use
Field(...)(ellipsis) for any required field — Pydantic-style - Use
Field(default=value)for optional fields with defaults
When to use default vs default_factory:
- Use default for immutable values (str, int, float, bool, None)
- Use default_factory for mutable values (list, dict, set) to avoid
shared mutable state between instances
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Any
|
Default value if environment variable not set. Use |
_MISSING
|
default_factory
|
Callable[[], Any] | None
|
Callable that returns a default value. Use this instead of
|
None
|
alias
|
str | None
|
Alternative environment variable name to read from. When set, the
field name is not used for env var lookup, and |
None
|
description
|
str | None
|
Human-readable description shown in |
None
|
ge
|
int | float | Decimal | None
|
Greater than or equal to (>=). For int, float, and Decimal fields.
Example: |
None
|
le
|
int | float | Decimal | None
|
Less than or equal to (<=). For int, float, and Decimal fields.
Example: |
None
|
gt
|
int | float | Decimal | None
|
Greater than (>). For int, float, and Decimal fields.
Example: |
None
|
lt
|
int | float | Decimal | None
|
Less than (<). For int, float, and Decimal fields.
Example: |
None
|
min_length
|
int | None
|
Minimum string length (inclusive). For str and SecretStr fields.
Example: |
None
|
max_length
|
int | None
|
Maximum string length (inclusive). For str and SecretStr fields.
Example: |
None
|
regex
|
str | None
|
Regular expression pattern the string must match (using |
None
|
starts_with
|
str | None
|
Required string prefix. For str and str subclasses (including
SecretStr and DSN types like HttpUrl, PostgresDsn, RedisDsn).
Example: |
None
|
ends_with
|
str | None
|
Required string suffix. For str and str subclasses (including
SecretStr and DSN types like HttpUrl, PostgresDsn, RedisDsn).
Example: |
None
|
strip
|
bool | str | Pattern[str] | None
|
Strip mode applied to the raw string before coercion. Applies to str, SecretStr, their Optional forms, and str subclasses (e.g. HttpUrl):
Example: |
None
|
choices
|
list[Any] | None
|
List of allowed values. The env var value must be in this list
(after type coercion). Example: |
None
|
validator
|
Callable[[Any, ValidatorContext], Any] | None
|
Custom hook called with the coerced, built-in-constraint-validated
value and a |
None
|
min_items
|
int | None
|
Minimum number of items in a collection (list, set, tuple, dict).
Example: |
None
|
max_items
|
int | None
|
Maximum number of items in a collection (list, set, tuple, dict).
Example: |
None
|
uuid_version
|
int | None
|
Required UUID version (1, 3, 4, or 5). For UUID fields.
Example: |
None
|
separator
|
str
|
Delimiter for parsing list/set/tuple/dict from a string.
Default is comma (","). Example: |
','
|
url_unquote
|
bool
|
Whether to URL-unquote SecretStr values (default True). Useful when secrets come from URL-encoded env vars. |
True
|
resolve_path
|
bool
|
Whether to resolve Path values (expanduser + resolve). Default True. Set to False to keep paths raw. |
True
|
require_exists
|
bool
|
Whether a Path field must point to an existing path. Default False. |
False
|
Returns:
| Type | Description |
|---|---|
Any
|
FieldInfo instance containing field metadata. Used by the |
Any
|
metaclass to discover and process fields. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
TypeError
|
If numeric constraints ( |
Example
class Config(DotEnvConfig):
# Required field (no default)
database_url: str = Field()
# Required field (Pydantic-style with ellipsis)
api_key: str = Field(...)
# Optional with default
debug: bool = Field(default=False)
# With validation
port: int = Field(default=8000, ge=1, le=65535)
# With alias (overrides env_prefix)
postgres_dsn: str = Field(alias="DATABASE_URL")
# Mutable default with default_factory
hosts: list[str] = Field(default_factory=list)
# List with custom separator
tags: list[str] = Field(default_factory=list, separator=";")
# Collection size constraints
allowed_ips: list[str] = Field(min_items=1, max_items=10)
# UUID version constraint
tenant_id: UUID = Field(uuid_version=4)
# Choice validation
env: str = Field(default="dev", choices=["dev", "test", "prod"])
# SecretStr with length constraint
api_key: SecretStr = Field(min_length=32)
# Strip whitespace from the raw value before coercion
name: str = Field(strip=True)
# Char-set and regex strip modes
tag: str = Field(strip=",'"") # str.strip(chars) semantics
key: str = Field(strip=re.compile(r"^['"]+|['"]+$")) # remove every match
# Prefix/suffix constraints
client_key: str = Field(starts_with="sk-")
signed_token: str = Field(ends_with=".sig")
# Custom validator (may also transform the value)
region: str = Field(default="us-east-1", validator=lambda v, ctx: v.lower())
Source code in dotenvmodel/fields.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | |