Initiailzer for PyCStructure by youknowone · Pull Request #6586 · RustPython/RustPython
573-596: Consider logging or erroring on malformed field tuples.
When iterating fields, if a field doesn't downcast to a tuple or lacks a PyStr name (lines 577-580), it's silently skipped without consuming a positional argument. While this may be intentional for flexibility, it could mask issues where _fields_ contains unexpected entries.
If the expectation is that all fields are well-formed (since process_fields validates them earlier), this should be safe. However, a debug assertion or logging might help catch inconsistencies during development.
540-628: Consider extracting shared initialization logic to reduce duplication.
The init_pos_args method and Initializer::init implementation are nearly identical to their counterparts in union.rs (lines 458-547). This ~90 lines of duplicated logic could be refactored into a shared helper in base.rs or via a trait with default implementations.
For example, a generic helper could be added to PyCData or a new trait:
// In base.rs pub fn init_cdata_from_args<T: AsRef<PyCData>>( zelf: &Py<T>, cls: &Py<PyType>, args: &FuncArgs, vm: &VirtualMachine, ) -> PyResult<()> { /* shared logic */ }
This is optional and can be deferred, as the current implementation is correct.