{"id":9885,"library":"lazr-config","title":"lazr-config","description":"lazr.config is a Python library for defining configuration schemas, processing configuration data from various sources (like files or command-line arguments), and validating that data against the defined schema. It ensures that applications receive well-formed and valid configurations, reducing runtime errors. The current version is 3.1, and it typically follows an as-needed release cadence driven by Launchpad's requirements.","status":"active","version":"3.1","language":"en","source_language":"en","source_url":"https://github.com/launchpad/lazr.config","tags":["configuration","schema","validation","config-management"],"install":[{"cmd":"pip install lazr.config","lang":"bash","label":"Install `lazr.config`"}],"dependencies":[{"reason":"Provides an interface system used internally for defining schema and configuration components.","package":"zope.interface"}],"imports":[{"symbol":"ConfigurationSchema","correct":"from lazr.config import ConfigurationSchema"},{"symbol":"Field","correct":"from lazr.config import Field"},{"symbol":"Config","correct":"from lazr.config import Config"},{"symbol":"ConfigError","correct":"from lazr.config import ConfigError"},{"note":"Used for parsing command-line arguments into a Config object.","symbol":"CommandLineArgumentParser","correct":"from lazr.config import CommandLineArgumentParser"},{"note":"Used for loading configurations from files.","symbol":"ConfigFile","correct":"from lazr.config import ConfigFile"}],"quickstart":{"code":"from lazr.config import ConfigurationSchema, Field, Config, ConfigError\n\n# 1. Define a configuration schema\nclass MySchema(ConfigurationSchema):\n    db_host = Field(str, default='localhost', doc=\"Database hostname\")\n    db_port = Field(int, default=5432, doc=\"Database port\")\n    debug_mode = Field(bool, default=False, doc=\"Enable debug logging\")\n\n# 2. Create a configuration instance from a dictionary\nconfig_data = {\n    'db_host': 'prod.database.com',\n    'db_port': 5432,\n    # 'debug_mode' is omitted, will use default\n}\n\ntry:\n    config = Config(MySchema, config_data)\n    print(f\"DB Host: {config.db_host}\")\n    print(f\"DB Port: {config.db_port}\")\n    print(f\"Debug Mode: {config.debug_mode}\")\n\n    # Accessing an unknown field raises an AttributeError\n    # print(config.unknown_field)\n\nexcept ConfigError as e:\n    print(f\"Configuration error: {e}\")\n\n# Example of an invalid configuration\ninvalid_config_data = {\n    'db_port': 'not_a_number', # This will fail validation\n}\ntry:\n    invalid_config = Config(MySchema, invalid_config_data)\nexcept ConfigError as e:\n    print(f\"Caught expected error for invalid config: {e}\")","lang":"python","description":"This quickstart demonstrates how to define a configuration schema using `ConfigurationSchema` and `Field`, and then create a validated `Config` instance from a Python dictionary. It also shows how type mismatches lead to `ConfigError`."},"warnings":[{"fix":"Upgrade your Python environment to 3.8 or newer. If Python 2 is required, use lazr.config<3.0 and be aware it is no longer maintained.","message":"Python 2 support was officially dropped in lazr.config version 3.0. Projects still relying on Python 2 will need to use an older version (e.g., 2.x) or migrate to Python 3.","severity":"breaking","affected_versions":">=3.0"},{"fix":"Ensure that the data provided to the `Config` object, regardless of its source, strictly conforms to the types defined in the `ConfigurationSchema`.","message":"Field types in lazr.config schemas are strictly enforced. Providing a value that does not match the declared type (e.g., a string for an integer field) will result in a `ConfigError`.","severity":"gotcha","affected_versions":"all"},{"fix":"All configuration parameters you intend to use must be explicitly declared as `Field`s within your `ConfigurationSchema`. If you need dynamic access, consider using `Config.get_raw_data()` to access the underlying dictionary.","message":"Attempting to access a configuration field that was not defined in the `ConfigurationSchema` will raise an `AttributeError` on the `Config` instance, similar to accessing an undeclared attribute on a regular Python object.","severity":"gotcha","affected_versions":"all"},{"fix":"For file-based configuration, use `ConfigFile(schema, config_file_path)` and then `config_object.config` or similar. For command-line, use `CommandLineArgumentParser(schema)` to parse arguments into a `Config` instance.","message":"The `Config` class constructor directly takes a dictionary for configuration data. For loading from external sources like configuration files (e.g., INI, YAML) or command-line arguments, you must use helper classes like `ConfigFile` or `CommandLineArgumentParser`.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure `lazr.config` is installed: `pip install lazr.config`. If using virtual environments, ensure it's installed in the correct one.","cause":"The `lazr.config` package is not installed or is installed incorrectly for your Python environment.","error":"ImportError: No module named 'lazr.config' or ImportError: cannot import name 'ConfigurationSchema' from 'lazr.config'"},{"fix":"Review your `ConfigurationSchema` and the input configuration data. Ensure that values passed for each field are of the correct Python type (e.g., an actual integer for an `int` field, a boolean for a `bool` field).","cause":"A value provided in the configuration data does not match the type specified for that field in the `ConfigurationSchema`.","error":"lazr.config.ConfigError: invalid type for field 'my_field_name': expected <class 'int'>, got 'some_string_value'"},{"fix":"Add `my_undeclared_field = Field(...)` to your `ConfigurationSchema` definition. All accessible configuration parameters must be part of the schema.","cause":"You are attempting to access a configuration parameter through the `Config` object that was not explicitly defined as a `Field` in the `ConfigurationSchema`.","error":"AttributeError: 'Config' object has no attribute 'my_undeclared_field'"},{"fix":"Either provide a value for the 'missing_field' in your input configuration, or add a `default` value to the `Field` definition in your `ConfigurationSchema` if it's not always required.","cause":"A `Field` was defined in the schema as required (i.e., no `default` value was given), but no value was provided for it in the input configuration data.","error":"lazr.config.SchemaError: Required Field 'missing_field' has no default value and was not provided."}]}