-
Notifications
You must be signed in to change notification settings - Fork 2
Parsers
Jag_k edited this page Feb 18, 2025
·
1 revision
This document describes the parsing system in pydantic-settings-export.
The parsing system is the foundation of pydantic-settings-export, responsible for:
-
Model Analysis
- Extracts metadata from Pydantic models
- Preserves type information
- Maintains documentation
- Handle inheritance
-
Type Management
- Converts Python types to human-readable formats
- Handles complex types (Union, Optional, etc)
- Supports custom types
- Preserves type constraints
-
Environment Integration
- Processes environment variables
- Handles prefixes and aliases
- Supports nested configurations
- Maintains variable hierarchy
-
Documentation Processing
- Extracts docstrings and comments
- Preserves formatting
- Handles multi-line descriptions
- Supports Markdown in documentation
The central model that represents parsed settings:
class SettingsInfoModel:
name: str # Settings class name
docs: str # Class documentation
env_prefix: str # Environment variable prefix
fields: list[FieldInfoModel] # Settings fields
child_settings: list[SettingsInfoModel] # Nested settings
Represents individual setting fields:
class FieldInfoModel:
name: str # Field name
types: list[str] # Field types
default: str # Default value
description: str # Field description
examples: list[str] # Example values
aliases: list[str] # Environment variable aliases
deprecated: bool # Deprecation status
The system automatically converts Python types to human-readable formats:
Python Type | Parsed Format |
---|---|
str | "string" |
int | "integer" |
float | "number" |
bool | "boolean" |
list[str] | "array of strings" |
dict[str, int] | "map of string to integer" |
Union[str, int] | "string | integer" |
Literal["a", "b"] | "'a' | 'b'" |
See how these types are used in different output formats in Generators.
- Respects Pydantic's env_prefix setting
- Handles nested delimiters
- Supports multiple aliases
- Case conversion options
- Recursive parsing of nested BaseSettings
- Maintains hierarchy information
- Proper prefix handling
- Documentation inheritance
- Extracts docstrings
- Removes form feed characters
- Handles multi-line descriptions
- Preserves formatting
- Handles complex default types
- Supports default factories
- Path normalization
- Special handling for collections
from pydantic_settings_export.models import SettingsInfoModel
from your_app.settings import AppSettings
# Parse settings model
settings_info = SettingsInfoModel.from_settings_model(
AppSettings,
global_settings=None, # Optional PSESettings instance
prefix="APP_", # Optional prefix override
nested_delimiter="_" # Optional delimiter
)
# Access parsed information
print(f"Settings name: {settings_info.name}")
print(f"Environment prefix: {settings_info.env_prefix}")
for field in settings_info.fields:
print(f"\nField: {field.name}")
print(f"Type: {' | '.join(field.types)}")
print(f"Default: {field.default}")
print(f"Description: {field.description}")
-
Type Hints
- Always use explicit type annotations
- Prefer specific types to Any
- Use appropriate container types
-
Documentation
- Provide clear field descriptions
- Include meaningful examples
- Document deprecated fields
-
Environment Variables
- Use consistent naming conventions
- Set appropriate prefixes
- Consider alias needs
-
Nested Settings
- Structure logically
- Maintain clear hierarchy
- Use meaningful prefixes