nautechsystems/nautilus_trader · error

Failed to set attribute {key}: {setattr_err}

Error message

Failed to set attribute {key}: {setattr_err}

What it means

Fallback configuration path: when building the config object from keyword arguments fails, `create_config_instance` constructs a default instance and sets each config key as a Python attribute via setattr. If any setattr fails (typically because the config class has no such attribute or disallows attribute assignment), this error is raised naming the key and the underlying Python exception.

Source

Thrown at crates/system/src/python/registration.rs:598

    let config_class = config_module
        .getattr(config_class_name)
        .map_err(|e| anyhow::anyhow!("Failed to get config class {config_class_name}: {e}"))?;
    let py_dict = PyDict::new(py);

    for (key, value) in config {
        let py_value = config_value_to_py(py, key, value)?;
        py_dict.set_item(key, py_value)?;
    }

    let config_instance = match config_class.call((), Some(&py_dict)) {
        Ok(instance) => instance,
        Err(kwargs_err) => match config_class.call0() {
            Ok(instance) => {
                for (key, value) in config {
                    let py_value = config_value_to_py(py, key, value)?;

                    if let Err(setattr_err) = instance.setattr(key, py_value) {
                        anyhow::bail!("Failed to set attribute {key}: {setattr_err}");
                    }
                }

                if instance.hasattr("__post_init__")? {
                    instance.call_method0("__post_init__")?;
                }

                instance
            }
            Err(default_err) => {
                anyhow::bail!(
                    "Failed to create config instance. Tried kwargs: {kwargs_err}, default: {default_err}"
                );
            }
        },
    };

    Ok(Some(config_instance))

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Fix or remove the config key named in the message so it matches a real field of the config class
  2. Compare your config keys against the class definition (e.g. `MyConfig.__dataclass_fields__` or its signature) for the installed library version
  3. Upgrade/downgrade so config keys match the library version's schema; check RELEASES.md for renamed fields
  4. Prefer passing a matching kwargs dict so the constructor path succeeds and setattr is never used

Example fix

// before
config = {"bar_type": ..., "trade_size": ..., "insturment_id": ...}  # typo
// after
config = {"bar_type": ..., "trade_size": ..., "instrument_id": ...}
Defensive patterns

Strategy: validation

Validate before calling

import inspect
valid = set(inspect.signature(ConfigClass).parameters)
bad = set(config.keys()) - valid
assert not bad, f"unknown config keys: {bad}"

Type guard

def keys_match_config(config: dict, config_class) -> bool:
    import inspect
    params = set(inspect.signature(config_class).parameters)
    return set(config) <= params

Try / catch

try:
    trader.add_actor_from_importable_config(cfg)
except ValueError as e:
    if "Failed to set attribute" in str(e):
        logger.error("unknown/mismatched config key: %s", e)
        raise ConfigError(str(e)) from e
    raise

Prevention

When it happens

Trigger: Supplying a config dict key that does not correspond to any attribute/field of the target config class, or a class with __slots__/frozen semantics rejecting setattr; the kwargs constructor path already failed before this fallback ran.

Common situations: Typo'd or renamed config keys in JSON/YAML after a library version changed the config class fields; passing extra keys not defined on the NautilusTrader config dataclass; using a custom config class that rejects dynamic attributes.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/c1a32ff6c202525c. Report an issue: GitHub.