or run

npx @tessl/cli init
Log in

Version

Files

tile.json

task.mdevals/scenario-2/

Configuration Loader

Build a configuration loader that converts JSON data into Python objects for a web application settings system.

Requirements

Your task is to implement a configuration loader that can parse JSON configuration data and convert it into strongly-typed Python objects. The system should handle multiple configuration sections with different data types.

Configuration Structure

The configuration will contain three sections:

  1. Database Configuration - Connection details for the database

    • host (string): Database server hostname
    • port (integer): Database server port
    • username (string): Database username
    • max_connections (integer): Maximum number of connections
  2. API Settings - External API configuration

    • base_url (string): Base URL for API calls
    • timeout (integer): Request timeout in seconds
    • retry_count (integer): Number of retries for failed requests
    • enabled (boolean): Whether API integration is enabled
  3. Feature Flags - Application feature toggles

    • dark_mode (boolean): Enable dark mode UI
    • beta_features (boolean): Enable beta features
    • analytics_enabled (boolean): Enable analytics tracking

Implementation

Create Python classes to represent each configuration section using dataclasses with type hints. Implement a load_config function that takes JSON strings for each section and returns the corresponding Python objects.

@generates

API

from dataclasses import dataclass

@dataclass
class DatabaseConfig:
    host: str
    port: int
    username: str
    max_connections: int

@dataclass
class APISettings:
    base_url: str
    timeout: int
    retry_count: int
    enabled: bool

@dataclass
class FeatureFlags:
    dark_mode: bool
    beta_features: bool
    analytics_enabled: bool

def load_config(json_string: str, config_type: type):
    """
    Load configuration from a JSON string and convert to the specified type.

    Args:
        json_string: JSON string containing configuration data
        config_type: The target class type to deserialize into

    Returns:
        An instance of the specified config_type
    """
    pass

Test Cases

  • Loading a valid DatabaseConfig JSON string returns a DatabaseConfig object with correct attributes @test
  • Loading a valid APISettings JSON string returns an APISettings object with correct attributes @test
  • Loading a valid FeatureFlags JSON string returns a FeatureFlags object with correct attributes @test
  • The returned objects have the correct types for all attributes @test

Dependencies { .dependencies }

jsons { .dependency }

Provides JSON serialization and deserialization support for Python objects.