CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-openapi-python-client

Generate modern Python clients from OpenAPI 3.0 and 3.1 documents

Pending
Overview
Eval results
Files

configuration.mddocs/

Configuration System

Comprehensive configuration system supporting file-based configuration, class overrides, project customization, and post-generation hooks. The configuration system allows fine-grained control over code generation behavior and output structure.

Capabilities

Configuration File Loading

Load configuration from YAML or JSON files for repeatable builds and complex setups.

class ConfigFile(BaseModel):
    class_overrides: Optional[dict[str, ClassOverride]] = None
    content_type_overrides: Optional[dict[str, str]] = None
    project_name_override: Optional[str] = None
    package_name_override: Optional[str] = None
    package_version_override: Optional[str] = None
    use_path_prefixes_for_title_model_names: bool = True
    post_hooks: Optional[list[str]] = None
    docstrings_on_attributes: bool = False
    field_prefix: str = "field_"
    generate_all_tags: bool = False
    http_timeout: int = 5
    literal_enums: bool = False

    @staticmethod
    def load_from_path(path: Path) -> "ConfigFile":
        """
        Load configuration from JSON or YAML file.
        
        Parameters:
        - path: Path to configuration file
        
        Returns:
        ConfigFile object with loaded settings
        """

Class Override Configuration

Control the names and organization of generated model classes.

class ClassOverride(BaseModel):
    class_name: Optional[str] = None
    module_name: Optional[str] = None
    
    """
    Configuration for overriding generated class names.
    
    Attributes:
    - class_name: Override the generated class name
    - module_name: Override the generated module name
    """

Metadata Type Configuration

Control the type of project metadata generated (pyproject.toml, setup.py, etc.).

class MetaType(str, Enum):
    NONE = "none"      # No project metadata files
    POETRY = "poetry"  # Poetry-compatible pyproject.toml
    SETUP = "setup"    # setup.py file
    PDM = "pdm"        # PDM-compatible pyproject.toml
    UV = "uv"          # uv-compatible pyproject.toml

Runtime Configuration

The main configuration object used during generation, combining file-based and runtime settings.

class Config:
    meta_type: MetaType
    class_overrides: dict[str, ClassOverride]
    project_name_override: Optional[str]
    package_name_override: Optional[str]
    package_version_override: Optional[str]
    use_path_prefixes_for_title_model_names: bool
    post_hooks: list[str]
    docstrings_on_attributes: bool
    field_prefix: str
    generate_all_tags: bool
    http_timeout: int
    literal_enums: bool
    document_source: Union[Path, str]
    file_encoding: str
    content_type_overrides: dict[str, str]
    overwrite: bool
    output_path: Optional[Path]

    @staticmethod
    def from_sources(
        config_file: ConfigFile,
        meta_type: MetaType,
        document_source: Union[Path, str],
        file_encoding: str,
        overwrite: bool,
        output_path: Optional[Path],
    ) -> "Config":
        """
        Create a Config from various sources.
        
        Parameters:
        - config_file: File-based configuration settings
        - meta_type: Project metadata type
        - document_source: OpenAPI document URL or file path
        - file_encoding: Text encoding for generated files
        - overwrite: Whether to overwrite existing output
        - output_path: Custom output directory
        
        Returns:
        Combined configuration object
        """

Configuration Options

Project Customization

Control the names and structure of generated projects:

# Project and package names
project_name_override: "my-awesome-api-client"
package_name_override: "my_awesome_api_client"
package_version_override: "1.0.0"

# Use path prefixes in model names for disambiguation
use_path_prefixes_for_title_model_names: true

Class and Module Overrides

Rename generated classes and control their module organization:

class_overrides:
  VeryLongGeneratedModelName:
    class_name: ShortName
    module_name: short_name
  AnotherLongName:
    class_name: Better
    # module_name defaults to snake_case of class_name

Code Generation Options

Control aspects of the generated code:

# Add docstrings to model attributes
docstrings_on_attributes: true

# Prefix for fields that conflict with Python keywords
field_prefix: "field_"

# Generate all tags even if they have no operations
generate_all_tags: false

# Use literal types instead of enums for string enums
literal_enums: true

Content Type Handling

Override content type detection for specific MIME types:

content_type_overrides:
  "application/vnd.api+json": "application/json"
  "text/csv": "text/plain"

Post-Generation Hooks

Run commands after code generation for formatting and validation:

post_hooks:
  - "ruff check --fix ."
  - "ruff format ."
  - "mypy ."
  - "pytest tests/"

Network Configuration

Control HTTP behavior when fetching OpenAPI documents:

# Timeout in seconds for HTTP requests
http_timeout: 30

Usage Examples

Basic YAML Configuration

# config.yaml
project_name_override: "weather-api-client"
package_name_override: "weather_api_client"

class_overrides:
  WeatherDataResponseModel:
    class_name: WeatherData
    module_name: weather_data

post_hooks:
  - "ruff check --fix ."
  - "ruff format ."

field_prefix: "attr_"
docstrings_on_attributes: true
http_timeout: 10

JSON Configuration

{
  "project_name_override": "weather-api-client",
  "package_name_override": "weather_api_client",
  "class_overrides": {
    "WeatherDataResponseModel": {
      "class_name": "WeatherData",
      "module_name": "weather_data"
    }
  },
  "post_hooks": [
    "ruff check --fix .",
    "ruff format ."
  ],
  "field_prefix": "attr_",
  "docstrings_on_attributes": true,
  "http_timeout": 10
}

Loading Configuration Programmatically

from pathlib import Path
from openapi_python_client.config import ConfigFile, MetaType, ClassOverride

# Load from file
config_file = ConfigFile.load_from_path(Path("config.yaml"))

# Or create programmatically
config_file = ConfigFile(
    project_name_override="my-client",
    class_overrides={
        "LongModelName": ClassOverride(
            class_name="ShortName",
            module_name="short_name"
        )
    },
    post_hooks=["ruff check --fix .", "ruff format ."],
    docstrings_on_attributes=True,
    field_prefix="field_",
    http_timeout=30
)

Different Metadata Types

from openapi_python_client.config import MetaType

# Poetry (default) - generates pyproject.toml with Poetry metadata
meta_type = MetaType.POETRY

# PDM - generates pyproject.toml with PDM metadata
meta_type = MetaType.PDM

# Setup.py - generates traditional setup.py file
meta_type = MetaType.SETUP

# UV - generates pyproject.toml with uv metadata
meta_type = MetaType.UV

# None - generates only Python code, no project metadata
meta_type = MetaType.NONE

Advanced Hook Configuration

# Advanced post-hooks for different environments
post_hooks:
  # Code formatting
  - "ruff check --fix ."
  - "ruff format ."
  
  # Type checking
  - "mypy ."
  
  # Testing
  - "pytest --cov=. --cov-report=html"
  
  # Documentation
  - "sphinx-build -b html docs docs/_build"
  
  # Security scanning
  - "bandit -r ."

Default Behavior

When no configuration file is provided, the system uses these defaults:

  • meta_type: MetaType.POETRY
  • field_prefix: "field_"
  • http_timeout: 5 seconds
  • file_encoding: "utf-8"
  • post_hooks: Ruff formatting and linting commands
  • use_path_prefixes_for_title_model_names: true
  • docstrings_on_attributes: false
  • generate_all_tags: false
  • literal_enums: false

Install with Tessl CLI

npx tessl i tessl/pypi-openapi-python-client

docs

cli-interface.md

configuration.md

index.md

programmatic-api.md

templates.md

tile.json