fn {{ method_name }}({{ sig }}) -> {{ ret_ty }} {
    Python::attach(|py| {
        let obj = self.python_obj.bind(py);
        if !obj.hasattr("{{ method_name }}").unwrap_or(false) {
            return {{ default_result_expr }};
        }

        match {{ py_call }} {
            Err(_) => {{ default_result_expr }},
            Ok(result) => {
                if let Ok(s) = result.extract::<String>() {
                    match s.as_str() {
{%- for variant in unit_result_variants %}
                        "{{ variant.wire_name }}" => {{ ret_ty }}::{{ variant.name }},
{%- endfor %}
                        _ => {{ unknown_string_result_expr }},
                    }
                } else if result.is_none() {
                    {{ default_result_expr }}
                } else {
                    let py_dict = result.cast::<pyo3::types::PyDict>();
                    if let Ok(d) = py_dict {
{%- for variant in payload_result_variants %}
                        if let Some(v) = d.get_item("{{ variant.wire_name }}").ok().flatten() {
                            return {{ ret_ty }}::{{ variant.name }}(v.extract::<String>().unwrap_or_default());
                        }
{%- endfor %}
                        {{ default_result_expr }}
                    } else {
                        {{ default_result_expr }}
                    }
                }
            }
        }
    })
}
