{"record":{"id":"fcd4f6573d4a4c3c","repo":"nautechsystems/nautilus_trader","slug":"failed-to-set-attribute-key-setattr-err","errorCode":null,"errorMessage":"Failed to set attribute {key}: {setattr_err}","messagePattern":"Failed to set attribute (.+?): (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/backtest/src/python/node.rs","lineNumber":549,"sourceCode":"    log::debug!(\"Created config dict: {py_dict:?}\");\n\n    // Try kwargs first, then default constructor with setattr\n    let config_instance = match config_class.call((), Some(&py_dict)) {\n        Ok(instance) => {\n            log::debug!(\"Created config instance with kwargs\");\n            instance\n        }\n        Err(kwargs_err) => {\n            log::debug!(\"Failed to create config with kwargs: {kwargs_err}\");\n\n            match config_class.call0() {\n                Ok(instance) => {\n                    log::debug!(\"Created default config instance, setting attributes\");\n                    for (key, value) in config {\n                        let py_value = config_value_to_py(py, key, value)?;\n\n                        if let Err(setattr_err) = instance.setattr(key, py_value) {\n                            anyhow::bail!(\"Failed to set attribute {key}: {setattr_err}\");\n                        }\n                    }\n\n                    // Only call __post_init__ if it exists (setattr path\n                    // needs it, kwargs path already triggered it via __init__)\n                    if instance.hasattr(\"__post_init__\")? {\n                        instance.call_method0(\"__post_init__\")?;\n                    }\n\n                    instance\n                }\n                Err(default_err) => {\n                    anyhow::bail!(\n                        \"Failed to create config instance. \\\n                         Tried kwargs: {kwargs_err}, default: {default_err}\"\n                    );\n                }\n            }","sourceCodeStart":531,"sourceCodeEnd":567,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/backtest/src/python/node.rs#L531-L567","documentation":"When the config class cannot be constructed via kwargs, create_config_instance falls back to building a default instance and setting each config key via setattr. If Python's setattr fails for any key (e.g. the dataclass field does not exist, or a __setattr__/__set_name__ check rejects the value), the error wraps the original Python exception message.","triggerScenarios":"Config dict contains a key not present on the default config class; a field is frozen or has a __post_init__/property that rejects assignment; value has the wrong type for a field with runtime validation.","commonSituations":"Renamed or removed config fields after a NautilusTrader upgrade; passing custom keys to a stock config class; typos in config keys (e.g. 'bar_type' vs 'bartype').","solutions":["Compare config keys against the config class fields and remove/rename unknown keys","Check the wrapped setattr_err message to find the exact failing attribute and its rejection reason","Construct the config directly in Python with correct kwargs instead of relying on the default-instance fallback","After upgrading NautilusTrader, diff your config keys against the new config class definitions"],"exampleFix":"// before\nconfig = {\"bar_types\": \"1-MINUTE-LAST\", \"strategy_id\": \"S-001\"}  # typo: bar_types\n// after\nconfig = {\"bar_type\": \"1-MINUTE-LAST\", \"strategy_id\": \"S-001\"}","handlingStrategy":"validation","validationCode":"import dataclasses\ndef validate_config_keys(config_cls, config: dict):\n    valid = {f.name for f in dataclasses.fields(config_cls)}\n    bad = set(config) - valid\n    if bad:\n        raise ValueError(f\"unknown config keys for {config_cls.__name__}: {sorted(bad)}\")","typeGuard":null,"tryCatchPattern":"try:\n    instance = create_config_instance(config_cls, config)\nexcept Exception as e:\n    log.error(\"config setattr failed: %s\", e)","preventionTips":["After upgrades, diff your config keys against the config class dataclass fields","Avoid typos by building configs from a typed schema, not hand-written dicts","Don't pass extra keys 'for later' — unknown keys fail setattr","Test config instantiation in Python before feeding it to the backtest node"],"tags":["rust","python","config","setattr"],"backgroundTag":"invalid-config-value","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}