or run

npx @tessl/cli init
Log in

Version

Files

tile.json

task.mdevals/scenario-6/

Typed Collection Configuration Manager

Build a configuration manager that handles strongly-typed collections of configuration data, supporting serialization and deserialization of nested typed structures.

Requirements

Implement a Python module config_manager.py with the following functionality:

1. Configuration Data Classes

Define the following data structures with proper type hints:

  • ServerConfig: Stores server connection details

    • host (str): Server hostname
    • port (int): Server port number
    • ssl_enabled (bool): Whether SSL is enabled
  • FeatureFlags: Stores feature flag settings

    • enabled_features (List[str]): List of enabled feature names
    • feature_metadata (Dict[str, int]): Mapping of feature names to priority levels
  • AppConfiguration: Main configuration container

    • app_name (str): Application name
    • version (str): Application version
    • servers (List[ServerConfig]): List of server configurations
    • feature_flags (Optional[FeatureFlags]): Optional feature flag configuration
    • environment (Union[str, Dict[str, str]]): Either a simple environment name or a dict of environment variables

2. Configuration Manager Functions

Implement the following functions:

  • save_config(config: AppConfiguration, filepath: str) -> None: Serialize the configuration object to a JSON file
  • load_config(filepath: str) -> AppConfiguration: Deserialize a JSON file back into an AppConfiguration object

3. Requirements

  • Use proper type hints for all collections (List, Dict, Optional, Union)
  • Ensure type safety during serialization and deserialization
  • The system must correctly reconstruct nested typed objects from JSON
  • Handle both simple and complex union types

Dependencies { .dependencies }

jsons { .dependency }

Provides Python object to JSON serialization with type preservation.

Test Cases

Test 1: Basic typed collections { .test }

# test_config_manager.py
from config_manager import ServerConfig, FeatureFlags, AppConfiguration, save_config, load_config
import os

def test_basic_typed_collections():
    """Test serialization of typed lists and dicts"""
    server = ServerConfig(host="localhost", port=8080, ssl_enabled=True)
    flags = FeatureFlags(
        enabled_features=["feature_a", "feature_b"],
        feature_metadata={"feature_a": 1, "feature_b": 2}
    )
    config = AppConfiguration(
        app_name="TestApp",
        version="1.0.0",
        servers=[server],
        feature_flags=flags,
        environment="production"
    )

    filepath = "test_config.json"
    save_config(config, filepath)
    loaded = load_config(filepath)

    assert loaded.app_name == "TestApp"
    assert len(loaded.servers) == 1
    assert loaded.servers[0].port == 8080
    assert loaded.feature_flags.enabled_features == ["feature_a", "feature_b"]
    assert loaded.feature_flags.feature_metadata["feature_a"] == 1
    assert loaded.environment == "production"

    os.remove(filepath)

Test 2: Optional and Union types { .test }

# test_config_manager.py
def test_optional_and_union_types():
    """Test handling of Optional and Union types"""
    # Test with None for optional field
    config1 = AppConfiguration(
        app_name="App1",
        version="1.0",
        servers=[],
        feature_flags=None,
        environment="staging"
    )

    filepath = "test_optional.json"
    save_config(config1, filepath)
    loaded1 = load_config(filepath)
    assert loaded1.feature_flags is None

    # Test with dict for Union type
    config2 = AppConfiguration(
        app_name="App2",
        version="2.0",
        servers=[],
        feature_flags=None,
        environment={"VAR1": "value1", "VAR2": "value2"}
    )

    save_config(config2, filepath)
    loaded2 = load_config(filepath)
    assert isinstance(loaded2.environment, dict)
    assert loaded2.environment["VAR1"] == "value1"

    os.remove(filepath)

Test 3: Nested typed structures { .test }

# test_config_manager.py
def test_nested_typed_structures():
    """Test complex nested typed collections"""
    servers = [
        ServerConfig(host="server1.example.com", port=8080, ssl_enabled=True),
        ServerConfig(host="server2.example.com", port=8081, ssl_enabled=False),
        ServerConfig(host="server3.example.com", port=8082, ssl_enabled=True)
    ]

    flags = FeatureFlags(
        enabled_features=["auth", "logging", "metrics"],
        feature_metadata={"auth": 10, "logging": 5, "metrics": 3}
    )

    config = AppConfiguration(
        app_name="ComplexApp",
        version="3.2.1",
        servers=servers,
        feature_flags=flags,
        environment={"ENV": "prod", "REGION": "us-east"}
    )

    filepath = "test_nested.json"
    save_config(config, filepath)
    loaded = load_config(filepath)

    assert len(loaded.servers) == 3
    assert loaded.servers[1].host == "server2.example.com"
    assert loaded.servers[2].ssl_enabled == True
    assert len(loaded.feature_flags.enabled_features) == 3
    assert loaded.feature_flags.feature_metadata["logging"] == 5
    assert loaded.environment["REGION"] == "us-east"

    os.remove(filepath)